sajid
sajid

Reputation: 883

Django can not send email to the recipient

I have been trying to develop a small demo app which requires to verify email address. After googling I found out that I can use

 django.core.mail.EmailMessage

to send emails. And below is the changes that I did for my settings.py file.

EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = 'MyPass'
EMAIL_PORT = 587

I created a message and tried to send in the following way.

 email = EmailMessage('Subject', 'Body', ['[email protected]', '[email protected]'])
 email.send()

But I do not get any email at the recipient end. What is wrong. Help will be appreciated. Thanks.

Upvotes: 0

Views: 284

Answers (2)

Mike Covington
Mike Covington

Reputation: 2157

You are missing the from_email and you haven't set DEFAULT_FROM_EMAIL. From the docs:

email = EmailMessage('Hello', 'Body goes here', '[email protected]',
            ['[email protected]', '[email protected]'], ['[email protected]'],
            reply_to=['[email protected]'], headers={'Message-ID': 'foo'})

from_email: The sender’s address. Both [email protected] and Fred <[email protected]> forms are legal. If omitted, the DEFAULT_FROM_EMAIL setting is used.

Upvotes: 1

wpercy
wpercy

Reputation: 10090

It looks like you're missing the from_address parameter which should go before the list of recipients. I believe the EmailMessage constructor should look like this:

email = EmailMessage('Subject', 'Body', EMAIL_HOST_USER, ['[email protected]', '[email protected]'])

It actually looks like you can omit the sender's address, but it will use the value stored in the DEFAULT_FROM_EMAIL core setting.

Upvotes: 1

Related Questions