API方式发送邮件

SES API包括SendEmail, SendRawEmail, and SendTemplatedEmail

  • SendEmail 用于发送格式化的邮件
  • SendRawEmail 用于发送原始的MIME格式的邮件
  • SendTemplatedEmail 根据预定义的模板来发送邮件

本节我们介绍通过SendEmail, SendRawEmail来发送邮件

SendEmail API

将以下内容保存为sendmail.py, 替换sender_emailrecipient_email

import boto3
from botocore.exceptions import ClientError


ses_client = boto3.client('ses')
sender_email = "sender@example.com"  # replace this with sender email address
recipient_email = "recipient@example.com"  # replace this with receiver email address

try:
    response = ses_client.send_email(
        Source=sender_email,
        Destination={
            'ToAddresses': [recipient_email]
        },
        Message={
            'Subject': {
                'Data': 'Company Newsletter'
            },
            'Body': {
                'Text': {
                    'Data': 'This is the content of the company newsletter.'
                }
            }
        }
    )
except ClientError as e:
    print(e.response['Error']['Message'])
else:
    print("Email sent! Message ID: ", response['MessageId'])

运行脚本:

image-20231005080224800

效果:

image-20231005080320854

SendRawEmail API

将下面内容保存为send-raw-email.py,替换sender_emailrecipient_email

import boto3
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from botocore.exceptions import ClientError


ses_client = boto3.client('ses',region_name = 'us-east-1')
sender_email = "sender@example.com"
recipient_email = "recipient@example.com"

# Create the MIME object
msg = MIMEMultipart()
msg['Subject'] = 'Company Newsletter with Attachment'
msg['From'] = sender_email
msg['To'] = recipient_email

# Add the newsletter content
text = MIMEText('This is the content of the company newsletter with an attachment.', 'plain')
msg.attach(text)

# Add the attachment
attachment = MIMEApplication('This is the attachment content.', _subtype='txt')
attachment.add_header('Content-Disposition', 'attachment', filename='attachment.txt')
msg.attach(attachment)

try:
    # Send the email
    response = ses_client.send_raw_email(
        Source=sender_email,
        Destinations=[recipient_email],
        RawMessage={
            'Data': msg.as_string()
        }
    )
except ClientError as e:
    print(e.response['Error']['Message'])
else:
    print("Email sent! Message ID: ", response['MessageId'])

运行脚本,效果如下:

image-20231005082109453

CLI

除了使用SDK外,使用CLI也能直接发送邮件:

aws sesv2 send-email \
    --from-email-address sender@example.com \
    --destination ToAddresses=recipient@example.com \
    --content '{"Simple": {"Subject": {"Data": "Test Email","Charset": "UTF-8"},"Body": {"Text": {"Data": "This is a test email sent using the AWS CLI SESv2.","Charset": "UTF-8"},"Html": {"Data": "This is a test email sent using the AWS CLI SESv2.","Charset": "UTF-8"}}}}'

Mailbox Simulator

使用Mailbox Simulator 可以模拟不同的行为,例如 successful delivery, bounce, complaint等。

测试success:

aws sesv2 send-email \
    --from-email-address sender@example.com \
    --destination ToAddresses=success@simulator.amazonses.com \
    --content '{"Simple": {"Subject": {"Data": "Test Email to Success Mailbox Simulator","Charset": "UTF-8"},"Body": {"Text": {"Data": "This is a test email to the success mailbox simulator.","Charset": "UTF-8"},"Html": {"Data": "This is a test email to the success mailbox simulator.","Charset": "UTF-8"}}}}'

image-20231005102616985

测试bounce

aws sesv2 send-email \
    --from-email-address sender@example.com \
    --destination ToAddresses=bounce@simulator.amazonses.com \
    --content '{"Simple": {"Subject": {"Data": "Test Email to Bounce Mailbox Simulator","Charset": "UTF-8"},"Body": {"Text": {"Data": "This is a test email to the bounce mailbox simulator.","Charset": "UTF-8"},"Html": {"Data": "This is a test email to the bounce mailbox simulator.","Charset": "UTF-8"}}}}'

测试out-of-the-office

aws sesv2 send-email \
    --from-email-address sender@example.com \
    --destination ToAddresses=ooto@simulator.amazonses.com \
    --content '{"Simple": {"Subject": {"Data": "Test Email to Out-of-the-Office Mailbox Simulator","Charset": "UTF-8"},"Body": {"Text": {"Data": "This is a test email to the Out-of-the-Office mailbox simulator.","Charset": "UTF-8"},"Html": {"Data": "This is a test email to the Out-of-the-Office mailbox simulator.","Charset": "UTF-8"}}}}'

测试complaint

aws sesv2 send-email \
    --from-email-address sender@example.com \
    --destination ToAddresses=complaint@simulator.amazonses.com \
    --content '{"Simple": {"Subject": {"Data": "Test Email to Complaint Mailbox Simulator","Charset": "UTF-8"},"Body": {"Text": {"Data": "This is a test email to the complaint mailbox simulator.","Charset": "UTF-8"},"Html": {"Data": "This is a test email to the complaint mailbox simulator.","Charset": "UTF-8"}}}}'

测试suppression list

aws sesv2 send-email \
    --from-email-address sender@example.com \
    --destination ToAddresses=suppressionlist@simulator.amazonses.com \
    --content '{"Simple": {"Subject": {"Data": "Test Email to Suppression List Mailbox Simulator","Charset": "UTF-8"},"Body": {"Text": {"Data": "This is a test email to the suppression list mailbox simulator.","Charset": "UTF-8"},"Html": {"Data": "This is a test email to the suppression list mailbox simulator.","Charset": "UTF-8"}}}}'

上面几种不同的行为将在后面进行介绍