yagmail
is a user-friendly library for sending emails from Python. It simplifies the process of composing and sending emails, including handling attachments and HTML content. This guide will show you how to configure yagmail
to use a custom SMTP server with a custom port, including SSL/TLS settings.
Table of Contents
Personal Experience
Normally, I would recommend using native utilities for email sending, especially on Linux. Tools like sendmail and mutt are commonly used. However, these utilities can be quite finicky to set up, with many moving parts and configuration nuances. In my experience, setting up these tools can be time-consuming and error-prone, especially when integrating them into automation scripts.
If you’re looking for a straightforward solution to attach to your automation script without much fuss, yagmail
is the simplest option I have encountered so far. It abstracts away much of the complexity and allows you to get up and running quickly with minimal configuration.
Prerequisites
Python Installed: Ensure you have Python installed on your system.
Yagmail Installed: Install yagmail
using pip.
$ pip install yagmail
Configuration
Step 1: Install yagmail
First, install yagmail
if you haven’t already:
$ pip install yagmail
Step 2: Set Up SMTP Configuration
To perform a yagmail custom SMTP setup, you need to configure yagmail
with your SMTP server details. You can either use environment variables or configure it directly in your Python script.
Option 1: Using Environment Variables
Set Environment Variables: Add the following lines to your shell profile (~/.bashrc
, ~/.zshrc
, etc.):
export YAGMAIL_SMTP_SERVER="smtp.yourprovider.com"
export YAGMAIL_SMTP_PORT=587 # or 465 for SSL
export YAGMAIL_SMTP_USER="your-email@yourprovider.com"
export YAGMAIL_SMTP_PASSWORD="your-password"
Reload Your Shell Profile:bashCopy code
$ source ~/.bashrc
# OR
$ source ~/.zshrc
Option 2: Directly in Python Script
You can configure yagmail
directly in your script by providing SMTP settings:
import yagmail
# Initialize yagmail with custom SMTP settings
yag = yagmail.SMTP(
user='your-email@yourprovider.com',
password='your-password',
host='smtp.yourprovider.com',
port=587, # or 465 for SSL
smtp_starttls=True # Use TLS (set to False for SSL)
)
Step 3: Sending Emails
With yagmail
configured, you can now send emails. Here are examples for basic emails, emails with attachments, and HTML emails.
Basic Email
import yagmail
# Initialize yagmail
yag = yagmail.SMTP('your-email@yourprovider.com', 'your-password')
# Send a basic email
yag.send(
to='recipient@example.com',
subject='Test Subject',
contents='This is the email body'
)
Email with Attachments
import yagmail
# Initialize yagmail
yag = yagmail.SMTP('your-email@yourprovider.com', 'your-password')
# Send an email with attachments
yag.send(
to='recipient@example.com',
subject='Test Subject',
contents='This is the email body',
attachments='/path/to/attachment.file'
)
HTML Email
import yagmail
# Send an HTML email
yag.send(
to='recipient@example.com',
subject='Test Subject',
contents='<h1>This is an HTML email</h1>'
)
Step 4: Using SSL/TLS
For SSL: Use port 465
and set smtp_starttls=False
.
yag = yagmail.SMTP(
user='your-email@yourprovider.com',
password='your-password',
host='smtp.yourprovider.com',
port=465, # SSL port
smtp_starttls=False
)
For TLS: Use port 587
and set smtp_starttls=True
.
yag = yagmail.SMTP(
user='your-email@yourprovider.com',
password='your-password',
host='smtp.yourprovider.com',
port=587, # TLS port
smtp_starttls=True
)
Troubleshooting
If you encounter issues:
- Check SMTP Server Details: Verify that the SMTP server address, port, and credentials are correct.
- Test Connectivity: Use tools like
openssl
to ensure you can connect to the SMTP server. - Review Errors: Check error messages for details and adjust configuration as needed.
Example Python Script
Here’s a complete example that uses custom SMTP settings with yagmail
:
import yagmail
# Initialize yagmail with custom SMTP settings
yag = yagmail.SMTP(
user='your-email@yourprovider.com',
password='your-password',
host='smtp.yourprovider.com',
port=587, # TLS port
smtp_starttls=True
)
# Send an email
try:
yag.send(
to='recipient@example.com',
subject='Test Subject',
contents='This is the email body'
)
print("Email sent successfully!")
except Exception as e:
print(f"Failed to send email: {e}")