Rahul Sharma
Rahul Sharma

Reputation: 2495

Send a mail from Django via Amazon SES

I am trying to send a mail from my django app via Amazon SES but I keep getting the following error:

smtplib.SMTPSenderRefused: (501, b'Invalid MAIL FROM address provided', '=?utf-8?q?AKIAWMDVL5UEWNT3ODOO?=')

These are the settings that I am using:

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'email-smtp.ap-south-1.amazonaws.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'smtp credential key'
EMAIL_HOST_PASSWORD = 'smtp credential password'
EMAIL_USE_TLS = True

and here's the code:

class CompanyCreateAPIView(APIView):
    permission_classes = (permissions.AllowAny,)
    def post(self, request):
        email = request.data["company_email"]
        phone = request.data["company_phone"]

        def random_with_N_digits(n):
            range_start = 10 ** (n - 1)
            range_end = (10 ** n) - 1
            return randint(range_start, range_end)

        code = random_with_N_digits(4)

        subject = 'Comapny Creation'
        message = 'Company Code is {}'.format(code)
        email_from = settings.EMAIL_HOST_USER
        recipient_list = [str(email), ]
        send_mail(subject, message, email_from, recipient_list)

I tried changing the port but it doesn't work, I have verified the email address in SES.. What is it that I am missing ?

Upvotes: 0

Views: 874

Answers (1)

deceze
deceze

Reputation: 522175

You probably do not want to use the SMTP credentials username as the "email from" value. That should be something human readable, whatever you want your recipients to see as the address where the email came from. That address and/or domain also need to be whitelisted in the SES settings.

In Django's settings.py, set the DEFAULT_FROM_EMAIL setting to that address:

DEFAULT_FROM_EMAIL = '[email protected]'

Do not pass email_from as an explicit setting to send_mail, unless you want to override it for specific emails (e.g. send from info@... for certain emails and newsletter@... for other kinds of emails).

Upvotes: 1

Related Questions