amazon_sender.py
To install: pip install ir
The ir package provides a Python helper class named AmazonSender for sending emails using Amazon's Simple Email Service (SES) through the boto library. This class handles encodings properly and supports sending both text and HTML emails. It is designed to be flexible, allowing users to specify various email parameters such as sender, recipient, and message content.
- Email Encoding: Proper handling of different encodings ensuring that text and HTML emails are correctly displayed.
- Multiple Recipients: Ability to send emails to multiple recipients.
- HTML and Text Emails: Support for both HTML and plain text parts in emails.
- Customizable Sender and Reply-To: Custom sender and reply-to addresses can be specified.
- AWS Credentials Management: Automatic fetching of AWS credentials from environment variables if not provided.
To install the package, use the following pip command:
pip install irYou can initialize the AmazonSender object by providing AWS credentials directly or letting the class fetch them from environment variables:
from ir import AmazonSender
# Initialize with direct credentials
amazon_sender = AmazonSender(aws_key='your_aws_access_key', aws_secret='your_aws_secret_key')
# Initialize with environment variables
amazon_sender = AmazonSender()To send an email, you can use the send_email method. This method allows you to specify the sender, recipient, subject, and body of the email in both text and HTML formats:
response = amazon_sender.send_email(
sender='Me <me@example.com>',
to_addresses=['recipient1@example.com', 'recipient2@example.com'],
subject='Greetings',
text='Hello, this is a plain text message.',
html='<h1>Hello</h1><p>This is an HTML message.</p>'
)Before sending an email, you might want to verify the email address using Amazon SES:
verification_status = amazon_sender.verify_email('test@example.com')A class to send emails using Amazon SES.
Constructor to initialize the AmazonSender with optional AWS credentials, sender, and recipient addresses.
- Parameters:
to_addresses: The default recipient address(es). Can be a string or a list of strings.sender: The default sender email address.aws_key: AWS access key.aws_secret: AWS secret key.
Sends an email with the specified subject, text, and/or HTML content.
- Parameters:
subject: Subject of the email.text: Plain text content of the email.html: HTML content of the email.reply_addresses: Email address(es) to which replies should be sent.sender_ascii: ASCII representation of the sender's email address, used in the email header.
Requests Amazon SES to verify an email address.
- Parameters:
email: The email address to verify.
Initializes or retrieves the SESConnection client.
This project is licensed under the BSD license.