SonOfSeuss
SonOfSeuss

Reputation: 400

AWS Lambda SES SMTPLib Email Timing Out Using Python

My lambda environment is set up in us-east-2 however I know SES only is present in us-east-1. This is the code:

    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

It times out right here with a 10 second time out. It always stops at this line of code: logger.info("send_emails: step 3: smtp") which means it stops here server.connect('email-smtp.us-east-1.amazonaws.com', 587).

I've given access to both of these email addresses in the SES AWS Console.

Do I have to move the function to us-east-1. Please keep in mind that my RDS instance is in us-east-2.

EDIT

These are my security group rules for outbound:

Outbound Port Rules

My Inbound rules are as follows:

Inbound Port Rules

The sources are my work and home machines.

Upvotes: 1

Views: 1372

Answers (1)

Arun Kamalanathan
Arun Kamalanathan

Reputation: 8593

The solution was to add the host name to the smtplib.SMTP() as well

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()

looks like its a python bug

Upvotes: 1

Related Questions