How to Send Gmail via Python

Sending emails programmatically is a common task for various applications, such as sending notifications, confirmations, or alerts. Python, with its vast array of libraries, makes it straightforward to send emails through Gmail. This article will guide you through the process of sending Gmail via Python, covering necessary setups and code examples.


Gmail Account Setup

Before diving into the code, ensure that your Gmail taiwan phone number  account is set up to allow access for less secure apps, or use an App Password if you have 2-Step Verification enabled.

  1. Allowing less secure apps: Go to your Google Account settings, scroll down to “Less secure app access,” and turn it on.
  2. Using App Passwords: If you have 2-Step Verification, generate an App Password specifically for this purpose.

Sending Emails Using smtplib

taiwan phone number

Setting Up the Environment

Python’s built-in smtplib library is a powerful tool for Australia Mobile Number List sending emails using the Simple Mail Transfer Protocol (SMTP). Let’s start by importing necessary libraries and setting up our SMTP server connection.


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

def send_gmail_via_python(sender_email, receiver_email, subject, body, smtp_server, smtp_port, login, password):
# Set up the MIME
message = MIMEMultipart()
message['From'] = sender_email
message['To'] = receiver_email
message['Subject'] = subject

# Add body to email
message.attach(MIMEText(body, 'plain'))

# Connect to the server
server = smtplib.SMTP(smtp_server, smtp_port)

# Login to the server
server.login(login, password)

# Send email
text = message.as_string()
server.sendmail(sender_email, receiver_email, text)
print("Email sent successfully")
except Exception as e:
print(f"Failed to send email: {e}")
# Terminate the SMTP session

Code Explanation

Setting Up MIME

The MIMEMultipart object is used to create a multi-part message and define headers such as ‘From’, ‘To’, and ‘Subject’. The MIMEText object is used to attach the body of the email.

Connecting to the SMTP Server

Using smtplib.SMTP, we connect to the Gmail SMTP server ( on port 587. The starttls() method is called to upgrade the connection to a secure encrypted SSL/TLS connection.

Logging In and Sending the Email

The login() method authenticates using the provided email and password. Then, the sendmail() method sends the email, converting the message object to a string with as_string().

Example Usage

Here’s how you can use the send_gmail_via_python function:


sender_email = "[email protected]"
receiver_email = "[email protected]"
subject = "Test Email"
body = "This is a test email sent via Python."
smtp_server = ""
smtp_port = 587
login = "[email protected]"
password = "your_password_or_app_password"

send_gmail_via_python(sender_email, receiver_email, subject, body, smtp_server, smtp_port, login, password)

Advanced Features

Sending HTML Emails

To send an HTML email, simply change the MIMEText content type to ‘html’:


message.attach(MIMEText(body, 'html'))

Adding Attachments

To add an attachment, you can use MIMEBase and encoders:


from email.mime.base import MIMEBase
from email import encoders

def add_attachment(message, file_path):
# Open the file in binary mode
with open(file_path, 'rb') as attachment:
# Add file as application/octet-stream
part = MIMEBase('application', 'octet-stream')

# Encode file in ASCII characters

# Add header
f'attachment; filename= {file_path}',

# Attach the file to the message

# Example usage
add_attachment(message, 'path_to_your_file')


Sending Gmail via Python is a powerful capability for automating email-related tasks. With libraries like smtplib and email.mime, you can customize and send emails with various formats and attachments. By following the steps outlined in this article, you can efficiently set up and send emails through Gmail using Python, enhancing your application’s functionality and communication capabilities.

Leave a Reply

Your email address will not be published. Required fields are marked *