Python is the language that can do it all - including SMTP email! Using the in-built
smtplib library we can setup outbound email sending in Python to external SMTP servers. Let us demonstrate.
What is SMTP and how does it work?
Firstly, a quick overview of SMTP and how emails work. SMTP (simple mail transfer protocol) describe the sending and receiving of email over TCP - typically on the ports 25, 2525, 465 or 587. Each email address (such as @gmail.com) has an underlying SMTP server that runs on the domain (smtp.gmail.com for instance) and listens for incoming emails. When we wish to email an address provided by the server we can connect via SMTP in Python and submit a message.
To use Python to send SMTP emails we first need to import and configure the batteries included
smtplib library. In a new python file add:
from smtplib import SMTP
Once imported you can open a new connection like so:
with SMTP(host='smtp.gmail.com', port=465) as smtp: smtp.sendmail(firstname.lastname@example.org', to_addrs=['email@example.com'], msg='Your message') smtp.quit()
For each email you wish to send you will need to connect to the underlying SMTP server or use your own SMTP server to pass the emails on for you. When using your own SMTP server you will want to create authentication so that only you can send from it.
Say you have created an SMTP server with MailSlurp, you can access the server using your username and password for the account hosted on a domain and port:
In code you can use the MailSlurp SDK to obtain these details with the
# get smtp imap access smtp_access = inbox_controller.get_imap_smtp_access(inbox_id=inbox1.id) msg = "Subject: Test subject\r\n\r\nThis is the body" with SMTP(host=smtp_access.smtp_server_host, port=smtp_access.smtp_server_port) as smtp: smtp.login(user=smtp_access.smtp_username, password=smtp_access.smtp_password) smtp.sendmail(from_addr=inbox1.email_address, to_addrs=[inbox2.email_address], msg=msg) smtp.quit()
SMTP servers provide several authentication methods but
PLAIN are two common methods.
Sending email content
To send SMTP emails in Python you must construct and email body. The body of an email begins with lines of headers that are key value pairs and then a body separated by a new line. For instance:
msg = "Subject: Test subject\r\n\r\nThis is the body".
Testing emails with MailSlurp
To test if your Python application is sending emails correctly or to run your own SMTP server try MailSlurp. MailSlurp provides unlimited disposable (or permanent) email mailboxes that can be connected to using Python and SMTP to send emails. Let us use MailSlurp and Pytest to demonstrate:
import os # get an api key at https://app.mailslurp.com/sign-up import mailslurp_client from smtplib import SMTP from mailslurp_client import CreateInboxDto api_key = os.environ.get('API_KEY') assert api_key is not None # create a mailslurp configuration configuration = mailslurp_client.Configuration() configuration.api_key['x-api-key'] = api_key class Test_MailSlurp_SDK: """ Can send email with SMTP """ def test_can_send_with_smtp(self): with mailslurp_client.ApiClient(configuration) as api_client: inbox_controller = mailslurp_client.InboxControllerApi(api_client) inbox1 = inbox_controller.create_inbox_with_options(CreateInboxDto(inbox_type="SMTP_INBOX")) inbox2 = inbox_controller.create_inbox() assert "@mailslurp.mx" in inbox1.email_address # get smtp imap access smtp_access = inbox_controller.get_imap_smtp_access(inbox_id=inbox1.id) msg = "Subject: Test subject\r\n\r\nThis is the body" with SMTP(host=smtp_access.smtp_server_host, port=smtp_access.smtp_server_port) as smtp: smtp.login(user=smtp_access.smtp_username, password=smtp_access.smtp_password) smtp.sendmail(from_addr=inbox1.email_address, to_addrs=[inbox2.email_address], msg=msg) smtp.quit() wait_for_controller = mailslurp_client.WaitForControllerApi(api_client) email = wait_for_controller.wait_for_latest_email(inbox_id=inbox2.id) assert "Test subject" in email.subject
Test emails safely with no risk of spamming users with real emails
MailSlurp Java SDK for sending and receive email and attachments on the JVM.
Send and receive email in Python, create test email accounts and more with the official PyPI MailSlurp package.
How to send email in CSharp, PHP, Ruby, Java, Python, and NodeJS using the SMTP protocol and MailSlurp mailservers.
1111 SMTP Connections failing
Conda is a package management system that is popular in the machine learning and python space.
Send emails using SMTP mailer in Python. Detailed tutorial for sending mail in Python.
JS SMTP email sending guide using HTML and NodeJS.
PHPMailer tutorial (Sending email with PHP)
Use Send-MailMessage in Windows Powershell to send emails using an SMTP server or MailSlurp's free email API.
RLang tutorial for sending emails over SMTP.
A full list of commands and response status codes for SMTP mail servers when sending emails.
How to use Telnet to test the SMTP servers?
Amazon SES is an email service among virtual servers, AI apps, and libraries. Ideally, you can integrate your email system with your program hosted on Amazon.
What are common SMTP mail server ports and how do you find them?
Picking the right port for SMTP mailservers
Send and receive email in Pytest Python tests.
Python automation email testing Robotframework plugin
How to send emails that don't trigger a spam warning.
What is cc and bcc in email? What is the difference between cc and bcc?
What is BCC email field (blind carbon copy) and how does it differ to CC?
MailServers use TCP ports to transmit emails. Picking the right port between 25 vs 2555, 465 vs 587 in SMTP is easy with this guide.
How to setup MailSlurp with Outlook, Gmail, Thunderbird, Mail app and more.
Create a custom SMTP client and access MailSlurp inboxes from CSharp/DotNET.
DMARC – All Facts that you should know
Email size limits
How to manage email subdomain custom mailboxes using SMTP/IMAP mailserver settings.
Send email in Visual basic
A guide to PHP Mailer
How to access MailSlurp email accounts using IMAP and SMTP servers
How to use SMTP relay to address spam and security issues that affect outbound mail servers.
The difference between HTTP and SMTP inboxes and domains in MailSlurp.
What are SMTP ports
How to use CSharp SMTP client to send email with MailSlurp mail server
How to use Java SMTP client to send email with MailSlurp mail server on the JDK
Use PHPMailer to send emails with SMTP and MailSlurp
How to send emails with Python's built-in SMTP client and MailSlurp mail servers.
How to use Ruby SMTP client to send email with the MailSlurp mail servers