Amazon SES 信用可与 smtplib 配合使用,但不能与 boto3 配合使用

问题描述 投票:0回答:1

我正在尝试使用 Amazon SES 发送电子邮件。创建角色、API 密钥和策略,并使用 Python

smtplib
对其进行测试。粘贴下面的片段:

import smtplib

fromaddr = "[email protected]"
toaddrs  = "[email protected]"
msg = """From: [email protected]
Hello, this is test from python.
"""

smtp_server = 'email-smtp.us-east-1.amazonaws.com'
smtp_username = 'SES_KEY'
smtp_password = 'SES_SECRET'
smtp_port = '25'
smtp_do_tls = True

server = smtplib.SMTP(
    host = smtp_server,
    port = smtp_port,
    timeout = 10
)
server.set_debuglevel(10)
server.starttls()
server.ehlo()
server.login(smtp_username, smtp_password)
server.sendmail(fromaddr, toaddrs, msg)
print(server.quit())

它按预期工作。我收到电子邮件和以下输出:

send: 'ehlo servername.com\r\n'
reply: '250-email-smtp.amazonaws.com\r\n'
reply: '250-8BITMIME\r\n'
reply: '250-STARTTLS\r\n'
reply: '250-AUTH PLAIN LOGIN\r\n'
reply: '250 Ok\r\n'
reply: retcode (250); Msg: email-smtp.amazonaws.com
8BITMIME
STARTTLS
AUTH PLAIN LOGIN
Ok
send: 'AUTH PLAIN AEFLSU......HJiRkh3\r\n'
reply: '235 Authentication successful.\r\n'
reply: retcode (235); Msg: Authentication successful.
send: 'mail FROM:<[email protected]>\r\n'
reply: '250 Ok\r\n'
reply: retcode (250); Msg: Ok
send: 'rcpt TO:<[email protected]>\r\n'
reply: '250 Ok\r\n'
reply: retcode (250); Msg: Ok
send: 'data\r\n'
reply: '354 End data with <CR><LF>.<CR><LF>\r\n'
reply: retcode (354); Msg: End data with <CR><LF>.<CR><LF>
data: (354, 'End data with <CR><LF>.<CR><LF>')
send: 'From: [email protected]\r\n\r\nHello, this is test from python.\r\n.\r\n'
reply: '250 Ok 0100018a4b42947f-c2eed....96d8078-000000\r\n'
reply: retcode (250); Msg: Ok 0100018a4b429....52-e84d296d8078-000000
data: (250, 'Ok 0100018a4b42947f-......-e84d296d8078-000000')
send: 'quit\r\n'
reply: '221 Bye\r\n'
reply: retcode (221); Msg: Bye
(221, 'Bye')

但是当我用

boto3
做同样的事情时,我收到错误:

Error: An error occurred (SignatureDoesNotMatch) when calling the SendEmail operation: The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details.

片段:

import boto3
from botocore.exceptions import BotoCoreError, ClientError

def send_email():
    ses = boto3.client('ses', region_name='us-east-1', aws_access_key_id='key', aws_secret_access_key='secret')
    subject = 'Your Email Subject'
    body = """
    This is a test email from BOTO3 SES.
    """
    message = {"Subject": {"Data": subject},
               "Body": {"Text": {"Data": body}}}

    try:
        response = ses.send_email(Source='[email protected]',
                                  Destination={'ToAddresses': ['[email protected]']},
                                  Message=message)
    except (BotoCoreError, ClientError) as error:
        print(f"Error: {error}")
send_email()

以前有人遇到过这个问题吗?我已经在 us-east-1 中创建了身份,因此我正在使用该区域,另外我还尝试授予该角色完全访问权限以在所有资源上发送电子邮件,但 boto3 仍然给出错误。

如有任何帮助,我们将不胜感激。

python amazon-web-services boto3 amazon-ses
1个回答
0
投票

您为通过 SMTP 发送电子邮件而创建的 SES 凭证适用于通过 SMTP 协议向 AWS SES SMTP 终端节点发送电子邮件。

根据官方文档

重要

您的 SMTP 凭证与您的 AWS 访问密钥或用于登录 Amazon SES 控制台的凭证不同。

Boto3

ses.send_email()
函数不使用SMTP协议,并且它不连接到SMTP终端节点,它直接调用AWS SES API。您无法使用 SMTP 凭证与标准 AWS API 进行交互。 SES SMTP 凭证主要用于与标准 SMTP 电子邮件服务器交互而构建的软件,并且不支持 AWS API。

您需要将常规 IAM 凭证与 Boto3 结合使用。

© www.soinside.com 2019 - 2024. All rights reserved.