Learn Python – Python Sending Email using SMTP- Basic and advance

Simple Mail Transfer Protocol (SMTP) is used as a protocol to deal with the e mail transfer using Python. It is used to route emails between email servers. It is an utility layer protocol which permits to users to ship mail to another. The receiver retrieves email the usage of the protocols POP(Post Office Protocol) and IMAP(Internet Message Access Protocol).

When the server listens for the TCP connection from a client, it initiates a connection on port 587.

Python presents a smtplib module, which defines an the SMTP client session object used to send emails to an web machine. For this purpose, we have to import the smtplib module using the import statement.

$ import smtplib  

The SMTP object is used for the e-mail transfer. The following syntax is used to create the smtplib object.

import smtplib     
smtpObj = smtplib.SMTP(host, port, local_hostname)      

It accepts the following parameters.

host: It is the hostname of the machine which is running your SMTP server. Here, we can specify the IP address of the server like (https://www.javatpoint.com) or localhost. It is an optional parameter.

port: It is the port number on which the host machine is listening to the SMTP connections. It is 25 by default.

local_hostname: If the SMTP server is running on your local machine, we can mention the hostname of the local machine.

The sendmail() method of the SMTP object is used to ship the mail to the desired machine. The syntax is given below.

smtpObj.sendmail(sender, receiver, message)    

Example

#!/usr/bin/python3    
import smtplib    
sender_mail = 'sender@fromdomain.com'    
receivers_mail = ['reciever@todomain.com']    
message = """From: From Person %s  
To: To Person %s  
Subject: Sending SMTP e-mail   
This is a test e-mail message.  
"""%(sender_mail,receivers_mail)    
try:    
   smtpObj = smtplib.SMTP('localhost')    
   smtpObj.sendmail(sender_mail, receivers_mail, message)    
   print("Successfully sent email")    
except Exception:    
   print("Error: unable to send email")    

Sending email from gmail

There are cases where the emails are sent the use of the Gmail SMTP server. In this case, we can ignore Gmail as the SMTP server as a substitute of using the localhost with the port 587.

Use the following syntax.

$ smtpObj = smtplib.SMTP("gmail.com", 587)     

Here, we need to login to the Gmail account the use of Gmail user identify and password. For this purpose, the smtplib grant the login() method, which accepts the username and password of the sender.

This may additionally make your Gmail ask you for get right of entry to to much less impervious apps if you’re the usage of Gmail. You will want to turn this ON quickly for this to work.

Consider the following example.

Example

#!/usr/bin/python3    
import smtplib    
sender_mail = 'sender@gmail.com'    
receivers_mail = ['reciever@gmail.com']    
message = """From: From Person %s  
To: To Person %s  
Subject: Sending SMTP e-mail   
This is a test e-mail message.  
"""%(sender_mail,receivers_mail)    
try:    
   password = input('Enter the password');    
   smtpObj = smtplib.SMTP('gmail.com',587)    
   smtpobj.login(sender_mail,password)    
   smtpObj.sendmail(sender_mail, receivers_mail, message)    
   print("Successfully sent email")    
except Exception:    
   print("Error: unable to send email")    

Sending HTML in email

We can structure the HTML in the message by way of specifying the MIME version, content-type, and personality set to send the HTML.

Consider the following example.

Example

#!/usr/bin/python3    
import smtplib    
sender_mail = 'sender@fromdomain.com'    
receivers_mail = ['reciever@todomain.com']    
message = """From: From Person %s  
To: To Person %s  
  
MIME-Version:1.0  
Content-type:text/html  
  
  
Subject: Sending SMTP e-mail   
  
<h3>Python SMTP</h3>  
<strong>This is a test e-mail message.</strong>  
"""%(sender_mail,receivers_mail)    
try:    
   smtpObj = smtplib.SMTP('localhost')    
   smtpObj.sendmail(sender_mail, receivers_mail, message)    
   print("Successfully sent email")    
except Exception:    
   print("Error: unable to send email")