Sending emails is a common task in many applications. From sending verification links to sending reports automatically, Python makes it easy to connect with Gmail and send emails with just a few lines of code.
Real-world use cases of sending emails in Python using Gmail include businesses automating invoices or reports for clients, students easily sending project files or assignments directly from Python, and web applications delivering welcome emails or secure password reset links to users.
In this tutorial, we will learn how to send emails in Python using Gmail step by step. We will use Gmail’s SMTP server along with an App Password for secure authentication. By the end, you’ll know how to send simple emails, HTML formatted emails, and even emails with attachments.
Before you start, make sure you’ve created a Gmail App Password. If you don’t know how, check out our detailed guide on How to generate Gmail App Password
Setting Up to Send Emails in Python Using Gmail
To send emails in Python using Gmail, we’ll use the built-in smtplib and email libraries. The flow is simple:
- Connect to Gmail’s SMTP server.
- Log in with your Gmail email and App Password.
- Create the email content (plain text, HTML, or attachments).
- Send the email securely.
Send a Simple HTML Email in Python Using Gmail
HTML emails are useful when you want to send formatted text, clickable links, or even styled content. Let’s take an example to send HTML email using python.
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
# Gmail credentials
sender_email = "your_email@gmail.com"
receiver_email = "receiver_email@example.com"
app_password = "your_app_password"
# Create the email
message = MIMEMultipart("alternative")
message["Subject"] = "Welcome to Codewolfy Gmail Tutorial"
message["From"] = sender_email
message["To"] = receiver_email
# HTML content
html = """
<html>
<body>
<h2 style="color:blue;">Hello from Codewolfy!</h2>
<p>This is a <b>HTML email</b> sent using Python and Gmail.</p>
<p>Visit <a href="https://codewolfy.com">Codewolfy.com</a> for more.</p>
</body>
</html>
"""
# Attach HTML to message
message.attach(MIMEText(html, "html"))
# Send email using Gmail SMTP
with smtplib.SMTP_SSL("smtp.gmail.com", 465) as server:
server.login(sender_email, app_password)
server.sendmail(sender_email, receiver_email, message.as_string())
print("HTML Email sent successfully!")
As you can see, we start by importing the required Python libraries: smtplib to connect with Gmail’s SMTP server and email.mime classes to structure our message.
Then, we define the sender’s Gmail address, the receiver’s email, and the Gmail App Password for authentication. Next, we create a MIMEMultipart message object where we set the subject, sender, and recipient. Inside this email, we add HTML content .
Finally, we connect to Gmail’s SMTP server using a secure SSL connection on port 465. After logging in with the sender’s Gmail and App Password, It sends the email to the recipient, and the script prints a confirmation message once email is sent.
Send an Email with Attachment in Python
Attachments are useful for sending invoices, reports, or images. In this example, we send email using google mail with PDF file attached. Let’s see example first and then understand it.
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
# Gmail credentials
sender_email = "sender@codewolfy.com"
receiver_email = "receiver@codewolfy.com"
app_password = "codewolfy"
# Create the email
message = MIMEMultipart()
message["Subject"] = "Monthly Report"
message["From"] = sender_email
message["To"] = receiver_email
# Email body
body = "Hello, please find attached the monthly report."
message.attach(MIMEText(body, "plain"))
# Attach a file (e.g., report.pdf)
filename = "report.pdf"
with open(filename, "rb") as attachment:
part = MIMEBase("application", "octet-stream")
part.set_payload(attachment.read())
# Encode file in ASCII
encoders.encode_base64(part)
part.add_header(
"Content-Disposition",
f"attachment; filename= {filename}",
)
message.attach(part)
# Send email
with smtplib.SMTP_SSL("smtp.gmail.com", 465) as server:
server.login(sender_email, app_password)
server.sendmail(sender_email, receiver_email, message.as_string())
print("Email with attachment sent successfully!")
Some of the part of code is common for both examples. The main difference in this example is, PDF attachment. Once we configure sender and receiver, we are reading a PDF file and loading it as attachment to the mail.
Conclusion
Now you know how to send emails in Python using Gmail with real-world examples. We covered sending HTML emails for formatted content and emails with attachments for files like reports. With Gmail App Passwords, the process is both secure and reliable.
In real projects, you may want to send multiple files in a single email, such as invoices, reports, or images. By moving the file attachment logic into a separate helper function, you can easily loop through a list of files and attach each one without repeating code. However, for better understanding in this tutorial, we have kept the example simple with just a single file attachment.