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.
- MIMEText: It consists of simple text. This will be the body of the email.
- MIMEImage: This would allow us to add images to our emails.
- MIMEAudio: If we wish to add audio files, we may do it easily with the help of this subclass.
- MIMEApplication: This can be used to add anything or any other attachments.
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.
- Provide the server address and port number to initiate our SMTP connection
- Then we’ll use smtp.ehlo to send an EHLO (Extended Hello) command.
- Now, we’ll use smtp.starttls to enable transport layer security (TLS) encryption.
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.
- Assign the MIMEMultipart object to the msg variable after initializing it.
- The MIMEText function will be used to attach text.
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:
- First, read the image as binary data.
- Attach the image data to MIMEMultipart using MIMEImage, we add the given filename use os.basename
Python3
img_data
=
open
(one_img,
'rb'
).read()
msg.attach(MIMEImage(img_data,
`` name
=
os.path.basename(one_img)))
Attaching Several Files:
- Read in the attachment using MIMEApplication.
- Then we edit the attached file metadata.
- Finally, add the attachment to our message object.
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.
- Make a list of all the emails you want to send.
- Then, by using the sendmail function, pass parameters such as from where, to where, and the message content.
- At last, just quit the server connection.
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: