Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content
No results
  • About AskPython
  • Blog
  • Contact Me
  • Free Online Developer Tools Hub
  • Home
  • Meet the Team
  • Privacy Policy
  • Python
  • Python Course for Beginners Online FREE
  • Python Tutorial – Mega Index
  • Terms and Conditions

🚀 Supercharge your YouTube channel's growth with AI.

Try YTGrowAI Free
  • Home
  • Blog
  • About AskPython
  • Python Course
  • Start Here

Python HowTo – Sending Emails using Python

  • Vijaykrishna RamVijaykrishna Ram
  • July 24, 2020
  • Python Programming Examples
Python HowTo – Sending Emails using Python

Hello everyone! In today’s article, we’ll be taking a look at how we can use Python to send emails.

Sending Emails – A brief overview

Usually, the task of sending emails is done using the MTP (Mail Transfer Protocol). In the present day, there is a separate protocol called SMTP (Simple Mail Transfer Protocol) which is the widely used protocol for sending emails.

This protocol works on a client-server basis, similar to any other. When we want to send an email to a target machine, we (the client) need to send the mail contents to the SMTP Server. The server will now route it to the desired target machine.

So, in order to send an email, you’ll need to use a SMTP Server. While you may have a custom SMTP Server, we’ll be using Gmail’s free SMTP Server to send emails using Gmail!


Prerequisite Setup for Sending Emails with Python

Before going through the rest of this tutorial, I’d advice you to set up a dummy gmail account that you can use to test sending emails.

After setting up the account, there’s one more thing you need to do.

By default, your Gmail account is not configured to allow access from less secure applications such as SMTP. We need to enable this access for our account.

You can go to your gmail account configuration page and enable access from your Google account.

Less Secure Access Gmail
Less Secure Access Gmail

Now, you’re ready to send emails using Python! Let’s move on.


Send Emails using Python SMTP

Python has an SMTP client library (smtplib), which it will use to send emails to an SMTP server (Gmail).

This is a part of the standard library, so you can directly import it!

import smtplib

Okay, so now let’s try writing a script to send a test email.

Any email using SMTP must have the following contents:

  • The Sender address
  • The receiver address
  • A subject (Optional)
  • The body of the mail

Let’s write all of them down.

import smtplib

sender_address = "[email protected]" # Replace this with your Gmail address

receiver_address = "[email protected]" # Replace this with any valid email address

account_password = "xxxxxxxxxx" # Replace this with your Gmail account password

subject = "Test Email using Python"

body = "Hello from AskPython!\n\nHappy to hear from you!\nWith regards,\n\tDeveloper"

# Endpoint for the SMTP Gmail server (Don't change this!)
smtp_server = smtplib.SMTP_SSL("smtp.gmail.com", 465)

# Login with your Gmail account using SMTP
smtp_server.login(sender_address, account_password)

# Let's combine the subject and the body onto a single message
message = f"Subject: {subject}\n\n{body}"

# We'll be sending this message in the above format (Subject:...\n\nBody)
smtp_server.sendmail(sender_address, receiver_address, message)

# Close our endpoint
smtp_server.close()

Make sure you replace the sender_address, receiver_address and account_password with your Gmail account information!

What we’re doing is that we use the SMTP Server to access our Gmail account, using a Secure SMTP (SMTP_SSL). After we login, we can send the message to the receiver directly, using smtp_server.sendmail()!

Now, if you enter the same account for the sender and receiver, you’ll get an email similar to mine.

Python Test Email
Python Test Email

Let’s check the contents.

Python Test Email Result
Python Test Email Result

Indeed, we’ve just sent a proper email using Python!

You can improve the code, to ensure that the resources are always closed, using context managers.

import smtplib

sender_address = "[email protected]" # Replace this with your Gmail address

receiver_address = "[email protected]" # Replace this with any valid email address

account_password = "xxxxxxxxxx" # Replace this with your Gmail account password

subject = "Test Email using Python"

body = "Hello from AskPython!\n\nHappy to hear from you!\nWith regards,\n\tDeveloper"

# We can use a context manager
with smtplib.SMTP_SSL("smtp.gmail.com", 465) as smtp_server:
    # Login with your Gmail account using SMTP
    smtp_server.login(sender_address, account_password)

    # Let's combine the subject and the body onto a single message
    message = f"Subject: {subject}\n\n{body}"

    # We'll be sending this message in the above format (Subject:...\n\nBody)
    smtp_server.sendmail(sender_address, receiver_address, message)

This will give the same results as before – another email!


Conclusion

In this article, we looked at how we could use Python to send emails easily, using gmail’s SMTP server.

References

  • Python SMTP Documentation

Vijaykrishna Ram
Vijaykrishna Ram
Articles: 99
Django Models – A Complete Beginner’s Guide
Previous Post Django Models - A Complete Beginner's Guide
Next Post Python Signal Module - What are Signals and How to Create Them?
Python Signal Module – What are Signals and How to Create Them?

Related Posts

How to Calculate Distance Between Two Points in Python

How to Calculate Distance Between Two Points in Python

  • November 26, 2026
CIFAR 10 Dataset: Load and Use in Python

CIFAR 10 Dataset: Load and Use in Python

  • November 24, 2026
Maximum A Posteriori Estimation in Python

Maximum A Posteriori Estimation in Python

  • November 24, 2026

Partner Websites

  • GoLangDocs
  • LinuxForDevices
  • CodeForGeek

Popular Categories

  • Python Programming
  • Python Modules
  • OOPS in Python
  • Python Programming Examples
  • Strings in Python

Hall of Fame

  • Python Programming
  • OOPS in Python
  • Creating Python Modules
  • Args and Kwargs in Python
  • Python Dictionary (Dict) Tutorial
  • Python List – 15 Things You MUST Know
  • Python Set – Things You MUST Know
  • Python String Functions: Methods by Task

Important Links

  • Home
  • Python Course
  • Start Here
  • Blog
  • About AskPython
Latest posts
  • np.linalg.eig: Eigenvalues and Eigenvectors
  • What Does %s Mean in a Python Format String?
  • How to Use np.sin() in NumPy
  • cv2.imread: Load Images with OpenCV
  • pd.read_json: Read JSON Into a DataFrame

Copyright © 2026 - AskPython. All rights reserved.