Sending automated emails with attachments can be important for several reasons:
Efficiency: Automated emailing saves time and effort by eliminating the need for manual sending of emails, and it ensures that emails are sent consistently and on time.
Accuracy: Automated emailing reduces the risk of human error, such as typos and mistakes in addresses, attachments, and content.
Consistency: Automated emailing ensures that the same message and attachments are sent to all recipients, eliminating the possibility of variations in the content of the email.
Record-keeping: Automated emailing helps keep records of all emails sent, including the time and date they were sent and the attachments that were included.
Increased productivity: Automated emailing frees up time and allows users to focus on more important tasks, increasing overall productivity.
Improved communication: Automated emailing can improve communication by ensuring that important information and attachments are consistently sent to all relevant parties.
Overall, automated emailing with attachments can improve the efficiency, accuracy, and consistency of communication and increase productivity.
While lots of resources are available online to send mail in python3, in this short article a challenge faced during sending mail will be discussed and a working script will be shared. smtplib library offers to send emails easily, But to send a list of different receivers addresses is sometimes challenging. To solve this we can pass all receiver email addresses as a list.
i.e, receivers = [‘email@example.com’, ‘firstname.lastname@example.org‘]
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
#To include date in subject
today_date = datetime.date.today()
sender = 'email@example.com'
sender_password = "password"
receivers = ['firstname.lastname@example.org','email@example.com','firstname.lastname@example.org']
cc_recipients = 
msg = MIMEMultipart()
msg['From'] = sender
msg['To'] = ", ".join(receivers)
msg['Cc'] = ", ".join(cc_recipients)
msg['Subject'] = "Sub "+str(today_date)
body = """\
<p>Your message. Please find the attached file.</p>
<p><em>This is an automatically generated email – please do not reply to it. If you have any queries regarding the report please email us at <span style="text-decoration: underline; color: #0000ff;">contactmail.</span><br /></em></p>
<p>Best Regards,<br /><strong>abcd</strong> | abcd.</p>
<p style="color: #999999; font-size: 11px;"><em>Your message. </em></p>
<p style="color: #999999; font-size: 11px;"><em>Message</em></p>
filename = 'filename.xlsx' #file located in same directory.
attachmentment = open(filename, "rb")
attachment = MIMEBase('application', 'octet-stream')
# To convert payload into encoded formate
# base64 encription
attachment.add_header('Content-Disposition', "attachmentment; filename= %s" % filename)
# attaching the instance to instance 'msg'
# creating an SMTP session
s = smtplib.SMTP('server_ip_or_address')
#s.set_debuglevel(1) # to turn on debug
text = msg.as_string()
s.sendmail(sender, receivers, text)
The Python script imports several modules that are used to send emails using the Simple Mail Transfer Protocol (SMTP).
- The “smtplib” module provides functions for communicating with an SMTP server to send email.
- The “email.mime.multipart” module provides the MIMEMultipart class, which is used to construct multi-part emails. Multi-part emails can contain text and attachments.
- The “email.mime.text” module provides the MIMEText class, which is used to construct plain text emails.
- The “email.mime.base” module provides the MIMEBase class, which is used as a base class for creating MIME (Multipurpose Internet Mail Extensions) messages.
- The “email” module provides the “encoders” module, which is used to encode the attachments in a format suitable for sending via email.
These modules are used together to create and send an email with text and attachments, using the SMTP protocol. The script sets up the email, attaches the necessary files, and sends the email using an SMTP server.
This Python program uses the
smtplib library to send an email with an attachment using Simple Mail Transfer Protocol (SMTP).
The program starts by importing the necessary libraries such as
datetime. It then sets up the sender’s email address and password as well as the recipient’s email addresses and the email subject with today’s date.
The message body is created in HTML format and is attached to the email as
MIMEText. The attachment is read from the local directory and attached to the email as
MIMEBase. The payload is converted into encoded form and encoded using base64.
The SMTP session is created using
smtplib.SMTP with the SMTP server’s IP address or hostname. The connection is secured using
starttls() and the sender’s email address and password are authenticated using
login(). Finally, the email is sent using
s.sendmail() and the SMTP session is closed using
Note that the program is customizable, so you can modify the sender, receiver, subject, attachment, and message body according to your needs.