In this tutorial we explore how to use various modules in Python to construct and send different type of emails. We will use the Python smtplib library and email modules to send emails to multiple email address.
The smtplib module in Python serves as a valuable resource for establishing connections to SMTP servers and sending emails using the Simple Mail Transfer Protocol, enabling developers to effectively manage email communication with ease.
The email module facilitates the construction, parsing, encoding, and decoding of email messages using the MIME standard, enabling developers to create well-structured emails and automate email processing tasks efficiently.
Steps To Send Basic Email Using Python
Step1: First the smtplib library need to be imported.
import smtplib
Step2: Create a session using the SMTP(Simple Mail Transfer Protocol) connection.
s = smtplib.SMTP('smtp.gmail.com', 587)
In this line the first parameter is the server location and the second parameter is the port address to connect. For Gmail we are using 587.
Step3: For security reason we have to put the SMTP connection is TLS(Transport Layer Security) mode. This will encrypt all the SMTP commands.
s.starttls()
Step4: Next provide your Gmail id and password for authentication.
s.login("gmail_id", "gmail_app_password")
Note: We have to pass the Google Application password here. The usual Gmail password will not work for security reason. Refer this post for how to generate Google App password the easy way.
Next prepare the message that we will add in the message body.
message = "A Basic Python Email"
In the last step we will use the sendmail() function to finally send the mail and quit the connection.
s.sendmail("from_email_address", "to_email_address", message)
s.quit()
Here the first parameter is the email address from where we will send the email. The second parameter is the id to which you will send the mail and the third parameter is the message that will go in the email body. At the end we will close the SMTP connection. You can also put the from and to email id same that is your email address for testing.
Below is the full code to send a basic email using Python.
import smtplib
# creates SMTP session
s = smtplib.SMTP('smtp.gmail.com', 587)
# start TLS for security
s.starttls()
# Authentication
s.login("gmail_address", "gmail_app_password")
# message to be sent
message = "Message_you_need_to_send"
# sending the mail
s.sendmail("from_email_address", "to_email_address", message)
# terminating the session
s.quit()
Send Email With Subject And Multiple Email Address
In the previous example we used only the smtplib python library to send mail. In those mail we don’t have subjects in the mail.
In this session we will use email library to send mails with Subject and to multiple email address. Below is the full code with explanation for the same.
import smtplib
from email.mime.text import MIMEText
subject = "Email Subject"
body = "This is the body of the text message"
sender = "[email protected]" #Sender Email Address
recipients = ["[email protected]", "[email protected]"] # Multiple email address can be given
password = "password" # Gmail Application Password
# We will create a function to send mail .We will pass above values in funcion parameter.
def send_email(subject, body, sender, recipients, password):
msg = MIMEText(body) # Creating msg object using MIMEText class of email module
msg['Subject'] = subject # Assigning the subject
msg['From'] = sender # Assigning the sender email address
msg['To'] = ', '.join(recipients) # Assigning recepients email address.
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp_server: # Creating connection using context manager
smtp_server.login(sender, password)
smtp_server.sendmail(sender, recipients, msg.as_string())
print("Email sent Successfully!")
# We will call the function and pass the parameter values defined in line no 4 to 8.
send_email(subject, body, sender, recipients, password)
In the above function at line no 13 we are creating msg object using the MIMEText class imported above, and setting the desired values for From, To, Subject. This prepares the header of the email.
At line number 5 we are setting the body of the plain email.
At line no. 17 we are creating the connection using the Python context manager. He we don’t have to close the connection explicitly. The SMTP connection will auto close once all the operation inside this completes.
Conclusion:
In this tutorial we learned how to send full fledged plain emails using Python. In the next topic we will see how to send HTML emails using Python. If you have any doubt or facing any issue in any of the codes, kindly comment below.
You may also like how to send email with attachment using Python.