Using Python3 and SMTP to Send Attachments to Multiple Email Recipients, Including Gmail

Featured Image
Hasan-Uz-Zaman Ashik

Written by Hasan-Uz-Zaman

September 7, 2021

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 = [‘receiver1@domain.com’, ‘receiver2@domain.com‘]

 

 

The Python script imports several modules that are used to send emails using the Simple Mail Transfer Protocol (SMTP).

  1. The “smtplib” module provides functions for communicating with an SMTP server to send email.
  2. 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.
  3. The “email.mime.text” module provides the MIMEText class, which is used to construct plain text emails.
  4. The “email.mime.base” module provides the MIMEBase class, which is used as a base class for creating MIME (Multipurpose Internet Mail Extensions) messages.
  5. 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 smtplib, MIMEMultipart, MIMEText, MIMEBase, encoders, and 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 s.quit().

Note that the program is customizable, so you can modify the sender, receiver, subject, attachment, and message body according to your needs.

You May Also Like…

Hasan-Uz-Zaman

Hasan-Uz-Zaman

Network Engineer

Zaman is an aspiring Technical Writer and passionate about software-defined networking (SDN), Network Automation, Ansible, Log data management (Syslog-ng), Python tools, Web Application development (django) etc.

Let's start talking!

0 Comments

Submit a Comment

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