Site icon DataFlair

Python Send Email Via SMTP | SMTP Server

Python Send Email

Python send Email via SMTP

Python course with 57 real-time projects - Learn Python

1. Objective

In this tutorial of Python Programming Language, we will learn how Python send Email via SMTP Server. Moreover, we will look at Python SMTP Server. Along with this, we will also discuss the working of SMTP server for sending a mail in Python. Also, we will look at Python send Email example. We will use the Python module smtplib for this. At last, we are going to discuss how Python send HTML Email using SMTP.
So, let’s discuss the process of sending mail in Python via SMTP.

Sending Mail with Python 3 via SMTP

2. What is SMTP Server?

Simple Mail Transfer Protocol is an application layer protocol in the OSI model. It lets a user send mail to another. Since this is a push protocol, we can use it to send a mail. At the receiver, this mail is retrieved using protocols POP (Post Office Protocol) and IMAP (Internet Message Access Protocol).

Python Send Email- SMTP Server

We need to start a server that is always listening for a request. As a client, we open a TCP connection to this server and then send the mail. When the server listens for a TCP connection from a client, it initiates a connection on port 587.
Have a look at Python Packages Comprehensive Guide

3. Python Send Email Using smtplib Module

We’ll use the smtplib module for sending Python Email. This is our Python SMTP script to send mail:

>>> import smtplib
>>> sender='thesender@gmail.com'
>>> receiver='whicheverreceiver@gmail.com'
>>> password=’<put your password here>'
>>> smtpserver=smtplib.SMTP("smtp.gmail.com",587)
>>> smtpserver.ehlo()
>>> smtpserver.starttls()
>>> smtpserver.ehlo
>>> smtpserver.login(sender,password)
>>> msg='Subject:Demo\nThis is a demo
>>> smtpserver.sendmail(sender,receiver,msg)
>>> print('Sent')
>>> smtpserver.close()

Save this as a .py script, and run this to find this output:

>>>

RESTART:

C:/Users/lifei/AppData/Local/Programs/Python/Python36-32/demomail.py Sent

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 Send Email using smtplib Module

4. How Does SMTP Work?

First, we import smtplib.

>>> import smtplib

Then, we set three strings- the sender’s and receiver’s email addresses and the sender’s password. Put your password for your mail ID in the string for a password.

>>> sender='thesender@gmail.com'
>>> receiver='whicheverreceiver@gmail.com'
>>> password=’<put your password here>'

Then, we create a server object using smptlib.SMTP(). We use the port 587 here, and the domain smtp.gmail.com.

>>> smtpserver=smtplib.SMTP("smtp.gmail.com",587)

On this, we call the ehlo() method for Extended Hello. This lets the server identify the client, and also tells it that it must use the ESMTP (Extended SMTP) Protocol.
Read Python Datetime Module with Quick Examples

>>> smtpserver.ehlo()

Then, we call starttls() to use a transport layer security connection.

>>> smtpserver.starttls()

And then we call ehlo() again.

>>> smtpserver.ehlo

Then, we call the login() method on this server object. To this, we pass the parameters sender and password to let the client gain access from the Gmail server to send a mail.

>>> smtpserver.login(sender,password)

Then, we decide the message to send. To set the subject for the message, we do:

>>> msg='Subject:Demo\nThis is a demo'

Now, we call the method sendmail() to send the mail in Python. To this, we pass sender, receiver, and msg.

>>> smtpserver.sendmail(sender,receiver,msg)

Finally, we print a success receipt to the client and then exit.

>>> print('Sent')
>>> smtpserver.close()

Also, if you type in the incorrect password, you will come across this exception:
Explore Python Variable Scope – Local, Global, Built-in, Enclosed

>>>

RESTART:

C:/Users/lifei/AppData/Local/Programs/Python/Python36-32/demomail.py Traceback (most recent call last): File “C:/Users/lifei/AppData/Local/Programs/Python/Python36-32/demomail.py”, line 10, in <module> smtpserver.login(sender,password) File

“C:\Users\lifei\AppData\Local\Programs\Python\Python36-32\lib\smtplib.py”, line 729, in login raise last_exception File “C:\Users\lifei\AppData\Local\Programs\Python\Python36-32\lib\smtplib.py”, line 720, in login initial_response_ok=initial_response_ok) File

“C:\Users\lifei\AppData\Local\Programs\Python\Python36-32\lib\smtplib.py”, line 641, in auth raise SMTPAuthenticationError(code, resp) smtplib.SMTPAuthenticationError: (535, b’5.7.8 Username and Password not accepted. Learn more at\n5.7.8 https://support.google.com/mail/?p=BadCredentials z62sm38593861pff.57 – gsmt

5. How to Send HTML Email in Python?

For sending an HTML Email you can also configure your message to be able to embed simple HTML code in it.
Import smtplib

>>> sender='thesender@gmail.com'
>>> receiver=’whicheverreceiver@gmail.com'
>>> password='<your password>'
>>> smtpserver=smtplib.SMTP("smtp.gmail.com",587)
>>> smtpserver.ehlo()
>>> smtpserver.starttls()
>>> smtpserver.ehlo
>>> smtpserver.login(sender,password)
>>> msg="""From: Ayushi
To: Ruchi
MIME-Version: 1.0
Content-type: text/html
Subject:Demo
This is a demo<br/><p align="center">Hi</p><hr/>"""
>>> smtpserver.sendmail(sender,receiver,msg)
>>> print('Sent')
>>> smtpserver.close()

So, we receive this email in the receiving inbox.

SMTP- Sending a HTML E-mail

So, this was all about Python send Email using SMTP server. Hope you like our explanation.
Learn Python Closure – Nested Functions and Nonlocal Variables

6. Conclusion

Hence, in this Python Send Email tutorial, we learn how can we send an email using Python with SMTP module. Hence, now we have an understanding of SMTP server in Python. Moreover, we discussed the process of sending a mail using smtplib module. Finally, we saw how to send an HTML email. Furthermore, if you have any query, feel free to ask in the comment section.
For reference

Exit mobile version