Python mail filter

Discussion in 'Python' started by Mailer, Feb 8, 2005.

  1. Mailer

    Mailer Guest

    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
    Mailer, Feb 8, 2005
    #1
    1. Advertising

  2. Mailer

    Kartic Guest

    Mika,

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

    http://groups-beta.google.com/group..._doneTitle=Back to Search&&d#e4d6c1efc9a71e39

    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
    Kartic, Feb 8, 2005
    #2
    1. Advertising

  3. Mailer

    Kartic Guest

    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.
    Kartic, Feb 8, 2005
    #3
  4. Mailer

    Tim Roberts Guest

    "Mailer" <> wrote:
    >
    >(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).


    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,
    Providenza & Boekelheide, Inc.
    Tim Roberts, Feb 9, 2005
    #4
  5. Mailer wrote:
    > 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.


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

    smtp = smtplib.SMTP('localhost')
    smtp.sendmail(sender, 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
    =?ISO-8859-1?Q?Ulf_G=F6ransson?=, Feb 9, 2005
    #5

  6. >> (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).


    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/
    Skip Montanaro, Feb 9, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. zax75
    Replies:
    1
    Views:
    1,077
  2. Le
    Replies:
    0
    Views:
    1,026
  3. Replies:
    3
    Views:
    102
    Hugh Sasse
    Nov 30, 2005
  4. Bertram Scharpf

    [ANN] Cropmail 0.9 mail filter

    Bertram Scharpf, Dec 24, 2006, in forum: Ruby
    Replies:
    11
    Views:
    194
    James Britt
    Dec 28, 2006
  5. Horst Lemminger
    Replies:
    1
    Views:
    424
    Dr Eberhard Lisse
    Dec 20, 2011
Loading...

Share This Page