Featured
- Get link
- X
- Other Apps
Sending Emails To Multiple Recipients Using Python SMTP and Email.EmailMessage
We use smtplib module to send emails to one or multiple recipients using Python code.
Before starting the project, let's understand the difference between two protocols: SMTP vs. IMAP.
SMTP stands for Simple Mail Transfer Protocol, IMAP stands for Internet Access Message Protocol. IMAP is for the retrieval of emails and SMTP is for the sending of emails. That means IMAP talks to both the client and server to receive emails, it allows users to access email messages from anywhere. On the other hand, SMTP talks only to servers to send emails.
This project only talks about SMTP for sending emails to recipients.
The smtplib module has a SMTP class, its instance starts an SMTP connection. SMTP stands for Simple Mail Transfer Protocol, it is an application for sending out emails using SMTP server.For connecting to Gmail's smtp server, for example, after a quick Google search of 'Gmail smtp settings', we can see that its smtp server host is 'smtp.gmail.com', its port number is '587' for TSL. These are two arguments we need for define a smtp server. For a list of most of email clients' IMAP or SMTP server, check out this article.
After a successful connection to an SMTP server, we need a valid email address of Gmail and its matching password for a secure login. Gmail requires a TLS secure login,
from smtplib import SMTP from email.message import EmailMessage # FOR GOOGLE EMAIL: google_port = 587 google_fqdn_host = 'smtp.gmail.com' google_smtp_server = 'smtp.gmail.com:587'
# both of below two lines of codes work for creating a smtp server # smtp_server = SMTP(google_fqdn_host, google_port) # smtp_server = SMTP(google_smtp_server) # log into email account with email address and password # password below is retrieved from a private text file. # You can create a text file # to store your own password and retrieve it by # reading the file. My email address # is also stored inside the text file, on its first line. # The second line is password. with open('password.txt', 'r') as file: myemail = file.readline().strip() password = file.readline().strip() # create email message with Python's email module # I stored email content inside a txt file with open('email_content.txt', 'r') as f: # create a text/plain message instance msg = EmailMessage() msg.set_content(f.read()) # headers of EmailMessage msg['Subject'] = f'Read this Test Email' msg['From'] = 'Me' # for sending to multiple addresses, each email # can be separated by a comma ',' msg['To'] = "toaddress@whatevermail.com" # attach image to the file, with open('myimage.jpg', 'rb') as f: img_byte = f.read() # if the image type is unknown, Python libraries such as # 'filetype' for inferring file type and MIME type msg.add_attachment(img_byte, maintype='image', subtype='jpg') # Now since the headers, content and attachment has been added to msg, # we can send email message thru the smtp server # for yahoo and gmail, this is needed before login() call # smtp_server.starttls() # Instead the above line of code, we use 'with' statement. # the SMTP QUIT command is issued automatically. with SMTP(google_smtp_server) as server: server.starttls() # call this before login() server.login(myemail, password) server.send_message(msg) # smtp_server.quit()
This is the simple way of sending emails using Python SMTP and Email libraries.
For source code of this project, go to github
- Get link
- X
- Other Apps
Popular Posts
Python SQLite3 Module Database - Many to Many Relationship Database Example
- Get link
- X
- Other Apps
CSS Staggered Animation - Show Different Elements Animations One After Another with CSS Only
- Get link
- X
- Other Apps