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

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

问题描述

我知道有 20 个问题与我的类似,但我已经尝试了一天多来获取电子邮件以与 Django 合作.

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

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

尝试:msg = EmailMessage(主题, 消息, from_email, [接收者])msg.content_subtype = "html"msg.send()

我的设置文件如下:

EMAIL_HOST = "本地主机"DEFAULT_FROM_EMAIL = "myemail@gmail.com"电子邮件端口 = 25EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"

我尝试使用 python -m smtpd -n -c DebuggingServer localhost:1025 进行测试发送并取得了成功,但归根结底是真正做到这一点时,没有成功.

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

<预><代码>>>>从 django.core.mail 导入 send_mail>>>send_mail('测试', '测试', 'myemail@gmail.com', ['myemail@gmail.com'])回溯(最近一次调用最后一次):文件<console>",第 1 行,在 <module> 中文件/usr/local/lib/python2.6/dist-packages/django/core/mail/__init__.py",第 61 行,在 send_mail连接=连接).send()文件/usr/local/lib/python2.6/dist-packages/django/core/mail/message.py",第251行,发送返回 self.get_connection(fail_silently).send_messages([self])文件/usr/local/lib/python2.6/dist-packages/django/core/mail/backends/smtp.py",第 79 行,在 send_messagesnew_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__ 中(代码,味精)= self.connect(主机,端口)文件/usr/lib/python2.6/smtplib.py",第 295 行,在连接中self.sock = self._get_socket(host, port, self.timeout)_get_socket 中的文件/usr/lib/python2.6/smtplib.py",第 273 行返回 socket.create_connection((port, host), timeout)文件/usr/lib/python2.6/socket.py",第 561 行,在 create_connection引发错误,味精错误:[Errno 111] 连接被拒绝

我似乎对此无所适从.任何帮助或建议将不胜感激.谢谢

另外,如果你还有什么想看的,就评论吧.

解决方案

您是否正在尝试使用 Gmail 帐户?也许试试这个:

EMAIL_HOST = 'smtp.gmail.com'EMAIL_HOST_USER = '您的用户名@gmail.com'EMAIL_HOST_PASSWORD = '您的密码'电子邮件端口 = 587EMAIL_USE_TLS = 真

然后试试 test (django < 1.4) by

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

如果你使用 django 1.4 使用这个:

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

如果您没有使用 Gmail 帐户并且仍然遇到问题,那么只需尝试将 EMAIL_HOST_USEREMAIL_HOST_PASSWORD 添加到您拥有的帐户中.如果您仍然有问题,则可能是您的网络阻止了您.操作系统或路由器上的防火墙.

感谢 knite 更新语法.给他一个 +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天全站免登陆