Python的Django的Gmail的SMTP设置 [英] Python Django Gmail SMTP setup

查看:293
本文介绍了Python的Django的Gmail的SMTP设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过设置Gmail的SMTP从Django的发送电子邮件。 但每次它返回我0状态。我已经搜查计算器不同的相关解答,我设置SMTP服务器的方式相同,但它仍然不发送任何电子邮件.. 下面是我的设置文件

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

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'

下面是我的code

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"

不过,每次它打印状态0,这意味着电子邮件不会被发送。关于环境我运行在Amazon EC2实例这一code具有作为Ubuntu的操作系统和Apache的服务器。

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..

我是否需要做额外的设置,以便通过Gmail SMTP发送邮件? 许多AP preciate您的帮助 在此先感谢

Do i need to do additional setups for sending email through gmail smtp?? Much appreciate your help Thanks in advance

推荐答案

gmail.smtp 设置是正确的。它看起来像你没有正确地调用 SEND_EMAIL 的功能,这就是为什么它不发送。在Python壳,请尝试以下操作:

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:

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,
    ['to@example.com'], fail_silently=False)

这篇关于Python的Django的Gmail的SMTP设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆