Python send mail to multiple recipients using SMTP server
In this post, you will learn how to write Python code to send email to multiple recipients using the SMTP server.
SMTP (Simple Mail Transfer Protocol) is a TCP/IP protocol of the Internet standard for electronic mail transmission from the sender to one or more recipients. To connect to an SMTP mail server, we need the following details HOSTNAME, USERNAME, PASSWORD and PORT NUMBER.
Python provides the in-built module smtplib to send email using SMTP Server. There is no extra installation required. So first, we need to import this at the top of the script and define a SMTP client session object that can be used to send mail.
import smtplib
Python Connection to SMTP Server
The SMTP object encapsulates a SMTP connection. It has the following syntax-
smtplib.SMTP([host, port, local_hostname, timeout)
host- The host name of your SMTP server. We can specify the IP address of the host or a domain name.
port- The port number where the SMTP server is listening. Generally, this port would be 25.
local_hostname- If your STMP server is running on the local machine, then we can just specify the 'localhost'.
timeout- It is an optional parameter and specifies a timeout in seconds for blocking operations.
SMTP Authentication
STMP Authentication requires login to the SMTP Server. It has the following syntax-
SMTP.login(username, password)
The arguments are the username and password to authenticate with the server.
Start TLS
We should start the TLS for a security reason. It will encrypt all the commands that follow this.
starttls()
Send Mail
The sendmail() function is used to send mail to the recipients. It has the following syntax-
sendmail(from, to, msg)
Here, the from is the sender mail address and the to is the recipient mail address, and the msg contains messages to be sent in the string.
Quit the SMTP connection
At last, we should terminate the SMTP session and close the connection using an SMTP QUIT() command.
Python code to send mail
Here is the complete code in Python to send mail to multiple recipients using the SMTP Mail Server.
#Python code to send mail
import smtplib
try:
# Set sender mail, receivers mail and messages
sender_mail = "This email address is being protected from spambots. You need JavaScript enabled to view it. "
receivers_mail = ['receiver1@domain', 'receiver2@domain']
to = ", ".join(receivers_mail)
message = """Subject: Python testing mail
This mail is sent using Python SMTP."""
# Make SMTP connection
obj = smtplib.SMTP('smtp.gmail.com', 587)
# secure with TLS
obj.starttls()
# Mail Server Authentication
obj.login(USRENAME, PASSWORD)
# sending the mail
obj.sendmail(sender_mail, to, message)
print("Mail sent successfully.")
# terminating the session
obj.quit()
except Exception:
print("Mail delivery failed.")
Related Articles
Python NumPy: Overview and ExamplesConvert Python list to numpy array
numpy dot product
Python Pandas Plotting
Pandas string to datetime
Convert Excel to CSV Python Pandas
Python take screenshot of specific window
Read data from excel file using Python Pandas
How To Find IP Address In Python
Python project ideas for beginners
Python send HTML email with attachment
Read data from excel file using Python Pandas
How to generate QR Code in Python using PyQRCode
Python programs to check Palindrome strings and numbers
CRUD operations in Python using MYSQL Connector
Fibonacci Series Program in Python
Python File Handler - Create, Read, Write, Access, Lock File
Python convert XML to JSON
Python convert xml to dict
Python convert dict to xml