


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
The python code and instructions for sending emails with attachments using a gmail account. It is useful for iot projects and other applications where automated emailing is required. The code includes steps for configuring the new email account for less secure apps and attaching a file to the email.
Typology: Study notes
1 / 4
This page cannot be seen from the preview
Don't miss anything!
The Python installed on your Raspberry Pi includes libraries that can be used to email messages. This could be useful for many applications. For example, suppose you are building an alarm system. You connect a switch to your front door. When someone opens the door, your Raspberry Pi takes his or her picture (with the Pi camera) and then sends a copy of the photo to you in an email attachment.
This lesson provides the Python code for sending an email with attachment. I suggest you create a new email account on Gmail for this lesson. When you are done with the lesson, you can continue to use this email account for IoT projects.
It is necessary for the sending Gmail account to have lower security settings. That is one reason why you should not use your regular email account to send messages by a Python script.
After you create your new email account, you need to open your account settings (click the circle near upper right of page, then click on the My Account button). On the My Account page that opens, find the Sign-in & security section on the left side. Click on " Apps with account access ." Under the heading of Apps with account access, find the section labeled " Allow less secure apps ." This will probably be set to OFF. With the mouse, move the slider button to the right to turn it ON. Now your new email account is configured properly to work with Python email code.
The code for sending an email is provided below. Use the Python version 2 editor to write the code. The code is missing some elements that you must add: sender email address, recipient email address, password of sender. If you are including an attached file, then you need to include the path to the file and the file name in the places specified in the code. In addition, you may wish to edit the subject heading and body of the email message.
#fill in fromaddr and toaddr and password of sender in appropriate places below #in the line starting with attachment, fill in the path to the file you wish to attach #comment out the attachment code if you don't want to send an attachment.
import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText
from email.mime.base import MIMEBase from email import encoders
fromaddr = " EMAIL address of the sender " toaddr = " EMAIL address of the receiver "
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = " Python generated with attachment "
body = " This message was generated by a Python script running on GEAR7 Raspberry Pi "
s = smtplib.SMTP('smtp.gmail.com', 587)
s.starttls()
s.login(fromaddr, " Password_of_the_sender ")
text = msg.as_string()
s.sendmail(fromaddr, toaddr, text)
s.quit()