Videos
Check out our tutorial video series.
Send emails in Python using SMTP mailer with this tutorial. Integrate email functionality into web apps and experiment on a test server.
To extend the functionality of any web application, it's essential to integrate the sending of notifications and emails. For such cases, Python has a built-in smtplib module for sending emails using Simple Mail Transfer Protocol (SMTP). In this tutorial, we'll discuss all the necessary steps to send emails through an SMTP server using Python programming language. We will also learn about the different kinds of sending emails, from emails with images and attachments to multiple emails at once.
The standard Python library includes an SMTP module for sending emails. There is no need for additional settings or procedures. The module can be imported using the following statement:
Type the following into an interactive Python process to make sure that the module has been successfully imported and to get a complete description of its classes and arguments:
To avoid spamming your mail and customers, it is important to experiment on a test server when creating a new application or adding features for the first time. That way, your domain will not be on the block due to spam.
You may test email functionality using Python's pre-installed smtpd module. It discards emails and prints their content to the console instead of sending them. Running a local debugging server eliminates the need to encrypt messages or log into an email server.
In Command Prompt, type:
To run the SMTP server on port 25, you need access to root on Linux:
It helps you test your code and identify any flaws. However, you won't be able to check your HTML email template.
In most cases, you need to add some formatting, links, or images to your email notifications. We can simply put all of these with the HTML content. For this purpose, Python has an email package.
We will deal with the MIME message type, which is able to combine HTML and plain text. In Python, it is handled by email.mime module.
It is better to write text and HTML versions separately and then merge them with the MIMEMultipart("alternative") instance. It means that such a message has two rendering options accordingly. In case an HTML isn't rendered successfully for some reason, a text version will still be available.
Let's learn how to attach various files to emails using Python. Python allows us to attach text, images, audio files, and applications. Attachments are MIME objects but must be base64 encoded. Use email.mime.audio.MIMEAudio or email.mime.image.MIMEImage to attach a file. Remember that you should not send files larger than 20 MB, although it is possible.
Receipts, tickets, boarding passes, order confirmations, etc., are usually sent in PDF format.
Let's see how to send boarding passes in PDF format:
To attach multiple files, you can use the message.attach() method several times.
Images are attachments, even if they are included in the message body. They come in three different varieties: linked images, base64 images, and CID attachments, which are all embedded as MIME objects. In this section, we'" cover their quirks, advantages and disadvantages, and compatibility with most email clients.
To add a CID attachment, we will create a multi-part MIME message with the MIMEImage component:
The CID picture is shown as an attachment and a component of an HTML message. However, many email applications, most notably Gmail, don't often display CID pictures since messages containing this image format are frequently seen as spam.
So let's look at how to attach a base64-encoded image.
The image is now embedded in the HTML message and is not available as an attachment and will not show up as spam.
The advantage of Python email is that you can send multiple emails to different recipients and personalize each one. Just insert the addresses of additional recipients, separating them with commas, and add CCs and BCCs. But Python loops will be handy if you're sending out bulk mail. One method is to create a database in.csv format and save it in the same directory as your Python script.
Let's create a simplest table with only two columns—name and email address—from the list:
The code below will open the file, loop through its rows line by line, and replace the value of the "name" column with the "name" placeholder.
We receive the following outcome after executing the script:
Set up your business server when you're ready to send emails to actual recipients. Your localhost or any external SMTP relies on your demands, objectives, and tastes. Let's look deeper at Gmail, one of the most well-liked choices. To use the Gmail server, you'll need to know:
You can use Yagmail, a specialized Gmail/SMTP, if you want something simple. Just compare the above examples with these few lines of code:
Or using Yagmail:
So in this tutorial, we have demonstrated the essential alternatives for sending emails using Python. Studying Python documentation and just playing with your code are two things we advise you to do to get great results.
Check out our tutorial video series.
Email and SMS guides for automation and testing.
View github project code for multiple languages.
Latest posts from the MailSlurp team.
Test, build, and automate messaging with a free MailSlurp account.