Python mail filter

M

Mailer

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
 
K

Kartic

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
 
K

Kartic

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.
 
T

Tim Roberts

Mailer said:
(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.
 
?

=?ISO-8859-1?Q?Ulf_G=F6ransson?=

Mailer said:
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
 
S

Skip Montanaro

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/
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top