Miserlou/Zappa

Unable to send emails via SES on django app hosted through Zappa(lambda)

shrinidhinhegde opened this issue · 2 comments

so i have hosted my site on lambda using zappa. and i am using django-amazon-ses to send email after submitting a form.

settings.py

AWS_ACCESS_KEY_ID = os.environ.get("AWS_ACCESS_KEY_ID", "my access key")
AWS_SECRET_ACCESS_KEY = os.environ.get("AWS_SECRET_ACCESS_KEY", "my secret key")
DEFAULT_FROM_EMAIL = 'xxxx@xxx.xx'
EMAIL_BACKEND = 'django_amazon_ses.EmailBackend'
AWS_SES_REGION = 'ap-south-1'
AWS_SES_REGION_ENDPOINT = 'email-smtp.ap-south-1.amazonaws.com'

view

@csrf_exempt
def formSubmit(request):
    if request.method == 'POST':
        var = json.loads(request.body)
        name = var['name1']
        email = var['email1']
        company = var['company1']
        description = var['description1']
        send_mail('subject',
                  'msg',
                  'xxxx@xxx.xx',
                  [email])
        send_mail('subject',
                  'msg',
                  'xxxx@xxx.xx',
                  ['toemail@gmail.com'])
    return JsonResponse({'result': 'done'})

now this works fine on my local host but, when i try to do it online, it shows the following error on submitting.

ClientError at /submit/
An error occurred (InvalidClientTokenId) when calling the SendRawEmail operation: The security token included in the request is invalid.

At first, i thought it is because i havent configued a vpc with the lambda function, but then it showed me the same error after i configured a public/private vpc wizard.

not sure what am i doing wrong. any help would be appreciated.

SES does not work with the default access keys attached to every Lambda. You have to create an IAM user with SES policy permissions attached to it and set AWS_SES_ACCESS_KEY_ID and AWS_SES_SECRET_ACCESS_KEY.

Should we add AWS_SES_ACCESS_KEY_ID and AWS_SES_SECRET_ACCESS_KEY in settings.py too ?