tags:

views:

2270

answers:

8

How can i receive and send email in python? A 'mail server' of sorts.

I am looking into making an app that listens to see if it recieves an email addressed to [email protected], and sends an email to the sender.

Now, am i able to do this all in python, would it be best to use 3rd party libraries?

+2  A: 

Yes, you can do pretty much everything with the built-in libraries. Do a search here looking for the tags [python] and [email] and you'll see how it's done.

Harley
+1  A: 

poplib and smtplib will be your friends when developing your app.

Bullines
+2  A: 

Zed's Son of Sam may be of interest as well.

rz
+1  A: 

Python has an SMTPD module that will be helpful to you for writing a server. You'll probably also want the SMTP module to do the re-send. Both modules are in the standard library at least since version 2.3.

mcrute
+8  A: 

Here is a very simple example:

import smtplib

server = 'mail.server.com'
user = ''
password = ''

recipients = ['[email protected]', '[email protected]']
sender = '[email protected]'
message = 'Hello World'

session = smtplib.SMTP(server)
# if your SMTP server doesn't need authentications,
# you don't need the following line:
session.login(user, password)
session.sendmail(sender, recipients, message)

For more options, error handling, etc, look at the smtplib module documentation.

Manuel Ceron
That covers the sending part, if you can add a quick snippet for listening for new mails using the smptd module this would be a great answer
Jay
Note this is very far from a real email server since most of the real work (queueuing and retransmission) is done by 'mail.server.com', not by the Python program.
bortzmeyer
you're right. it's very far from a real mail server.
Manuel Ceron
+1  A: 

Depending on the amount of mail you are sending you might want to look into using a real mail server like postifx or sendmail (*nix systems) Both of those programs have the ability to send a received mail to a program based on the email address.

epochwolf
+1  A: 

The sending part has been covered, for the receiving you can use pop or imap

Toni Ruža
+3  A: 

I do not think it would be a good idea to write a real mail server in Python. This is certainly possible (see mcrute's and Manuel Ceron's posts to have details) but it is a lot of work when you think of everything that a real mail server must handle (queuing, retransmission, dealing with spam, etc).

You should explain in more detail what you need. If you just want to react to incoming email, I would suggest to configure the mail server to call a program when it receives the email. This program could do what it wants (updating a database, creating a file, talking to another Python program).

To call an arbitrary program from the mail server, you have several choices:

  1. For sendmail and Postfix, a ~/.forward containing "|/path/to/program"
  2. If you use procmail, a recipe action of |path/to/program
  3. And certainly many others
bortzmeyer
I hate when people downvote without adding a comment. What was the problem with my answer?
bortzmeyer
I don't know what the problem would be. It's basically my answer with more details. I think this is a good solution.
epochwolf
Actually python's smtpd server is pretty good for the internal apps I've used it for. SMTP really isn't all that difficult to get right, the hard part is multiplexing network connections. Not sure I'd use it to handle "real" production load though.
mcrute