10 May How to Send EMails from Your Gmail Account using Python
Today, we’d be looking at an interesting topic, Sending Emails from your Gmail account using Python. How does that sound? Python is such a powerful programming language.
We are going to use the SMTP(Simple Mail Transfer Protocol) library to implement this. Python provides smtplib module which defines an SMTP client session object that can be used to send mail to any Internet machine with an SMTP or ESMTP listener daemon.
The SMTP object has an instance method which is called Sendmail, this is used for mailing messages. It has three parameters which are;
- The sender − A string with the address of the sender.
- The receivers − A list of strings, one for each recipient.
- The message − A message as a string formatted as specified in the various RFCs.
Note: When sending emails with Python, you must ensure your SMTP connection is encrypted, so your message and login credentials are not easily accessed by others. SSL (Secure Sockets Layer) and TLS (Transport Layer Security) are two protocols that can be used to encrypt an SMTP connection.
Let’s dive into each of these protocols;
SMPTP WITH SSL
Here is a demonstration of how to create a secure connection with Gmail’s SMTP server using SSL Protocol.
1import smtplib, ssl 2 3port = 465 # For SSL 4password = input("type-your-password-here") 5 6# using a secure SSL context 7context = ssl.create_default_context() 8#this will be used to send our email 9 10with smtplib.SMTP_SSL("smtp.gmail.com", port, context=context) as server: 11 server.login("youremail@gmail.com", password) 12
SMPTP WITH TLS
Here is a demonstration of how to create a secure connection with Gmail’s SMTP server using TLS Protocol.
1import smtplib, ssl 2 3 4smtp_server = "smtp.gmail.com" #name of smptp server 5port = 587 # For starttls 6sender_email = "youremail@gmail.com" #put in your email address here 7password = input("type-your-password-here ") 8 9# we use a secure SSL context 10context = ssl.create_default_context() 11 12# Now we try to log in to the server and send email 13try: 14 server = smtplib.SMTP(smtp_server,port) 15 server.ehlo() # Can be omitted 16 server.starttls(context=context) # Secure the connection using the tls protocol 17 server.login(sender_email, password) 18 19except Exception as e: 20 print(e) 21finally: 22 server.quit()
Now, we’ve seen how to create a secure connection using either of the protocols listed above. The next thing to do is to put an email message which will be in plain text format for our demonstration here.
Sending Email In Simple Text Format using SSL Protocol
Here, we will pass in our email body in plain text format and then replicate the same code we used earlier for creating an SSL secured connection.
1import smtplib, ssl 2 3port = 465 # For SSL 4smtp_server = "smtp.gmail.com" 5sender_email = "youremail@gmail.com" #put in your email address here 6receiver_email = "receiveremail@gmail.com" #put in your receiver email address here 7password = input("type-your-password-here") 8message = """\ 9Subject: Hi there 10 11This message is to notify you my friend....""" 12 13context = ssl.create_default_context() 14 15with smtplib.SMTP_SSL(smtp_server, port, context=context) as server: 16 server.login(sender_email, password) 17 server.sendmail(sender_email, receiver_email, message)
Sending Email in HTML Format
You might be wondering if it’s possible to send your text in a more modified or beautified format whereby your email content is bolded, italicized, contain images, and so on.
Python email.mime
module can help in handling that. An example code is shown below on how we have both plain text and that of Html format. In this code, we will use the MIMEText from email.mime module to convert both plain text and that of Html format into MIME object, before proceeding to attach as our message body.
1 import smtplib, ssl 2 from email.mime.text import MIMEText 3 from email.mime.multipart import MIMEMultipart 4 5 sender_email = "youremail@gmail.com" 6 receiver_email = "receiveremail@gmail.com" 7 password = input("type-your-password-here") 8 9 message = MIMEMultipart("alternative") 10 message["Subject"] = "multipart test" 11 message["From"] = sender_email 12 message["To"] = receiver_email 13 14 #Create the plain-text and HTML version of your message 15 text = """\ 16 Hello, 17 How are you? 18 """ 19 html = """\ 20 <html> 21 <body> 22 <p>Hello<br> 23 How are you? 24 </p> 25 </body> 26 </html> 27 """ 28 29 # we then convert these into plain/html MIMEText objects 30 part1 = MIMEText(text, "plain") 31 part2 = MIMEText(html, "html") 32 33 #You then add the HTML/plain-text parts to MIMEMultipart message 34 # The email client will try to render the last part first 35 message.attach(part1) 36 message.attach(part2) 37 38 #create secure connection and send your email 39 context = ssl.create_default_context() 40 with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server: 41 server.login(sender_email, password) 42 server.sendmail( 43 sender_email, receiver_email, message.as_string() 44 )
And that’s a wrap. Follow the steps above to start sending emails from your Gmail using Python. For questions and observations, use the comments section below. Follow us for more informative posts. Thanks for reading.
No Comments