Sending emails with the Raspberry Pi

I recently learned how to send emails through gmail using Python, and thought I would share it. I recommend that you create a new email specifically for this purpose. Google blocks a lot of potentionally dangerous access to your account, so you need to turn this feature. You have been warned, you will be opening up the account to less security.

It certainly helps to know some background about email. This way you can understand why the objects are named the way they are in the libraries we are about to use.

When you send an email through Gmail for example, you send that message to an email server using a protocol called Simple Mail Transfer Protocol (SMTP). That message will be in a format called Multipurpose Internet Mail Extension (MIME).

Now for Python to send emails, we will need to use two libraries. The email library contains objects that make composing MIME format emails very easy. The smtplib library will help us connect and send emails to the email server.

My code is uploaded to Github for you to use. I will go through the code to explain what is really going on here.

First we import the smtp library and needed objects from the email library. The email objects will make more sense later on.

# First import the needed libraries
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
from email.mime.text import MIMEText
import smtplib

Then fill in the input information. You will need to put the email address you want to send emails from and the password. Fill in the people you want to send to with a subject line and message. Also include a path to the image of your choice.

# Fill in email login details
MY_ADDRESS = "[email protected]"
PASSWORD = "password123"

# First define where you want to send and subject
TARGET_ADDRESS = "[email protected]"
SUBJECT = "Incorrect Cover Sheet for TPS Report"
MESSAGE = "Hello Karl. Please send me the TPS report with the correct cover sheet."
IMG = "Tps_report.png"

So back to the MIME objects, Python lets composing emails in MIME format a breeze. We first define MIMEMultipart, which is like a container like object to store all the parts of our email. The first thing we will add are headers to show who is it to, from, and the subject.

# Compose email in MIME format.
# We want to send an email with a message and png attachment
msg = MIMEMultipart(MESSAGE)
msg["Subject"] = SUBJECT
msg["To"] = TARGET_ADDRESS
msg["From"] = MY_ADDRESS

Now to add a message to the body of the email, we will use the MIMEText class. We define that class, and pass in the message we want. Then we add it to the container class using the attach method.

# Attaching message to email
mime_txt = MIMEText(MESSAGE)
msg.attach(mime_txt)

Adding the image is a bit more work. We open up the image, read it into a MIMEImage object, and attach that object to the container object.

# Attaching img to email
with open(IMG, "rb") as f:
    mime_img= MIMEImage(f.read())
msg.attach(mime_img)

Now let’s examine the try statement.

try:
    server = smtplib.SMTP_SSL("smtp.gmail.com")
    server.login(MY_ADDRESS, PASSWORD)
    server.sendmail(MY_ADDRESS, TARGET_ADDRESS, msg.as_string())
    server.quit()
    print("Email sent successfully!")
except Exception as e:
    print(e)
    print("Email not sent!")

 

We create a smtp object to connect to the email server. We use SMTP_SSL to provide some encryption since we are passing in a password. The email account credentials are passed to the login method. We send the email and then close the connection to the smtp server. If it does fail, we print the exception.

That’s it! So now you can send emails from python.

Leave a Reply

Your email address will not be published. Required fields are marked *