python
  1. python-sending-emails

Python Sending Emails

Email is one of the most popular and primary modes of communication in today's digital world. Python provides an inbuilt smtplib module that can be used to send emails from Python.

Syntax

The following is the basic syntax for sending an email using Python:

import smtplib
  
sender_email = "sender_email_address"
receiver_email = "receiver_email_address"
password = "sender_email_password"
  
message = """\
Subject: Your subject
    
Your email message goes here
"""
  
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(sender_email, password)
server.sendmail(sender_email, receiver_email, message)
print("Mail Sent!")
server.quit()

Example

Let us consider the following example where we implement the above syntax to send an email using Python:

import smtplib
  
sender_email = "your_email_address@gmail.com"
receiver_email = "receiver_email_address@gmail.com"
password = input("Enter your email password: ")
  
message = """\
Subject: Your Subject
    
Hello there,
This email is sent using Python. 
"""
  
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(sender_email, password)
server.sendmail(sender_email, receiver_email, message)
print("Mail Sent!")
server.quit()

Explanation

  • Import the smtplib module which defines an SMTP client session object that can be used to send emails to any internet machine.
  • Define the sender's email address and receiver's email address.
  • Define the password for the sender's email address
  • Define the message you want to send as a string variable.
  • Start an SMTP session with the given parameters.
  • Password is entered through input function and stored in a variable.
  • Start Transport Layer Security (TLS) for secure transmission.
  • Login to the SMTP server using the sender's email and password.
  • Send the mail using the sendmail() function, which takes three arguments: sender's email, receiver's email, and message.
  • Close the SMTP server using quit() function.

Use

Python's smtplib module can be used to send emails from Python using the Simple Mail Transfer Protocol (SMTP) server.

Important Points

  • sender_email: This is the email address of the sender.
  • receiver_email: This is the email address of the receiver.
  • password: This is the password of the sender's email address in order to authenticate and login to the SMTP server.
  • message: This is the message you want to send via email.
  • server.starttls(): Start Transport Layer Security (TLS) for secure transmission.
  • server.login(sender_email, password): Login to the SMTP server using the sender's email and password.
  • server.sendmail(sender_email, receiver_email, message): Send the mail using the sendmail() function, which takes three arguments: sender's email, receiver's email, and message.
  • server.quit(): Close the SMTP server using the quit() function.

Summary

Sending emails using Python can be a very useful feature in many applications such as sending reports, notifications, etc. Python's smtplib module provides a simple and efficient way to send emails from Python. In this tutorial, we discussed the syntax, example, explanation, use, important points, and summary of sending emails using Python.

Published on: