Sending Mail using Python || Using SMTP Module

 Sending Mail Using SMTP



SMTP is a Simple Mail Transfer Protocol that is responsible for sending emails and routing emails between Mailing Servers. It's an application layer protocol that allows users to transfer mail to another user.

Python provides smtplib module to define a SMTP Client session object to send mails to any Internet Machine. 

Module Installation:

pip install smtplib

import smtplib
smtpObj = smtplib.SMTP(host, port = 25, local_hostName)

Required Parameters: 

  • host = It is the hostname on which the SMTP is running, we can also specify the IP address of the server or Domain Name.
  • port = Port number on which the host machine is listening to the SMTP connections. The default is 25.
  • local_hostName = If SMTP is running on your local Machine we can assign the localhost as the option.
Once the SMTP() is ready with the required parameters we will move to the next step i.e. send mail to a particular destination through sendmail(). Following are the parameter:

smtpObj.sendmail(sender, receiver, message)

  • sender = senders email address <str-type>
  • receiver = receiver's email address <str-type>
  • message = body of mail <str-type>

And once the sender, receiver & message/body of mail has been decided then it's time to handle the exception that can be thrown during code execution like No Internet Connection, Wrong Sender/Receiver mail address. For handling such errors we use "try-catch block" with parent Exception class SMTPException

In any condition you don't have any of this "host", "port", "local hostName" and you are a learner then you use only 'localhost' as the parameter for SMTP('localhost')  and it will work completely fine.

import smtplib

message = """Greetings from Prathmesh Chaudhari"""
try:
    smtpObj = smtplib.SMTP("localhost")
    smtpObj.sendmail('sender@domain.com','receiver@domain.com',message)
    print("MAIL SEND SUCCESSFUL")
except SMTPException:
    print("MAIL SEND UNSUCCESSFUL")

Using Gmail Server Host:

And if you want to send mail using GMAIL SMTP so instead of SMTP('smtp.gmail.com',587), here '587' is the local port number for Gmail SMTP either you can send mail using yahoo so SMTP('smtp.mail.yahoo.com',465) here '465' is the port number for Yahoo.

import smtplib

message = """Subject: Follow to prathmesh_chaudhari_05 This is Test Mail through SMTP using
Python. <b>Follow to @prathmesh_chaudhari_05</b>"""
try:
    smtpObj = smtplib.SMTP('smtp.gmail.com', 587)
    smtpObj.starttls()
    smtpObj.login('email_address',email_password)
    smtpObj.sendmail('from@doamin.com','prathmeshchaudhari05@gmail.com',message)
    smtpObj.close()

For using Gmail SMTP service we need to login using login() with required parameters as "your_email_Adddress" & "your_email_password". Once all your work is done don't forget to call close() to logout from the session

Output for above code:






 Having any problem regarding programming related stuff !!! Feel free to dm me on my Social Media Handle Instagram


Check my Portfolioprathmeshchaudhari.me

Subscribe to my Blog & comment below !!!




Comments