How to Send Automated Email Messages in Python (original) (raw)

Last Updated : 31 Aug, 2021

In this article, we are going to see how to send automated email messages which involve delivering text messages, essential photos, and important files, among other things. in Python.

We’ll be using two libraries for this: email, and smtplib, as well as the MIMEMultipart object. This object has multiple subclasses; these subclasses will be used to build our email message.

Step-by-step Implementation

Step 1: Import the following modules

Python3

from email.mime.text import MIMEText

from email.mime.image import MIMEImage

from email.mime.application import MIMEApplication

from email.mime.multipart import MIMEMultipart

import smtplib

import os

Step 2: Let’s set up a connection to our email server.

Python3

smtp = smtplib.SMTP( 'smtp.gmail.com' , 587 )

smtp.ehlo()

smtp.starttls()

smtp.login( 'YourMail@gmail.com' , 'Your Password' )

Step 3: Now, built the message content.

Python3

msg = MIMEMultipart()

msg[ 'Subject' ] = subject

msg.attach(MIMEText(text))

Step 4: Let’s look at how to attach pictures and multiple attachments.

Attaching Images:

Python3

img_data = open (one_img, 'rb' ).read()

msg.attach(MIMEImage(img_data,

`` name = os.path.basename(one_img)))

Attaching Several Files:

Python3

with open (one_attachment, 'rb' ) as f:

`` file = MIMEApplication(

`` f.read(), name = os.path.basename(one_attachment)

`` )

`` file [ 'Content-Disposition' ] = f'attachment; \

`` filename = "{os.path.basename(one_attachment)}" '

`` msg.attach( file )

Step 5: The last step is to send the email.

Python3

to = [ "klm@gmail.com" , "xyz@gmail.com" , "abc@gmail.com" ]

smtp.sendmail(from_addr = "Your Login Email" ,

`` to_addrs = to, msg = msg.as_string())

smtp.quit()

Below is the full implementation:

Python3

from email.mime.text import MIMEText

from email.mime.image import MIMEImage

from email.mime.application import MIMEApplication

from email.mime.multipart import MIMEMultipart

import smtplib

import os

smtp = smtplib.SMTP( 'smtp.gmail.com' , 587 )

smtp.ehlo()

smtp.starttls()

smtp.login( 'Your Email' , 'Your Password' )

def message(subject = "Python Notification" ,

`` text = "", img = None ,

`` attachment = None ):

`` msg = MIMEMultipart()

`` msg[ 'Subject' ] = subject

`` msg.attach(MIMEText(text))

`` if img is not None :

`` if type (img) is not list :

`` img = [img]

`` for one_img in img:

`` img_data = open (one_img, 'rb' ).read()

`` msg.attach(MIMEImage(img_data,

`` name = os.path.basename(one_img)))

`` if attachment is not None :

`` if type (attachment) is not list :

`` attachment = [attachment]

`` for one_attachment in attachment:

`` with open (one_attachment, 'rb' ) as f:

`` file = MIMEApplication(

`` f.read(),

`` name = os.path.basename(one_attachment)

`` )

`` file [ 'Content-Disposition' ] = f'attachment;\

`` filename = "{os.path.basename(one_attachment)}" '

`` msg.attach( file )

`` return msg

msg = message( "Good!" , "Hi there!" ,

`` r "C:\Users\Dell\Downloads\Garbage\Cartoon.jpg" ,

`` r "C:\Users\Dell\Desktop\slack.py" )

to = [ "ABC@gmail.com" ,

`` "XYZ@gmail.com" , "insaaf@gmail.com" ]

smtp.sendmail(from_addr = "hello@gmail.com" ,

`` to_addrs = to, msg = msg.as_string())

smtp.quit()

Output:

Schedule Email Messages

For scheduling the mail, we will make use of the schedule package in python. It is very lightweight and easy to use.

Install the module

pip install schedule

Now look at the different functions that are defined in a schedule module and their use:

The below function will call the function mail every 2 seconds.

schedule.every(2).seconds.do(mail)

This will call the function mail every 10 minutes.

schedule.every(10).minutes.do(mail)

This will call the function in every hour.

schedule.every().hour.do(mail)

Calling every day at 10:30 AM.

schedule.every().day.at("10:30").do(mail)

Calling a particular day.

schedule.every().monday.do(mail)

Below is the implementation:

Python3

import schedule

import time

from email.mime.text import MIMEText

from email.mime.image import MIMEImage

from email.mime.application import MIMEApplication

from email.mime.multipart import MIMEMultipart

import smtplib

import os

def message(subject = "Python Notification" ,

`` text = "", img = None , attachment = None ):

`` msg = MIMEMultipart()

`` msg[ 'Subject' ] = subject

`` msg.attach(MIMEText(text))

`` if img is not None :

`` if type (img) is not list :

`` img = [img]

`` for one_img in img:

`` img_data = open (one_img, 'rb' ).read()

`` msg.attach(MIMEImage(img_data,

`` name = os.path.basename(one_img)))

`` if attachment is not None :

`` if type (attachment) is not list :

`` attachment = [attachment]

`` for one_attachment in attachment:

`` with open (one_attachment, 'rb' ) as f:

`` file = MIMEApplication(

`` f.read(),

`` name = os.path.basename(one_attachment)

`` )

`` file [ 'Content-Disposition' ] = f'attachment;\

`` filename = "{os.path.basename(one_attachment)}" '

`` msg.attach( file )

`` return msg

def mail():

`` smtp = smtplib.SMTP( 'smtp.gmail.com' , 587 )

`` smtp.ehlo()

`` smtp.starttls()

`` smtp.login( 'Email' , 'Password' )

`` msg = message( "Good!" , "Hi there!" ,

`` r "C:\Users\Dell\Downloads\Garbage\Cartoon.jpg" ,

`` r "C:\Users\Dell\Desktop\slack.py" )

`` to = [ "ABC@gmail.com" ,

`` "XYZ@gmail.com" , "insaaf@gmail.com" ]

`` smtp.sendmail(from_addr = "hello@gmail.com" ,

`` to_addrs = to, msg = msg.as_string())

`` smtp.quit()

schedule.every( 2 ).seconds.do(mail)

schedule.every( 10 ).minutes.do(mail)

schedule.every().hour.do(mail)

schedule.every().day.at( "10:30" ).do(mail)

schedule.every( 5 ).to( 10 ).minutes.do(mail)

schedule.every().monday.do(mail)

schedule.every().wednesday.at( "13:15" ).do(mail)

schedule.every().minute.at( ":17" ).do(mail)

while True :

`` schedule.run_pending()

`` time.sleep( 1 )

Output:

Similar Reads

Projects for Beginners
























Projects for Intermediate


















Web Scraping












Automating boring Stuff Using Python

















Tkinter Projects



































Turtle Projects












OpenCV Projects

































Python Django Projects














Python Text to Speech and Vice-Versa