Python – Receive and send emails in python


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 receives an email addressed to, 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?

Best Answer

Here is a very simple example:

import smtplib

server = ''
user = ''
password = ''

recipients = ['', '']
sender = ''
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.