The classic email sending library for Python, now with enhanced features like HTML templates and attachment support.
To install PyMailers, run the following command:
pip install pymailers
Send emails with HTML content. Support for secure SMTP connections using starttls. Ability to send emails with attachments. Templating support using Jinja2 for dynamic email bodies. Optional logging for successful and failed email deliveries. Requirements Ensure you have the following Python modules installed: smtplib email os sys jinja2 (for templating) logging (for logging support)
You can install the required modules using pip:
pip install -r requirements.txt
PyMailers is simple to use yet powerful. Here's a quick example of sending an email with a plain text body. You can set display to False to suppress logs, or True to print logs after sending the email.
from pymailers.PyMailer import PyMailer
pymailer = PyMailer({
'smtp_host': 'smtp.gmail.com',
'smtp_port': 587,
'email': 'your-email@gmail.com',
'password': 'your-password',
'to': 'recipient-email@gmail.com',
'subject': 'Hello World',
'body': '<p>Hi, I'm Rio</p>',
'display': True
})
pymailer.send()
To send an email with attachments and using an HTML template:
pymailer = PyMailer({
'smtp_host': 'smtp.gmail.com',
'smtp_port': 587,
'email': 'your-email@gmail.com',
'password': 'your-password',
'to': 'recipient-email@gmail.com',
'subject': 'Greetings',
'body': '', # Body will be generated by the template
'display': True,
'attachments': ['path/to/attachment1.pdf', 'path/to/image.jpg'],
'template_path': 'path/to/template.html',
'template_context': {'name': 'Rio'}
})
pymailer.send()
Make sure to replace the placeholders with your actual SMTP settings, email credentials, and file paths.
Logging All operations are logged into pymailer.log. This is helpful for auditing and debugging purposes.