Email works with SMTP(Simple Mail Transfer Protocol) protocol which handles sending e-mails and routing e-mails between mail servers
Python provides smtplib
for sending emails
smtplib module defines an SMTP client session object that can be used to send mail to any Internet machine with an SMTP or ESMTP listener daemon
import smtplib
Check https://emailconfiguration.com/ for more
SMTP object of smtplib
module is used for connection establishment with server
It requires service provider's domain name and its port number as parameters
A smtp connection can be made with following code
connection = smtplib.SMTP('smtp.gmail.com', 587)
connection.starttls()
Following example sends an email using SMTP
# importing smttplib
import smtplib
#import MIMEText to add fields like Subject, etc to the message string
from email.mime.text import MIMEText
# Create a MIMEText object with text message as argument
msg = MIMEText('Text content for email')
sender_email = '[email protected]'
receiver_email = '[email protected]'
msg['Subject'] = 'Sending email from Python'
msg['From'] = sender_email
msg['To'] = receiver_email
# Create SMTP object and call starttls() method for encrypted connection
connection = smtplib.SMTP('smtp.yandex.com', 587)
connection.starttls()
# login() method is used to login into the server which requires email and password as parameters
connection.login(sender_email, 'dummy_password')
# sendmail() is used to send email which requires sender's and receiver's emails and the message(as string) as parameters
connection.sendmail(sender_email, receiver_email, msg.as_string())
connection.quit()
Save the program and run with python (considering filename is nc_email.py)
python nc_email.py
Check if email is received
To attach an attachment(or multiple) in an email, a MIMEMultipart object is used
from email.mime.multipart import MIMEMultipart
msg = MIMEMultipart()
File is read and set as payload of a MIMEBase object, which is encoded to base64 so that it can be sent along with text
from email.mime.base import MIMEBase
from email import encoders
part = MIMEBase('application', 'octet_stream')
part.add_header('Content-Disposition', "attachment; filename=some_file_name")
part.set_payload((open("some_file_name", 'rb')).read())
encoders.encode_base64(part)
It is then attached to the MIMEMultipart object, which can be converted to string to be used in sendmail() function
msg.attach(part)
text = msg.as_string()
Following example code sends an attachment with email
import smtplib
# import encoders to encode the payloads for transport through compliant mail servers
from email import encoders
# Base class for MIME-specific subclasses of Message
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
# subclass of MIMEBase, which is an intermediate base class for MIME messages that are multipart
from email.mime.multipart import MIMEMultipart
sender_email = '[email protected]'
receiver_email = '[email protected]'
subject = 'sending attachment with python'
msg = MIMEMultipart()
msg['From'] = sender_email
msg['To'] = receiver_email
msg['Subject']= subject
body = 'Text content of email'
msg.attach(MIMEText(body, 'plain'))
# file to be added as attachment
# provide absolute path in case file is in different directory than directory of python file
filename = 'wallpaper.jpeg'
attachment = open(filename, 'rb')
# A MIME attachment with the content type "application/octet-stream" is a binary file
part = MIMEBase('application', 'octet_stream')
part.set_payload((attachment).read())
# Base64 encoding schemes are commonly used to encode binary data that is to be transferred over media which deal with textual data
encoders.encode_base64(part)
# add filename to the header
part.add_header('Content-Disposition', "attachment; filename= "+filename)
# attach the attachment to the message
msg.attach(part)
text = msg.as_string()
connection = smtplib.SMTP('smtp.yandex.com', 587)
connection.starttls()
connection.login(sender_email, 'dummy_password')
connection.sendmail(sender_email, receiver_email, text )
connection.quit()