使用Python的AWS Lambda SES SMTPLib电子邮件超时

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

我的Lambda环境在us-east-2中设置,但是我知道us-east-1中仅存在SES。这是代码:

    sent_from = "[email protected]"
    send_to = "[email protected]"
    aws_user_name = "<USER NAME KEY>"
    #stub - replace this
    aws_password = "<USER PASSWORD KEY>"
    logger.info("send_emails: start")

    for email in email_list:

        try:

            logger.info("send_emails: step 1: smtp")
            server = smtplib.SMTP()
            logger.info("send_emails: step 2: smtp")
            server.connect('email-smtp.us-east-1.amazonaws.com', 587)
            #server.ehlo()
            logger.info("send_emails: step 3: smtp")            
            server.starttls()
            logger.info("send_emails: step 4: smtp")
            #logger.info("User: " + aws_user_user + " Password:" + aws_password)
            server.login(aws_user_name, aws_password)
            logger.info("Email Send To: " + email[1])
            logger.info("Test Point: send_emails: A")
            #server.sendmail(sent_from, email[1], email_body.as_string())
            #REPLACE THIS LINE.  THIS IS JUST FOR TESTING.
            server.sendmail(sent_from, send_to, email_body.as_string())
            server.close()
            insert_into_email_sent_tbl(email[0], "Success")
            logger.info('Email sent!')
            return True

        except Exception as e: 
            logger.info(e)  
            logger.info('Something went wrong...')
            insert_into_email_sent_tbl(email[0], "Fail")
            return False

它在这里超时,超时10秒。它总是在以下代码行停止:logger.info("send_emails: step 3: smtp"),这意味着它在这里停止server.connect('email-smtp.us-east-1.amazonaws.com', 587)

我已在SES AWS控制台中同时访问了这两个电子邮件地址。

我是否必须将功能移至us-east-1。请记住,我的RDS实例在us-east-2中。

编辑

这些是我的出站安全组规则:

Outbound Port Rules

我的入站规则如下:

Inbound Port Rules

来源是我的工作和家用机器。

amazon-web-services aws-lambda amazon-ses smtplib
1个回答
0
投票

解决方案是也将主机名添加到smtplib.SMTP()

print("send_emails: step 1: smtp")
print("send_emails: step 1: smtp")
server = smtplib.SMTP('email-smtp.us-east-1.amazonaws.com')
print("send_emails: step 2: smtp")
server.connect('email-smtp.us-east-1.amazonaws.com', 587)
#server.ehlo()
print("send_emails: step 3: smtp")            
server.starttls()

看起来像它的python bug

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