All rights are reserved by Niteesh Kumar.. Theme images by Storman. Powered by Blogger.


Total Pageviews

Blog Archive

Follow by Email


Tuesday, 10 July 2018

learn sending emails in python

If you are a working professional and your job is to send a lot of emails to the client or customers on daily basis. Then this will be a very hectic task for you as compose mail for each person in the list.

Python includes several modules in the standard library for working with emails and email servers. As a learning exercise, I recently worked with Python 3 to see how I could use python for sending emails using smtp servers. Here I am writing a tutorial for the same.

Before starting there are few prerequisites you must have -
  •  You must have python installed in your system.

  • Working Internet

Python has various libraries to work with emails. We will use smtplib.

SMTP stands for Simple Mail Transfer Protocol.
smtplib module defines an SMTP client session object and we will use this object to send emails in python to any internet machine with SMTP or ESMTP listener.

Sending emails in python is done using smtplib module using SMTP server.

Actual usage varies depending upon the complexity of email as for detailed emails we also need to use email module. The tutorial given below is for sending emails in python through Gmail.

Basic Script to send emails in Python:

First of all, you need to create an SMTP object and each object you create will be used for connection with one server

import smtplib
server = smtplib.SMTP('', 587)

Next, we need to login to the server - 

server.login("youremailusername", "password")

And now send the email - 

msg = "\nHello!" # The /n separates the message from the headers (which we ignore for this example)
server.sendmail("", "", msg)

The script written above is very basic without header, subject etc. We will use email module to make it more advanced.

email module overview:
Python's email package contains many classes and functions for composing and parsing email messages. We will cover only a small section that is used to send emails.

Use of email package:
We can directly import email module but if we do this we need to write full module name. So we only import classes that are needed.

from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

After importing the required classes we will compose some of the basic headers.

fromaddr = ""
toaddr = ""
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "Python email"

now we will attach the body of the message to MIME(multipurpose internal mail extension) message.

body = "sending test emails using python"
msg.attach(MIMEText(body, 'plain'))

for sending the email we need to convert the message to string and then use the same procedure as above to send email using the SMTP server.

import smtplib
server = smtplib.SMTP('', 587)
server.login("youremailusername", "password")
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)

Writing a function to send emails using Gmail in Python:
We will write a function which can return a dictionary of any addresses it could not forward to and other connetion problems raise errors.

import smtplib
def sendemail(from_addr, to_addr_list, cc_addr_list,
              subject, message,
              login, password,
    header  = 'From: %s\n' % from_addr
    header += 'To: %s\n' % ','.join(to_addr_list)
    header += 'Cc: %s\n' % ','.join(cc_addr_list)
    header += 'Subject: %s\n\n' % subject
    message = header + message
    server = smtplib.SMTP(smtpserver)
    problems = server.sendmail(from_addr, to_addr_list, message)
    return problems

Call the above function to send emails:

sendemail(from_addr    = '', 
          to_addr_list = [''],
          cc_addr_list = [''], 
          subject      = 'Howdy', 
          message      = 'Howdy from a python function', 
          login        = 'pythonuser', 
          password     = 'XXXXX')

0 on: "learn sending emails in python"