Sending emails with tools such as Thunderbird or Outlook is easy. You can integrate services with many providers such as Gmail, Hotmail, Yahoo! Mail, etc. Unfortunately, Python doesn't have a beautiful Graphical User Interface (GUI). Although, it has a complet set of tools which allows you to send emails quickly.
You built a command line software or you simply need to receive an email with results when your script is done, this tutorial will help you master the concept of sending emails with Python.
The first thing you need is to connect to the SMTP server you want to use to send your email. It requires you to have a valid username and password to that server (i.e. you need a Google Account to use Gmail).
To connect to the server, you need to find the server's domain name. A quick trick normally you only need to add stmp. in front of the email service provider. For Google's Gmail it's stmp.gmail.com.
|Provider||SMTP server domain name|
Once you have the domain name and the outgoing port associated with that domain you are ready to follow the following steps.
The following code will create a SSL connection to the smtp server.
import smtplib smtp_obj = smtplib.SMTP('smtp.provider.com', 465) smtp_obj.ehlo()
Let's import the build-in Python library that will help us manage all the smtp protocol for us. Then, we specify the domain and the port we want to use. The last step is to send the message Hello to the server. This step is very important so make sure you don't forget it
The following code will create an TLS connection to the smtp server.
import smtplib smtp_obj = smtplib.SMTP_SSL('smtp.provider.com', 587) smtp_obj.ehlo() smtp_obj.starttls()
Let's import the build-in Python library that will help us manage all the smtp protocol for us. Then, we specify the domain and the port we want to use. The last step is to send the message Hello to the server. This step is very important so make sure you don't forget it. The last line is to start a TLS Encryption.
We are almost ready to send the email. We simply need to authenticate our account with the smtp server.
Once again, let's use a simple function sendmail()to prepare and send the email.
smtp_obj.sendmail('firstname.lastname@example.org', 'email@example.com', 'Subject: Hello World\nDear recipient, I went arround the world to listen to Daft Punk latest mix. Sincerely, House-Addict')
This function require 3 arguments
To insert a subject, you need to start the email body with 'Subject:' and to end the subject simply add '\n'. After the end line character you can continue the rest of your email.
And the last step you should always do, is to disconnect from the smtp server.
You should create a quick function that you can call from any of your projects. This is the function I created for myself.
import smtplib def send_email (recipiant, subject, message): smtp_obj = smtplib.SMTP_SSL('smtp.gmail.com', 465) smtp_obj.ehlo() smtp_obj.login('firstname.lastname@example.org', 'SuperPassword') smtp_obj.sendmail('email@example.com', recipiant, 'Subject: '+str(subject)+'\n'+message) smtp_obj.quit()
Now, you simply need to call your file from any python project you are working on.
import emailme emailme.send_email(['firstname.lastname@example.org', 'email@example.com', 'firstname.lastname@example.org', 'email@example.com'], "Lucky Luke is coming for you!" "You need to hide, Lucky Luke is coming for you. Love, Ma Dalton")