Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Receive and send emails in python

Tags:

python

email

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 [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?

like image 554
Josh Hunt Avatar asked Dec 08 '08 00:12

Josh Hunt


People also ask

How do you send and receive emails in Python?

Sending Emails message import EmailMessageto=["[email protected]","[email protected]"]#To whomever you want to send the maila="""Enter your body of the email. Note: Don't save your file as email.py, it won't work as it clashes with the file in which the library is written.

Can you use Python to send emails?

Python offers a ` library to send emails- “SMTP lib”. “smtplib” creates a Simple Mail Transfer Protocol client session object which is used to send emails to any valid email id on the internet. The Port number used here is '587'.


3 Answers

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.

like image 108
Manuel Ceron Avatar answered Oct 16 '22 23:10

Manuel Ceron


Found a helpful example for reading emails by connecting using IMAP:

Python — imaplib IMAP example with Gmail

import imaplib
mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('[email protected]', 'mypassword')
mail.list()
# Out: list of "folders" aka labels in gmail.
mail.select("inbox") # connect to inbox.
result, data = mail.search(None, "ALL")

ids = data[0] # data is a list.
id_list = ids.split() # ids is a space separated string
latest_email_id = id_list[-1] # get the latest

# fetch the email body (RFC822) for the given ID
result, data = mail.fetch(latest_email_id, "(RFC822)") 

raw_email = data[0][1] # here's the body, which is raw text of the whole email
# including headers and alternate payloads
like image 42
jakebrinkmann Avatar answered Oct 16 '22 22:10

jakebrinkmann


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
like image 12
bortzmeyer Avatar answered Oct 16 '22 22:10

bortzmeyer