DEV Community

Cover image for Send Emails for Free With Python and Gmail SMTP Mail Server API
Ali
Ali

Posted on • Updated on • Originally published at Medium

Send Emails for Free With Python and Gmail SMTP Mail Server API

Sending Emails With Python

This is the easiest way you can start sending emails with Python using only two libraries, smtplib and email.

We will be using Gmail’s free RESTful API in this examples.

Let’s check out some code!

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

message = MIMEMultipart()
message["To"] = 'To line here.'
message["From"] = 'From line here.'
message["Subject"] = 'Subject line here.'

title = '<b> Title line here. </b>'
messageText = MIMEText('''Message body goes here.''','html')
message.attach(messageText)

email = 'Your Gmail address here.'
password = 'Your app password here.'

server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo('Gmail')
server.starttls()
server.login(email,password)
fromaddr = 'From line here.'
toaddrs  = 'Address you send to.'
server.sendmail(fromaddr,toaddrs,message.as_string())

server.quit()
Enter fullscreen mode Exit fullscreen mode

Code Walkthrough

First, let’s import smtplib. Then MIMEMultipart and MIMEText from email.mime.multipart and email.mime.text respectively:

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
Enter fullscreen mode Exit fullscreen mode

Then, we call MIMEMultiPart() and instantiate it to the variable Message. We then give a string value to each variables in Message, To, From and Subject:

Message = MIMEMultipart()
Message["To"] = 'To line here.'
Message["From"] = 'From line here.'
Message["Subject"] = 'Subject line here.'
Enter fullscreen mode Exit fullscreen mode

We will also give a title to our email through the variable title, add a message to our email through MIMEText() and instantiate it to the variable messagetext and finally attach our message to our main variable Message using the attach() function.

title = '<b> Title line here. </b>'
messageText = MIMEText('''Message body goes here.''','html')
Message.attach(messageText)
Enter fullscreen mode Exit fullscreen mode

Let’s add our Gmail address and our App password, click the link here if you don’t know how to get one:

email = 'Your Gmail address here.'
password = 'Your App password here.'
Enter fullscreen mode Exit fullscreen mode

Then, we add Gmail’s SMTP server and port we will be connecting to with smtplib.SMTP(smtp.gmail.com:587) and instantiate it to the variable server, these are smtp.gmail.com and 587 respectively. (With ‘:’ in between them):

server = smtplib.SMTP('smtp.gmail.com:587')
Enter fullscreen mode Exit fullscreen mode

We then add an ehlo statement using server.ehlo(‘Gmail’), this should be your domain name, this is useful when sending en email to another mail server that supports ESMTP, let’s just put Gmail to keep it simple:

server.ehlo('Gmail')

Let’s also start the TLS protocol with server.starttls(), this tells the mail server we want to send our email through a secure connection:

server.starttls()

We login to the mail sever using server.login(email,password):

server.login(email,password)

Let’s add a from address and to address(es), using fromaddr and toaddrs respectively:


fromaddr = 'Your Gmail address.'
toaddrs  = 'Destination address.'
Enter fullscreen mode Exit fullscreen mode

Finally, we send our email using server.sendmail(fromaddr,toaddrs,message.as_string()) and we close our connection to the mail server using server.quit():

server.sendmail(fromaddr,toaddrs,message.as_string())

server.quit()
Enter fullscreen mode Exit fullscreen mode

Result

Let’s launch it in a Terminal:

Screenshot of a Terminal

Email sent directly to my inbox:

Screenshot of an email in Gmail.

Simple and easy!

Top comments (0)