Python Django Gmail SMTP 设置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19264907/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python Django Gmail SMTP setup
提问by planet260
I am trying to send email from Django by setting up gmail smtp. But everytime it is returning me 0 status. I have searched different relevant answers in stackoverflow and i am setting up the smtp server the same way but still it is not sending any email.. Below is my setting file
我正在尝试通过设置 gmail smtp 从 Django 发送电子邮件。但每次它都返回 0 状态。我在 stackoverflow 中搜索了不同的相关答案,我正在以相同的方式设置 smtp 服务器,但它仍然没有发送任何电子邮件.. 下面是我的设置文件
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'my gmail account'
EMAIL_HOST_PASSWORD = 'my gmail account password'
DEFAULT_FROM_EMAIL = 'my gmail account'
DEFAULT_TO_EMAIL = 'to email'
Below is my code
下面是我的代码
from django.conf import settings
from django.core.mail import send_mail
print "Sending Email"
mail_title = 'Test Email'
message = 'This is a test email.'
email = settings.DEFAULT_FROM_EMAIL
recipients = [settings.DEFAULT_TO_EMAIL]
print send_mail(mail_title, message, email, recipients, settings.EMAIL_HOST_USER, settings.EMAIL_HOST_PASSWORD)
print "Email Sent"
But everytime it print status 0 which means email is not sent. About the environment i am running this code on Amazon EC2 instance which has ubuntu as an OS and Apache as server..
但每次它打印状态 0 时,这意味着不发送电子邮件。关于环境,我在 Amazon EC2 实例上运行此代码,该实例将 ubuntu 作为操作系统,Apache 作为服务器。
Do i need to do additional setups for sending email through gmail smtp?? Much appreciate your help Thanks in advance
我需要为通过 gmail smtp 发送电子邮件做额外的设置吗??非常感谢您的帮助 在此先感谢
采纳答案by Aaron Lelevier
Your gmail.smtp
setup is correct. It looks like you are not calling the send_email
function correctly, and that's why it's not sending. In the python shell, try the following:
你的gmail.smtp
设置是正确的。看起来您没有send_email
正确调用该函数,这就是它不发送的原因。在 python shell 中,尝试以下操作:
import django
from django.conf import settings
from django.core.mail import send_mail
send_mail('Subject here', 'Here is the message.', settings.EMAIL_HOST_USER,
['[email protected]'], fail_silently=False)
回答by Pedro Henrique Silva
Try to change EMAIL_USE_TLS=True to EMAIL_USE_SSL=True and EMAIL_PORT=465
尝试将 EMAIL_USE_TLS=True 更改为 EMAIL_USE_SSL=True 和 EMAIL_PORT=465