Python Sending Email using SMTP

Python Programming Training Certification

Flexible Hours

100 Assignments

Instructor Led online Training

50 LMS Access

24X7 Support

100% Skill Level

Enquire Now

4.9 out of 1000+ Ratings
Best Python Institute for Learning Python Course & Training, Live Project Training in Python with Django, Data Science and AI, Interview Assistance, Expert Coaching Trainers. Python Certification & Interview Assistance! Get free demo now!

Course Overview

Python is one of the world’s top programming languages used today and Python training has become the most popular training across individuals. Training Basket’s Python Training & Certification course covers basic and advanced Python concepts and how to apply them in real-world applications.Python is a flexible and powerful open-source language that is easy to learn and consists of powerful libraries for data analysis and manipulation. Our Python training course content is curated by experts as per the standard Industry curriculum. The curriculum, coding challenges and real-life problems cover data operations in Python, strings, conditional statements, error handling, shell scripting, web scraping and the commonly used Python web framework Django. Take this Python training and certification course and become job-ready now.

Python Sending Email using SMTP

Simple 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. It is an application layer protocol which allows to users to send mail to another. The receiver retrieves email using the protocols POP(Post Office Protocol) and IMAP(Internet Message Access Protocol).

Python Sending Email using SMTP

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

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

  • $ import smtplib

The SMTP object is used for the email 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 send the mail to the desired machine. The syntax is given below.

  • smtpObj.sendmail(sender, receiver, message)

Example

  • #!/usr/bin/python3
    import smtplib
    sender_mail = ‘[email protected]
    receivers_mail = [‘[email protected]’]
    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 using the Gmail SMTP server. In this case, we can pass Gmail as the SMTP server instead 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 using Gmail user name and password. For this purpose, the smtplib provide the login() method, which accepts the username and password of the sender.

This may make your Gmail ask you for access to less secure apps if you’re using Gmail. You will need to turn this ON temporarily for this to work.

Python Sending Email using SMTP

Consider the following example.

Example

  • #!/usr/bin/python3
    import smtplib
    sender_mail = ‘[email protected]
    receivers_mail = [‘[email protected]’]
    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 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

  • #!/usr/bin/python3
    import smtplib
    sender_mail = ‘[email protected]
    receivers_mail = [‘[email protected]’]
    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”)

Recently Trained Students

Jessica Biel

– Infosys

My instructor had sound Knowledge and used to puts a lot of effort that made the course as simple and easy as possible. I was aiming for with the help of the ZebLearn Online training imparted to me by this organization.

Richard Harris

– ITC

I got my training from Zeblearn in the Python Certification Training, I would like to say that say he is one of the best trainers. He has not even trained me but also motivated me to explore more and the way he executed the project, in the end, was mind-blowing.


Candidate’s Journey During Our Training Program

Card image cap

Expert’s Advice & Selection of Module

Choosing the right type of module for the training is half the battle & Our Team of experts will help & guide you.

Card image cap

Get Trained

Get Trained & Learn End to End Implementation from our Expert Trainer who are working on the same domain.

Card image cap

Work on Projects

We Do make our student’s work on multiple case studies , scenario based tasks & projects in order to provide real-time exposure to them.

Card image cap

Placements

We have a dedicated placement cell in order to provide placement assistance & relevant interviews to our candididates till selection

Placement Partner

×

Leave your details

×

Download Course Content




wop;[\]