Sending Email using SMTP

Sending Mail Transfer Protocol (SMTP) is used as a protocol to handle the email transfer using python. It is used to route emails between email servers.

Sending Email using SMTP

Sending Mail Transfer Protocol (SMTP) is used as a protocol to handle the email transfer using python. It is used to route emails between email servers.

Python provides smtplib module, which defines an SMTP client session object used for the purpose of sending emails to an internet machine. For this purpose, we have to import the smtplib module using the import statement.

 
  1. import smtplib  

The SMTP object is used for the email transfer. The following syntax is used to create the smtplib object.

 
  1. import smtplib   
  2. 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.samtpoint.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 send the mail to the desired machine. The syntax is given below.

 
  1. smtpObj.sendmail(sender, receiver, message)  

Example

 
  1. #!/usr/bin/python3  
  2. import smtplib  
  3. sender_mail = 'sender@fromdomain.com'  
  4. receivers_mail = ['reciever@todomain.com']  
  5. message = """From: From Person %s 
  6. To: To Person %s 
  7. Subject: Sending SMTP e-mail  
  8. This is a test e-mail message. 
  9. """%(sender_mail,receivers_mail)  
  10. try:  
  11.    smtpObj = smtplib.SMTP('localhost')  
  12.    smtpObj.sendmail(sender_mail, receivers_mail, message)  
  13.    print("Successfully sent email")  
  14. except Exception:  
  15.    print("Error: unable to send email")  

Sending email from gmail

There are the cases where the emails are sent using gmail SMTP server. In this case, we can pass gmail as the SMTP server instead of using localhost with the port 587.

Use the following syntax.

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

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

Consider the following example.

Example

 
  1. #!/usr/bin/python3  
  2. import smtplib  
  3. sender_mail = 'sender@gmail.com'  
  4. receivers_mail = ['reciever@gmail.com']  
  5. message = """From: From Person %s 
  6. To: To Person %s 
  7. Subject: Sending SMTP e-mail  
  8. This is a test e-mail message. 
  9. """%(sender_mail,receivers_mail)  
  10. try:  
  11.    password = input('Enter the password');  
  12.    smtpObj = smtplib.SMTP('gmail.com',587)  
  13.    smtpobj.login(sender_mail,password)  
  14.    smtpObj.sendmail(sender_mail, receivers_mail, message)  
  15.    print("Successfully sent email")  
  16. except Exception:  
  17.    print("Error: unable to send email")  

Sending HTML in email

We can format the HTML in the message by specifying the MIME version, content-type, and character set to send the HTML.

Consider the following example.

Example

 
  1. #!/usr/bin/python3  
  2. import smtplib  
  3. sender_mail = 'sender@fromdomain.com'  
  4. receivers_mail = ['reciever@todomain.com']  
  5. message = """From: From Person %s 
  6. To: To Person %s 
  7.  
  8. MIME-Version:1.0 
  9. Content-type:text/html 
  10.  
  11.  
  12. Subject: Sending SMTP e-mail  
  13.  
  14. <h3>Python SMTP</h3> 
  15. <strong>This is a test e-mail message.</strong> 
  16. """%(sender_mail,receivers_mail)  
  17. try:  
  18.    smtpObj = smtplib.SMTP('localhost')  
  19.    smtpObj.sendmail(sender_mail, receivers_mail, message)  
  20.    print("Successfully sent email")  
  21. except Exception:  
  22.    print("Error: unable to send email")