Coders Packet

Sending Email using SMTP in Python

By Rachit R Jindal

This emailing system using Python can be used in an alert system like an e-commerce price alert system, etc. It uses the smtplib, email, imghdr, and os Python modules.

This code packet is an automated e-mailing system made in Python that involves the use of smptlib, os, imghdr, and email modules.

import smtplib
import os
from email.message import EmailMessage
import imghdr

Smtplib is used for sending email and establishing the server between the packet and the delivery system, email module provides structure to email, and all other modules os, imghdr contribute to the file attachment by extracting the data from the described file.

# Enter the Username and Password of sender
user = str(input("Enter the senders e-mail address: "))
password = str(input("Enter the senders password: "))


# Email function for building email structure
msg = EmailMessage()


# Enter the receivers address
msg['To'] = str(input("Enter the receivers e-mail address: "))
# Enter the sensers address
msg['FROM'] = user
# Enter the subject and bondy for email
msg['SUBJECT'] = str(input("Enter the subject of e-mail: "))
msg.set_content(str(input("Enter the content of e-mail: ")))

The above code takes the sender's email address and password for login in. The 'msg' variable creates the structure of email using the EmailMessage() function of the email module. The 'msg' variable takes all the general input that an email takes like the to, from, subject, body(content), and file attachment(if any). This is all stored just like a dictionary(but it isn't a dictionary).

Attaching file in an email:

# Adding attachment text file
file_path = str(input("Enter the path of text file to send: "))

with open(file_path,'rb') as f:
    data = f.read()
    file_name = f.name

msg.add_attachment(data,maintype='file',subtype='txt',filename=file_name)


# Adding attachment pdf file
file_path = str(input("Enter the path of pdf file to send: "))

with open(file_path,'rb') as f:
    data = f.read()
    file_name = f.name

msg.add_attachment(data,maintype='file',subtype='pdf',filename=file_name)


# Adding attachment image file
file_path = str(input("Enter the path of image file to send: "))

with open(file_path,'rb') as f:
    data = f.read()
    file_type = imghdr.what(f.name)
    file_name = f.name

msg.add_attachment(data,maintype='image',subtype=file_type,filename=file_name)

The above code attaches the required file with the email to be sent. It takes the path of the file as input and reads the file into the 'data' variable and extracts the file name, and type that is used as attachment information. Imghdr is used for the image file to extract the type of file or file format. The files are attached using the 'add_attachment' function of the email module. After putting the required text to data in the email, now comes the part of sending an email by establishing the server which is done using the smtplib module.

Sending mail using smtplib module:

# Declaring the server and port (This server serves for gmail e-mail addresses)
smtp = smtplib.SMTP("smtp.gmail.com",587)
# Starting the server and logining in
smtp.starttls()
smtp.login(user,password)
# Sending message
smtp.send_message(msg)

Now, a server is created between the python code server and the mailing address server using smtplib. You can also change the address from 'gmail.com' to the required mailing service. The 'starttls()' starts the execution of the server and the' login()' function tries to log in to the senders account for sending the mail. The message/mail is sent using 'send_message()' function. 

For sending an email using this packet you first need to allow access to less secure apps to log in to the account. Once the email is sent the server automatically quits from the port described ('587' in the above case).

Download project

Reviews Report

Submitted by Rachit R Jindal (rachit99)

Download packets of source code on Coders Packet