Ido Naveh
Ido Naveh

Reputation: 2492

Task timed out when trying to use SES SendEmail operation

I'm trying to send an email message using AWS SES with Python 3.7 lambda function.

When I'm trying to test the function and see if it sends the email message, I get a message that the task timed out.
It reaches to the code part where it sends the message, but the message isn't being sent at any time, and the task just got timeout.

This is the code that I'm using to send the message:

from __future__ import print_function
import boto3
import json
import decimal
from datetime import datetime
from boto3.dynamodb.conditions import Key, Attr
from botocore.exceptions import ClientError

ses = boto3.client(
    'ses', 
    region_name = 'us-east-1', 
    endpoint_url = 'https://email.us-east-1.amazonaws.com'
)

try:
    response = ses.send_email(
        Destination = {
            'ToAddresses': [
                email
            ],
        },
        Message = {
            'Body': {
                'Html': {
                    'Charset': CHARSET,
                    'Data': BODY_HTML
                },
                'Text': {
                    'Charset': CHARSET,
                    'Data': BODY_TEXT
                },
            },
            'Subject': {
                'Charset': CHARSET,
                'Data': SUBJECT
            }
        },
        Source = SENDER
    )
except ClientError as e:
    print(e)
else:
    print('Email sent! Message ID:'),
    print(response['MessageId'])

All of the above variables are hardcoded in the code, and definitely has value (I have printed it to the console to ensure it).

Upvotes: 4

Views: 4261

Answers (4)

Jasper George
Jasper George

Reputation: 11

I reached out to AWS tech support and they suggested me to use a private subnet with NAT gateway to make SES email function work in Lambda setup.

Upvotes: 1

Rocky
Rocky

Reputation: 331

I tried unsuccessfully for a long time to get this to work with a boto3 SES client and just couldn't do it, even after creating an SES endpoint (my lambda is in a VPC). It kept timing out without any other error. In case this helps anyone else, I switched to using smtplib as explained here: https://www.authsmtp.com/python/index.html. I don't know if there is a downside to this approach; I only know that it worked for me.

import boto3
import json
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib

and then (I have the SES SMTP credentials stored in AWS Secrets Manager):

# get SES smtp email credentials from Secrets Manager
client2 = boto3.client('secretsmanager')
response = client2.get_secret_value(
SecretId='secretIDhere'
)
secretDict = json.loads(response['SecretString'])
smtpusername = secretDict['username']
smtppassword = secretDict['password']
smtphost = 'email-smtp.us-east-1.amazonaws.com:25'

and finally

# send email
msg = MIMEMultipart()
message = "Message text here"
msg['From'] = "email address here"
msg['To'] = "email address here"
msg['Subject'] = "Subject line here"
msg.attach(MIMEText(message, 'plain'))
server = smtplib.SMTP(smtphost)
server.starttls()
server.login(smtpusername, smtppassword)
server.sendmail(msg['From'], msg['To'], msg.as_string())
server.quit()

Upvotes: 1

Gilad S
Gilad S

Reputation: 2021

I'm having the same issue (but with nodejs). I found these answers that may help you, looks like a networking issue of aws. https://forums.aws.amazon.com/thread.jspa?threadID=232508 https://github.com/aws/aws-sdk-js/issues/1451

Edit: The easiest solution for me in a similar situation was to use SNS to invoke another lambda that is not inside a VPC. The second lambda is used to call the SES service. You can create an endpoint for SNS and connect it to the VPC.

Upvotes: 2

Tomanow
Tomanow

Reputation: 7377

It looks like your endpoint_url = 'https://dynamodb.us-east-1.amazonaws.com' is pointing to DynamoDB which doesn' seem valid in an SES client. Try removing that:

ses = boto3.client(
    'ses', 
    region_name = 'us-east-1',
)

Upvotes: 1

Related Questions