Open-Source Internship opportunity by OpenGenus for programmers. Apply now.
In this article, we have explained the idea of using Python to send email in Gmail with a complete Python implementation example.
Table of contents:
- Introduction to SMTP and MIME
- Generate App password in Google
- Email config file setup
- Implementation in Python
Introduction to SMTP and MIME
The application level protocol, Simple Mail Transfer Protocol (SMTP), handles message service over TCP/IP networks. It is limited to only 7 bit ASCII characters and hence do not support languages such as Arabic, Chinese, Korean, Japanese, etc. SMTP cannot used to transmit binary files such as vidoe, images and document files created by office productivity software.
Multipurpose Internet Mail Extensions (MIME) remove the limitation faced by SMTP. Email messages can support languages such as Arabic, Chinese, Korean, Japanese, etc. It is possible to attach video, images, and document files created by office productivity software in email messages.
Generate App password in Google
Go to myaccount.google.com/security, ensure that you have enabled 2-Step Verification.
Next click into the App passwords section to generate your password for our python script
Email config file setup
Create an email_config.py
file with the following
gmail_pass = "[this will be the App password you generated in the previous step]"
user = "[replace it with your gmail address, for example hello-world@gmail.com]"
host = "smtp.gmail.com"
port = 465
Implementation in Python
Let's review the following code on how to use Python to send email in Gmail. We first need to import the following modules. smtplib
for sending emails using the Simple Mail Transfer Protocol (SMTP). email
for managing email messages. email_config
which is the email_config.py
file which we had created earlier.
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from email.header import Header
from email_config import gmail_pass, user, host, port
Thereafter we create a send_email_attach
function which take in three inputs. The email address to send to, the subject and the content (body) of the email message. The email to be sent is formatted in plain text
.
def send_email_attach(to, subject, body):
# create message object
message = MIMEMultipart()
# add the header
message['From'] = Header(user)
message['To'] = Header(to)
message['Subject'] = Header(subject)
# attach nessage body as MIMEText
message.attach(MIMEText(body,'plain', 'utf-8'))
We had previously stored the host, port, user, gmail_pass
information in the email_config.py
file. At the beginning of our Python script, we had import these data which allow us to setup the email server below. With our email server setup , we are now ready to send the email.
# setup email server
server = smtplib.SMTP_SSL(host, port)
server.login(user,gmail_pass)
# send email and quit server
server.sendmail(user, to, message.as_string())
server.quit()
The below codes will request the user input for the recipient email address, subject and the content (body) of the email message. Once the input is received, the send_email_attach
function will be called and our email is sent in Gmail by our Python program.
print(f"Preparing to send email")
to = input("Who are we sending this email to? email address please ")
subject = input("Enter the subject of this email? ")
body = input("Type your email message here: ")
send_email_attach(to, subject, body)
Here is the entire code block for reference
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from email.header import Header
from email_config import gmail_pass, user, host, port
def send_email_attach(to, subject, body):
# create message object
message = MIMEMultipart()
# add the header
message['From'] = Header(user)
message['To'] = Header(to)
message['Subject'] = Header(subject)
# attach nessage body as MIMEText
message.attach(MIMEText(body,'plain', 'utf-8'))
# setup email server
server = smtplib.SMTP_SSL(host, port)
server.login(user,gmail_pass)
# send email and quit server
server.sendmail(user, to, message.as_string())
server.quit()
print(f"Preparing to send email")
to = input("Who are we sending this email to? email address please ")
subject = input("Enter the subject of this email? ")
body = input("Type your email message here: ")
send_email_attach(to, subject, body)
With this article at OpenGenus, you must have the complete idea of using Python to send email in Gmail.