A script that sends emails could be very useful for reports. We could use it to create personalized alerts about any topic we want or to monitor our running scripts at a distance.
By sending an email to yourself with the conditions of your choice, as coded in your script, you can create completely customized alerts.
Before continuing be aware that Gmail made some security change some time ago. You now have to allow “less secure apps” on your account. Otherwise, you won’t be able to connect to it with your python script.
1. Send a basic email
There’s a native library in Python to send emails: smtplib. No need to install external librairies!
To send a basic email (without subject line), with a Gmail address, the code is simple:
import smtplib server = smtplib.SMTP('smtp.gmail.com', 587) server.starttls() server.login("YOUR EMAIL ADDRESS", "YOUR PASSWORD") msg = "YOUR MESSAGE!" server.sendmail("YOUR EMAIL ADDRESS", "THE EMAIL ADDRESS TO SEND TO", msg) server.quit()
line 2, it’s the parameters for the Gmail server. First, the server location (or its ip address), then the port to use. If you have an email address from another service, like Yahoo for example, you have to find the corresponding information.
line 4, there’s a security function, needed to connect to the Gmail server. It will protect your password.
Don’t forget to indicate your email address and your password on
The msg variable will contain your message and the last line will send it!
2. Send a more elaborate email
With the code below, you will send a clean email, with a sender, a receiver and a subject line.
To do this, you need two more modules: email.MIMEMultipart et email.MIMEText. They are part of the basic Python librairies. No need to install them.
import smtplib from email.MIMEMultipart import MIMEMultipart from email.MIMEText import MIMEText fromaddr = "YOUR ADDRESS" toaddr = "ADDRESS YOU WANT TO SEND TO" msg = MIMEMultipart() msg['From'] = fromaddr msg['To'] = toaddr msg['Subject'] = "SUBJECT OF THE MAIL" body = "YOUR MESSAGE HERE" msg.attach(MIMEText(body, 'plain')) server = smtplib.SMTP('smtp.gmail.com', 587) server.starttls() server.login(fromaddr, "YOUR PASSWORD") text = msg.as_string() server.sendmail(fromaddr, toaddr, text) server.quit()
Again, don’t forget to change the following lines:
Line 5 with your email address
Line 5 with the receiver’s email address
Line 10 with the email subject
Line 12 with your message
Line 17 with your password
3. Send an email with attachment
To have files attached to an email requires a more complicated code. While doing some research, I noticed almost everybody has a slightly different method to do this.
I tried to keep the code as simple as possible. However, many modules are still needed, as you can see below.
In summary, the essential step is to convert the file into a Base64 before sending it. This code works for text files, pdf files, images, audio files and video files!
import smtplib from email.MIMEMultipart import MIMEMultipart from email.MIMEText import MIMEText from email.MIMEBase import MIMEBase from email import encoders fromaddr = "YOUR EMAIL" toaddr = "EMAIL ADDRESS YOU SEND TO" msg = MIMEMultipart() msg['From'] = fromaddr msg['To'] = toaddr msg['Subject'] = "SUBJECT OF THE EMAIL" body = "TEXT YOU WANT TO SEND" msg.attach(MIMEText(body, 'plain')) filename = "NAME OF THE FILE WITH ITS EXTENSION" attachment = open("PATH OF THE FILE", "rb") part = MIMEBase('application', 'octet-stream') part.set_payload((attachment).read()) encoders.encode_base64(part) part.add_header('Content-Disposition', "attachment; filename= %s" % filename) msg.attach(part) server = smtplib.SMTP('smtp.gmail.com', 587) server.starttls() server.login(fromaddr, "YOUR PASSWORD") text = msg.as_string() server.sendmail(fromaddr, toaddr, text) server.quit()
On top of all the authentification needed, this time you also need to indicate:
The name of the file on
The path to the file on
It’s done! You have everything you need to add email sending to your python scripts.