Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
200 views
in Technique[技术] by (71.8m points)

python 3.x - Unable to set X-SES-CONFIGURATION-SET headers in aws ses

I am using this script to send email using AWS SES and I am able to receive email in my inbox but I am not able to see X-SES-CONFIGURATION-SET header in the received email. I also tried using Simple email body type with ConfigurationSetName still no luck. Any help is highly appreciated.

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

# Replace [email protected] with your "From" address.
# This address must be verified with Amazon SES.
SENDER = "Niranj Raja <[email protected]>"

# Replace [email protected] with a "To" address. If your account
# is still in the sandbox, this address must be verified.
RECIPIENT = "[email protected]"

# Specify a configuration set. If you do not want to use a configuration
# set, comment the following variable, and the
# ConfigurationSetName=CONFIGURATION_SET argument below.
CONFIGURATION_SET = "test-me"

# The subject line for the email.
SUBJECT = "Amazon SES Test (SDK for Python)"

# The email body for recipients with non-HTML email clients.
BODY_TEXT = ("Amazon SES Test (Python)
"
             "This email was sent with Amazon SES using the "
             "AWS SDK for Python (Boto)."
             )

# The HTML body of the email.
BODY_HTML = """<html>
<head></head>
<body>
  <h1>Amazon SES Test (SDK for Python)</h1>
  <p>This email was sent with
    <a href='https://aws.amazon.com/ses/'>Amazon SES</a> using the
    <a href='https://aws.amazon.com/sdk-for-python/'>
      AWS SDK for Python (Boto)</a>.</p>
</body>
</html>
            """

# The character encoding for the email.
CHARSET = "UTF-8"

msg = MIMEMultipart('mixed')
# Add subject, from and to lines.
msg['Subject'] = SUBJECT
msg['From'] = SENDER
msg['To'] = RECIPIENT
#msg['X-SES-CONFIGURATION-SET'] = CONFIGURATION_SET

msg.add_header('X-SES-CONFIGURATION-SET', CONFIGURATION_SET)

print(dir(msg))

print('')
# Create a multipart/alternative child container.
msg_body = MIMEMultipart('alternative')
print(dir(msg_body))
# Encode the text and HTML content and set the character encoding. This step is
# necessary if you're sending a message with characters outside the ASCII range.
textpart = MIMEText(BODY_TEXT.encode(CHARSET), 'plain', CHARSET)
htmlpart = MIMEText(BODY_HTML.encode(CHARSET), 'html', CHARSET)

msg_body.attach(textpart)
msg_body.attach(htmlpart)

# Create a new SES resource and specify a region.
client = boto3.client(
            'sesv2',
            aws_access_key_id='test',
            aws_secret_access_key='test1',
            region_name='region-10'
        )

# Try to send the email.
try:
    # Provide the contents of the email.
    response = client.send_email(
        FromEmailAddress=SENDER,
        Destination={
            'ToAddresses': [
                RECIPIENT,
            ],
        },
        Content={
            'Raw': {
               'Data': msg_body.as_string()
            }
        }
    )
    print(response)
# Display an error if something goes wrong.
except ClientError as e:
    print(e.response['Error']['Message'])
else:
    print("Email sent! Message ID:"),
    print(response['MessageId'])
question from:https://stackoverflow.com/questions/65884138/unable-to-set-x-ses-configuration-set-headers-in-aws-ses

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...