Django发送电子邮件 [英] Django sending email

查看:185
本文介绍了Django发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有20个类似于我的问题,但我已经尝试了一天以上,让电子邮件与Django合作。



我收到此错误: [Errno 111]当我尝试发送电子邮件时拒绝连接 p>

这是我创建电子邮件并尝试在我的视图中发送的地方:

  try:
msg = EmailMessage(subject,message,from_email,[receiver])
msg.content_subtype =html
msg.send()

我的设置文件如下:

  EMAIL_HOST =localhost
DEFAULT_FROM_EMAIL =myemail@gmail.com
EMAIL_PORT = 25
EMAIL_BACKEND =django.core.mail.backends.smtp.EmailBackend

我尝试使用并获得成功,但是当它归结为真正的,没有成功。



当我尝试从shell中执行send_mail时,我得到这个追溯:

 >>>来自django.core.mail import send_mail 
>>> send_mail('Test','Test','myemail@gmail.com',['myemail@gmail.com'])
追溯(最近的最后一次呼叫):
文件< console> ,第1行在< module>
文件/usr/local/lib/python2.6/dist-packages/django/core/mail/__init__.py,第61行,send_mail
connection = connection).send()
文件/usr/local/lib/python2.6/dist-packages/django/core/mail/message.py,第251行,发送
return self.get_connection(fail_silently).send_messages( [self])
文件/usr/local/lib/python2.6/dist-packages/django/core/mail/backends/smtp.py,第79行,send_messages
new_conn_created = self .open()
文件/usr/local/lib/python2.6/dist-packages/django/core/mail/backends/smtp.py,第42行,打开
local_hostname = DNS_NAME .get_fqdn())
文件/usr/lib/python2.6/smtplib.py,第239行,__init__
(代码,msg)= self.connect(host,port)
文件/usr/lib/python2.6/smtplib.py,第295行,连接
self.sock = self._get_socket(host,port,self.timeout)
文件/ usr / lib / python2.6 / smtplib.py,第273行,_get_socket
返回socket.create_connection((端口,主机),超时)
文件/usr/lib/python2.6/socket.py,第561行,在create_connection
raise错误,msg
错误:[Errno 111]连接拒绝

我似乎没有得到任何地方。任何帮助或建议将不胜感激。谢谢



另外,如果还有其他东西你想看,只要评论一下。

解决方案

您是否尝试使用Gmail帐户?可能尝试这样:

  EMAIL_HOST ='smtp.gmail.com'
EMAIL_HOST_USER ='your-username @ gmail .com'
EMAIL_HOST_PASSWORD ='你的密码'
EMAIL_PORT = 587
EMAIL_USE_TLS = True

然后尝试通过

 测试(django< 1.4)python manage.py shell 
>>>来自django.core.mail import send_mail
>>> send_mail('test email','hello world',to = ['test@email.com'])

如果您使用django 1.4,请使用以下命令:

  python manage.py shell 
>> ;来自django.core.mail import send_mail
>>> send_mail('test email','hello world','your@email.com',['test@email.com'])

如果您没有使用Gmail帐户并仍然遇到问题,那么只需尝试添加 EMAIL_HOST_USER EMAIL_HOST_PASSWORD 你所拥有的
如果您还有问题,您的网络可能会阻止您。您的操作系统或路由器上的防火墙。



感谢更新的语法。给他+1一个+1,感谢pranavk让我知道django 1.4中的语法更改。


I know there are 20 questions similar to mine but I've tried for over a day now to get email to work with Django.

I'm getting this error: [Errno 111] Connection refused when I attempt to send an email

This is where I create the email and attempt to send it in my view:

try:
    msg = EmailMessage(subject, message, from_email, [receiver])
    msg.content_subtype = "html"
    msg.send()

My settings file is as follows:

EMAIL_HOST = "localhost"
DEFAULT_FROM_EMAIL = "myemail@gmail.com"
EMAIL_PORT = 25
EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"

I've tried doing test sending using python -m smtpd -n -c DebuggingServer localhost:1025 and had success, but when it comes down to doing it for real, no success.

When I try doing a send_mail from the shell I get this traceback:

>>> from django.core.mail import send_mail
>>> send_mail('Test', 'Test', 'myemail@gmail.com', ['myemail@gmail.com'])
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/local/lib/python2.6/dist-packages/django/core/mail/__init__.py", line 61, in send_mail
    connection=connection).send()
  File "/usr/local/lib/python2.6/dist-packages/django/core/mail/message.py", line 251, in send
    return self.get_connection(fail_silently).send_messages([self])
  File "/usr/local/lib/python2.6/dist-packages/django/core/mail/backends/smtp.py", line 79, in send_messages
    new_conn_created = self.open()
  File "/usr/local/lib/python2.6/dist-packages/django/core/mail/backends/smtp.py", line 42, in open
    local_hostname=DNS_NAME.get_fqdn())
  File "/usr/lib/python2.6/smtplib.py", line 239, in __init__
    (code, msg) = self.connect(host, port)
  File "/usr/lib/python2.6/smtplib.py", line 295, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "/usr/lib/python2.6/smtplib.py", line 273, in _get_socket
    return socket.create_connection((port, host), timeout)
  File "/usr/lib/python2.6/socket.py", line 561, in create_connection
    raise error, msg
error: [Errno 111] Connection refused

I just don't seem to be getting anywhere with this. Any help or advice would be much appreciated. Thanks

Also, if there is something else you'd like to see, just comment about it.

解决方案

Are you trying to use a gmail account? Maybe try this then:

EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'your-username@gmail.com'
EMAIL_HOST_PASSWORD = 'your-password'
EMAIL_PORT = 587
EMAIL_USE_TLS = True

Then try test (django < 1.4) by

python manage.py shell
>>> from django.core.mail import send_mail
>>> send_mail('test email', 'hello world', to=['test@email.com'])

And if you use django 1.4 use this:

python manage.py shell
>>> from django.core.mail import send_mail
>>> send_mail('test email', 'hello world', 'your@email.com', ['test@email.com'])

If you're not using a gmail account and still getting problems then just try add the EMAIL_HOST_USER and EMAIL_HOST_PASSWORD to what you have. If you still have issues maybe your network is blocking you. Firewalls on your OS or router.

Thanks to knite for the updated syntax. Throw him a +1 and thanks to pranavk for letting me know about the syntax change in django 1.4

这篇关于Django发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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