简体   繁体   中英

Django:Setting up email

I created the following entries in my 'settings.py' file

# Email setup
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'someone@someorg.com'
EMAIL_HOST_PASSWORD = 'thepassword'
EMAIL_PORT = 587

Then I issued the following command in the terminal:

email = EmailMessage('Hello','Have you received this mail?',to=['someone@gmail.com'])

I got 1 as an ouput.

When I checked the mailbox at 'someone@gmail.com',no mail was there in the mailbox.Did I miss something?

You should setup from_email parameter (as a kwarg or 3rd arg) with EmailMessage call. Or define in settings.py:

DEFAULT_FROM_EMAIL = 'some.mail@inter.net'

This is taken as default if no from_email is provided to EmailMessage .

On a semi-related note, it's probably a good idea to also define SERVER_EMAIL in settings.py. This one is used with mail_admins and mail_managers by Django.

All your settings are correct and complete, you just missed one thing at the end. You created a mail object and now email is ready to be sent email object, but you need to actually send it with email.send() . For more info and examples check Django documentation . Also, you can use send_mail which automatically creates the email object and sends it.

from django.core.mail import send_mail

mail_title = 'Hello!'
message = 'Have you received this mail?'
email = 'admin@company.com'
recipients = 'someone@gmail.com'

send_mail(mail_title, message, email, [recipients])

According to the following link https://docs.djangoproject.com/en/1.3/topics/email/ , you should put email in brackets. That means, it should look like this:

from django.core.mail.import send_mail

mail_title = 'Hello!'
message = 'Have you received this mail?'
email = admin@company.com
recipients = 'someone@gmail.com'

send_mail(mail_title, message, email, [recipients])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM