Django发送电子邮件

39

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

当我尝试发送电子邮件时,出现了这个错误:[Errno 111] Connection refused

以下是我在视图中创建并尝试发送电子邮件的位置:

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"
我尝试使用命令 python -m smtpd -n -c DebuggingServer localhost:1025 进行测试发送并且成功了,但是在实际操作中却没有成功。
当我在 shell 中尝试发送邮件时,出现了以下回溯信息:
>>> 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

我似乎在这方面毫无进展。非常感谢您的任何帮助或建议。谢谢

另外,如果您还有其他想看到的内容,请留言。

8个回答

77

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

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

然后尝试测试 (django < 1.4) 通过

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

如果您使用的是Django 1.4,请使用以下内容:

python manage.py shell
>>> from django.core.mail import 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中语法发生了变化。

我一直尝试通过我的本地服务器发送电子邮件。不过,我想也可以通过Gmail发送。谢谢。 - Luke
您确定 EMAIL_HOST_USER 包括 gmail 在内,它不应该只是“用户名”吗? - pranavk
4
此外,最新的Django版本中似乎已经略微更改了send_mail语法,现在需要四个参数而不是三个。 - pranavk
@pranavk 是的,谷歌规定必须使用您的完整电子邮件地址(包括 @gmail.com),这很可能是因为他们通过其应用程序计划为其他域提供服务。 - Ryan Jenkins
它可以在 shell 中运行,但是当我从视图中使用这段代码时,它就无法工作了。有什么想法吗? - mariowise
对于Digital Ocean的用户,默认情况下会阻止STMP端口。https://dev59.com/qYXca4cB1Zd3GeqPInak - Julio Marins

46

首先创建一个应用程序专用密码

  1. 访问您的Google账户安全页面。并单击两步验证: enter image description here

  1. Google账户安全页面中,单击应用程序密码: enter image description here

  1. 创建一个应用程序,选择邮件并给它一个名称: enter image description here

  1. 记下应用程序密码: enter image description here

然后将适当的值添加到settings.py中:

EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'your-username@gmail.com'
EMAIL_HOST_PASSWORD = 'Application spectific password(for eg: smbumqjiurmqrywn)'
EMAIL_PORT = 587
EMAIL_USE_TLS = True

您可以使用Shell进行测试:

python manage.py shell
>>> from django.core.mail import send_mail
>>> send_mail('Test', 'This is a test', 'your@email.com', ['toemail@email.com'],
     fail_silently=False)

1
很遗憾,Google for Business的Gmail帐户似乎没有提供应用程序特定密码。当我尝试直接访问应用程序特定密码页面(https://security.google.com/settings/u/3/security/apppasswords)时,会出现以下错误:您要查找的设置对于您的帐户不可用。 - randalv
这是更新的文档 - suhailvs

17
"@mongoose_za提供了一个很好的答案,但在Django 1.4+中语法有些不同。

替代方法如下:

"
send_mail('test email', 'hello world', to=['test@email.com'])

使用

send_mail('test email', 'hello world', 'your@email.com', ['test@email.com'])

前四个参数是必需的:subject(主题)、message(消息)、from_email(发件人)和recipient_list(收件人列表)。


4
  1. 在Gmail设置中启用POP3。
  2. 为此Django应用程序创建特定的应用程序密码。(http://support.google.com/accounts/bin/answer.py?hl=zh-Hans&answer=185833)

3

我建议避免使用GMail。虽然它能工作一段时间,但之后你可能会发现你的所有电子邮件都被拒绝或被标记为垃圾邮件。我使用了亚马逊的“SES”服务与Django-SES一起解决这个问题。


2
最近开始使用SendGrid,这也是一个很棒的服务。 - Sam Stoelinga

1
在settings.py中,使用smtp作为后端而不是控制台。
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'

0

首先在settings.py文件中设置电子邮件后端变量。

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

接下来,创建一个邮件对象来发送邮件。

from django.core import mail

connection = mail.get_connection() # Manually open the connection 
connection.open() # Construct an email message that uses the connection 
email = mail.EmailMessage( 'Hello', 'Body goes here', 'from@example.com', ['to1@example.com'], connection=connection, ) 
email.send() # Send the email
connection.close()

0
请在您的服务器的settings.py或local_settings.py文件中添加以下最小设置。
EMAIL_HOST = 'localhost'
EMAIL_PORT = 587
EMAIL_USE_TLS = True

不要使用限制很多的smtp.gmail.com,你可以拥有自己的邮件服务器。

你可以通过安装自己的邮件服务器来实现:

sudo apt-get install sendmail

1
我使用了Postfix而不是Sendmail,并且设置与上述相同,但是电子邮件没有发送。 - toothie
@toothie 我也遇到了同样的问题,直到我意识到我在生产服务器上并没有真正安装postfix... - e18r

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接