【问题标题】:Django sending emailDjango 发送电子邮件
【发布时间】:2011-10-18 09:15:12
【问题描述】:

我知道有 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 执行 send_mail 时,我得到了这个回溯:

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

我似乎对此一无所知。任何帮助或建议将不胜感激。谢谢

此外,如果您还有其他想看的内容,请发表评论。

【问题讨论】:

    标签: python django smtp


    【解决方案1】:

    您是否尝试使用 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
    

    然后试试 test (django

    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_USER 和 EMAIL_HOST_PASSWORD 添加到您拥有的帐户中。 如果您仍然有问题,则可能是您的网络阻止了您。操作系统或路由器上的防火墙。

    Thanks to knite for the updated syntax. Throw him a +1 并感谢 pranavk 让我了解 django 1.4 中的语法更改

    【讨论】:

    • 我一直在尝试通过我的本地服务器发送电子邮件。但是我想通过gmail也可以。谢谢
    • 您确定 EMAIL_HOST_USER 也包含 gmail,不应该只是“用户名”吗?
    • 此外,在最新的 django 版本中,send_mail 语法似乎略有变化,现在需要四个参数而不是 3 个。
    • @pranavk 是的,Google 指定需要使用 your full email address(包括 @gmail.com),这很可能是因为他们通过其应用程序为其他域提供服务。
    • 它在 shell 中工作,但是当我从视图中使用这段代码时,它没有。有什么想法吗??
    【解决方案2】:

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

    1. 访问您的Google Account security page。然后点击两步验证:

    1. 点击App passwordsGoogle Account security page:

    1. 创建App,选择Mail 并命名:

    1. 记下App Password:

    然后在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)
    

    【讨论】:

    【解决方案3】:

    @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'])
    

    前四个参数是必需的:主题、消息、发件人电子邮件和收件人列表。

    【讨论】:

      【解决方案4】:
      1. 在 gmail 设置中启用 pop3。
      2. 为此 django 应用程序创建应用程序专用密码。 (http://support.google.com/accounts/bin/answer.py?hl=en&answer=185833)

      【讨论】:

        【解决方案5】:

        我会避免使用 GMail。它适用于一些电子邮件,但在那之后,您可能会发现您的所有电子邮件都被拒绝或垃圾邮件罐头。我使用 Amazon 的“SES”服务和 Django-SES 来解决这个问题。

        【讨论】:

        • 最近开始使用 SendGrid 这也是一项很棒的服务。
        【解决方案6】:

        在 settings.py 中,使用 smtp 作为后端,不是控制台

        EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
        

        【讨论】:

          【解决方案7】:

          将以下最小设置放入服务器上的 settings.py 或 local_settings.py 文件中。

          EMAIL_HOST = 'localhost'
          EMAIL_PORT = 587
          EMAIL_USE_TLS = True
          

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

          您可以通过安装自己的邮件服务器来做到这一点:

          sudo apt-get install sendmail
          

          【讨论】:

          • 我用postfix代替sendmail,设置和上面一样,但是邮件没有发送。
          • @toothie 我遇到了同样的问题,直到我意识到我并没有在我的生产服务器上真正安装 postfix...
          【解决方案8】:

          首先在 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()
          

          【讨论】:

            猜你喜欢
            • 2019-07-10
            • 2014-10-26
            • 2012-05-20
            • 1970-01-01
            • 2013-06-07
            • 2018-12-09
            • 2013-03-07
            • 2013-07-20
            • 2012-04-26
            相关资源
            最近更新 更多