SMTP in Python
The Python standard library comes with support for constructing complicated email messages, and sending those messages via SMTP using the smtplib
module. This guide provides a comprehensive walkthrough on how to send emails using the SMTP protocol in Python with the smtplib
module. We'll cover both a simple example with a plaintext body and a more advanced example that includes an HTML body and attachments.
Prerequisites
- Ensure that you have Python installed on your system.
- You have created and verified your domain on AhaSend.
- You have created SMTP credentials (username and password) for authentication.
Sending emails with the smtplib module
1. Simple Example: Sending a Plaintext Email
Here's a straightforward example demonstrating how to send a plain text email. Create a new file called send_plain_text_email.py
and import smtplib
and email.message
modules. Configure it to use the SMTP server with the necessary authentication and TLS settings.
import smtplib
from email.message import EmailMessage
def send_plain_text_email():
smtp_server = "send.ahasend.com"
smtp_port = 587
smtp_username = "YOUR_SMTP_USERNAME"
smtp_password = "YOUR_SMTP_PASSWORD"
from_addr = "[email protected]"
to_addr = "[email protected]"
subject = "Test Email"
body = "Hello, this is a test email sent from Python."
# Create the email message
msg = EmailMessage()
msg.set_content(body)
msg['Subject'] = subject
msg['From'] = from_addr
msg['To'] = to_addr
# Connect to the SMTP server
server = smtplib.SMTP(smtp_server, smtp_port)
server.starttls() # Secure the connection
server.login(smtp_username, smtp_password)
server.send_message(msg)
server.quit()
send_plain_text_email()
[email protected]
, YOUR_SMTP_USERNAME
and YOUR_SMTP_PASSWORD
with an email address from your domain (that is added to your AhaSend account), and your actual SMTP credential username and password.Run your Python script using the python (or python3) command to send the email:
python send_plain_text_email.py
2. Advanced Example: Sending an Email with HTML Body and Attachments
Sending an email with an HTML body and attachments requires constructing a MIME message. Create a new file called send_mime_email.py
and follow the example below:
import smtplib
from email.message import EmailMessage
def send_html_email_with_attachment():
smtp_server = "send.ahasend.com"
smtp_port = 587
smtp_username = "YOUR_SMTP_USERNAME"
smtp_password = "YOUR_SMTP_PASSWORD"
from_addr = "[email protected]"
to_addr = "[email protected]"
subject = "HTML Email with Attachment"
# Create a MIME message
msg = EmailMessage()
msg['Subject'] = subject
msg['From'] = from_addr
msg['To'] = to_addr
msg.set_content('This is a fallback for HTML clients')
# Attach the HTML part
html_content = """\
<html>
<head></head>
<body>
<p>Hi!<br>
How are you?<br>
Here is the <a href="http://www.python.org">link</a> you wanted.
</p>
</body>
</html>
"""
msg.add_alternative(html_content, subtype='html')
# Attach a file
filename = "path_to_your_attachment"
with open(filename, "rb") as f:
file_data = f.read()
file_name = f.name
msg.add_attachment(file_data, maintype='application', subtype='octet-stream', filename=file_name)
# Connect to the SMTP server and send the email
server = smtplib.SMTP(smtp_server, smtp_port)
server.starttls()
server.login(smtp_username, smtp_password)
server.send_message(msg)
server.quit()
send_html_email_with_attachment()
[email protected]
, YOUR_SMTP_USERNAME
and YOUR_SMTP_PASSWORD
and path_to_your_attachment
with the appropriate values.smtplib
and email
modules are powerful libraries for working with and sending emails in Python, supporting a wide range of features from simple plaintext emails to complex messages with HTML content and attachments. This guide covered the basics to get you started. For more advanced features and options, please refer to the official documentation:
- Python smtplib module documentation: https://docs.python.org/3/library/smtplib.html
- Python email module documentation: https://docs.python.org/3/library/email.html