Python mail filter

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mailer

    Python mail filter

    Hello, all,

    Well, I need to write a mail filter in Python. The filter will be used in
    shared web hosting environment, so the control over MTA configuration etc is
    limited.

    The basic premise, as I understand is this:

    Read mail from stdin
    Parse headers etc using rfc822 or email module
    Process

    # Now I need to do one of the following:

    # Discard mail
    # Pass through
    # Forward to another account, possibly modifying the mail

    Now that I have coded up some stuff, the first looks easy - mails are
    getting lost. So the question is (may not be entirely specific to Python),
    how do I achieve the other two?

    Currently, I have set up a .forward that pipes the mail to my script. I can
    verify that this works by dumping the contents to a file. If I write to
    stdout, however, the mail is not delivered. That doesn't quite look right
    either - it's probably too late for the MTA to pick up. What I want to do is
    to pass the processed mail back to Postfix so it can deliver it to the
    correct local mail box.

    FYI, the setup is Sendmail/Postfix.

    (P.S. I am very much aware of the existence of procmail, TMDA etc. My
    requirements are very specific and requires integration with another
    program, so I am only interested in getting a custom solution).

    TIA,
    Mika


  • Kartic

    #2
    Re: Python mail filter

    Mika,

    Somebody else had a similar situation of having a script receive the
    email and process it.



    This is my response to that post and the latter half may help you.

    One thing you must remember is once you pipe the email to your script,
    it is considered delivered. If you want to script to process and also
    receive the email into a user account, you have to list the recipients
    in your .forward file. If you want to process the email through your
    script and _then_ send it to a user, I think the only way would be to
    send the email from your script to next intended recipient.

    Thank you,
    -Kartic

    Comment

    • Kartic

      #3
      Re: Python mail filter

      I just read your requirement of a pass through... Writing the email
      back into sys.stdout from your script might do the trick of passing it
      through to the original intended recipient.

      Comment

      • Tim Roberts

        #4
        Re: Python mail filter

        "Mailer" <mailer@pymaile r.com> wrote:[color=blue]
        >
        >(P.S. I am very much aware of the existence of procmail, TMDA etc. My
        >requirements are very specific and requires integration with another
        >program, so I am only interested in getting a custom solution).[/color]

        But are you aware that a procmail recipe can feed a message through a
        filter, and then pass the results of that filter back in to the e-mail
        system for further processing?

        It seems to me that you should consider letting procmail do what it is good
        at, and use your Python solution for the things that are beyond it.
        --
        - Tim Roberts, timr@probo.com
        Providenza & Boekelheide, Inc.

        Comment

        • Ulf Göransson

          #5
          Re: Python mail filter

          Mailer wrote:[color=blue]
          > The basic premise, as I understand is this:
          >
          > Read mail from stdin
          > Parse headers etc using rfc822 or email module
          > Process
          >
          > # Now I need to do one of the following:
          >
          > # Discard mail
          > # Pass through
          > # Forward to another account, possibly modifying the mail
          >
          > Now that I have coded up some stuff, the first looks easy - mails are
          > getting lost. So the question is (may not be entirely specific to Python),
          > how do I achieve the other two?
          >
          > Currently, I have set up a .forward that pipes the mail to my script. I can
          > verify that this works by dumping the contents to a file. If I write to
          > stdout, however, the mail is not delivered. That doesn't quite look right
          > either - it's probably too late for the MTA to pick up. What I want to do is
          > to pass the processed mail back to Postfix so it can deliver it to the
          > correct local mail box.[/color]

          I think something like this might work (snipped from a small script I
          use to email log files to myself):

          smtp = smtplib.SMTP('l ocalhost')
          smtp.sendmail(s ender, rcpt, msg.as_string() )
          smtp.close()

          Regardless of MTA software, this should resend whatever is in msg (in my
          case a MIMEMultipart). If it should be forwarded, just change rcpt and
          the To: header.

          Unless of course, the server is configured to block messages sent by
          localhost claiming to be from somewhere else...

          /ug

          Comment

          • Skip Montanaro

            #6
            Re: Python mail filter

            [color=blue][color=green]
            >> (P.S. I am very much aware of the existence of procmail, TMDA etc. My
            >> requirements are very specific and requires integration with another
            >> program, so I am only interested in getting a custom solution).[/color][/color]

            Tim> But are you aware that a procmail recipe can feed a message through
            Tim> a filter, and then pass the results of that filter back in to the
            Tim> e-mail system for further processing?

            Should the OP need further evidence that this is possible, this is exactly
            how I use SpamBayes [1].

            Skip

            [1] http://www.spambayes.org/

            Comment

            Working...