Python Email Example – An Easy Instruction

In our Python email example, we write about creating email using Python. In the modern world people get a lot of letters, and some even have several mailboxes. All this complicates the process of administration, which forces us to seek solutions to the problem. Motherboard editor Michael Byrne wrote a story on how to send emails to different mailboxes using Python. Even if you do not want to create a letter sorting program, managing emails as a set of data is quite interesting. Byrne decided to use Python to solve the problem, since this language is good for easy and intuitive creation of such things, and is suitable even for non-programmers.

The subsequent Python email module example is highly recommended to be read through if you have a similar task to complete. This sample is available for free, but you can’t hand it in as your own assignment. To start, you need to read through the sample and then you will be able to do your own task easily.

Work With Email Using Python

1) Import Python SMTP module.

First, we need a suitable Python module. Smtplib ships with Python, so there is no need to take any additional actions. Just enter this line in console:

import smtplib

2) Connect to email server.

We need to create a smtplib object, which can be considered as a kind of portal, giving access to the connection and various tools to work with it from the smtplib module.

In this case, the function that returns the correct object takes two parameters or arguments. The first argument contains a domain name that is an email address that begins with “smtp” as shown below. The second argument is the port number to which the connection is performed on the mail server. It almost always takes the value 587 in accordance with the standard TLS encryption. Very rare services use port 465.

PORT = 587
session = smtplib.SMTP(SERVER, PORT)

3) Authorization.

In order to login, you only need to write:

user = ''
pwd = 'smtptest2017'
session.login(user, pwd)

4) Send message.

It’s quite simple:

session.sendmail(user, recep, msg + endmsg)

In one program, it looks like this:

from socket import *

msg = "\r\nOK GOOGLE"     # email body

endmsg = "\r\n.\r\n"                    # end of body

mailserver = ''
clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect((mailserver, 25))
recv = clientSocket.recv(1024)

print (recv),

heloCommand = 'HELLO\r\n'


recv1 = clientSocket.recv(1024)
print (recv1)
if recv1[:3] != '250':
    print ('250 reply not received from server.')

#Send MAIL FROM command and print server response.
print ('Send MAIL FROM command and print server response')
clientSocket.send(bytes('MAIL From:\r\n','utf-8'))
recv2 = clientSocket.recv(1024)
print (recv2)

print ('Mail Sent')

import smtplib
PORT = 587
user = ''
pwd = 'smtptest'
recep = ''
print ('Sending letter...')
session = smtplib.SMTP(SERVER, PORT)
session.login(user, pwd)
session.sendmail(user, recep, msg + endmsg)
print ('Successful!')

Leave a Reply

Your email address will not be published. Required fields are marked *

Customer testimonials

Submit your instructions to the experts without charge.