Send Emails Using Python
In this article, I will show you how to send emails using the python programming language. I will be using a GMail account for this as well as Googles outgoing mail server. Before you get started, if you are using a GMail account, then you will need to set up less secure app access. This is not recommended to do by Google, so be sure to do this at your own risk.
Set Up Less Secure Apps
If you prefer not to read this article and would like a video representation of it, you can check out the YouTube Video . It goes through everything in this article with a little more detail, and will help make it easy for you to start programming even if you don’t have the programming language Python installed on your computer. Or you can use both as supplementary materials for learning !
If you are interested in reading more about Python — one of the fastest-growing programming languages that many companies and computer science departments use — then I recommend you check out the book Learning Python by Mark Lutz.
First I will write a description for the program, this way I know what the program is supposed to do when I look back at it in the future.
#Description: This program sends emails
Import the libraries that will be used throughout the program.
#Import the libraries
from email.mime.text import MIMEText as MT
from email.mime.multipart import MIMEMultipart as MM
Now, I need to store the email addresses for the receiver and the sender. I also want to store the senders email password for later use.
#Store the email addresses for the reciever, and the sender. Also store the senders email password
receiver = 'firstname.lastname@example.org'
sender = 'email@example.com'
sender_password = 'PythonPassword123$'
Let’s create a MIMEMultipart object to get and store the subject of the email, the email address to send the email to for the ‘To’ column and the senders email address for the ‘From’ column.
#Create a MIMEMultipart Object
msg = MM()
msg["Subject"] = "Python Programming Is Amazing !"
msg["From"] = sender
msg["To"] = receiver
Next, create the HTML message to send as the body of the email.
#Create the HTML for the message
HTML = """
<h1>Python is cool !</h1>
<img src="https://everythingcomputerscience.com/Photos/Python_Image.png" alt="Python_Image" width="640" height="360"></img>
Check out: <a href="http://www.everythingcomputerscience.com">Everything Computerscience</a>
it has great information on computers, programming, data structures, and more.
Create an HTML MIMEText object.
#Create a html MIMEText object
MTObj = MT(HTML, "html")
Attach the MimeText object into the message container.
Last, but not least, create a secure connection with the server, login to your email account and send the email !
#Create the secure socket layer (SSL) context object
SSL_context = ssl.create_default_context()
#Create the secure Simple Mail Transfer Protocol (SMTP) connection
server = smtplib.SMTP_SSL(host ="smtp.gmail.com",port= 465, context=SSL_context) #Use Googles (Outgoing Mail Server) Simple Mail Transfer Protocol
#Login to the email account
#Send the email
server.sendmail(sender, receiver, msg.as_string())
Thanks for reading this article I hope it’s helpful to you all! If you enjoyed this article and found it helpful please leave some claps to show your appreciation ! Have a great day !