【问题标题】:Send email through Zoho SMTP通过 Zoho SMTP 发送电子邮件
【发布时间】:2013-08-22 13:01:03
【问题描述】:

我正在尝试从基于 django 的网站发送电子邮件,但遇到了一些问题 - SMTPServerDisconnected 连接意外关闭 我的设置.py:

EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.zoho.com'
EMAIL_PORT = 465
EMAIL_HOST_USER = 'me@mydomain.com'
EMAIL_HOST_PASSWORD = 'XXXXXX'

我正在使用 django 1.5.1、python 2.7.3。谁能解决这个问题?

感谢您的帮助

【问题讨论】:

    标签: django email smtp zoho


    【解决方案1】:

    我在连接超时方面遇到了同样的问题。在我看来,默认 Django SMTP 库中的 SSL 套接字存在一些问题。在 Django 的开发版本中,有一个设置 EMAIL_USE_SSL = True 的选项,它允许使用 implicit TLS 连接(而不是 explicit,由 @ 指定987654325@)。一般前者(隐式)使用端口 465,而后者(显式)使用端口 587。参见Django docs -- 将开发版本与 1.5 版本进行比较。请注意,选项 EMAIL_USE_SSL 在 1.5 中不存在。

    因此,问题在于Zoho's default SMTP server uses port 465 and requires SSL,但EMAIL_USE_TLS 选项不满足此要求。 (旁注:也许尝试将此选项设置为False?我没有尝试过。)无论如何,我最好的猜测是这是一个特定于 Django 的问题,可能要到 1.7 才能解决。

    至于您的问题的解决方案:您绝对可以使用 Python (2.7.1) 的 smtplib 访问 Zoho 的 SMTP 服务器(参见下面的脚本)。所以,如果你想要一个稍微不优雅的修复,你可以走那条路。我已经在 Django 1.5.1 中测试过它,它就像一个魅力。

    这里是独立的 Python 脚本(可以改编为在 Django 项目中使用):

    import smtplib
    from email.mime.text import MIMEText
    
    # Define to/from
    sender = 'sender@example.com'
    recipient = 'recipient@example.com'
    
    # Create message
    msg = MIMEText("Message text")
    msg['Subject'] = "Sent from python"
    msg['From'] = sender
    msg['To'] = recipient
    
    # Create server object with SSL option
    server = smtplib.SMTP_SSL('smtp.zoho.com', 465)
    
    # Perform operations via server
    server.login('sender@example.com', 'password')
    server.sendmail(sender, [recipient], msg.as_string())
    server.quit()
    

    在将上述脚本插入您的 Web 项目之前,请尝试检查上述脚本是否使用您的 Zoho 凭据运行。祝你好运!

    【讨论】:

    • 这个方法对我有用 :) 如何在消息中发送 html 内容
    • 像魅力一样工作。补充:如果要给多人发邮件,改两行:msg['To'] = 'u1@ex.com;u2@ex.com'server.sendmail(sender, ['u1@ex.com', 'u2@ex.com'], msg.as_string())
    • @ZenOut 你将不得不使用 Multipart email ..see docs.python.org/2/library/email-examples.html#id5
    【解决方案2】:

    就我而言,我收到的是:

    SMTPServerDisconnected: Connection unexpectedly closed
    

    使用这些设置:

    EMAIL_PORT = 465
    EMAIL_BACKEND = 'django_smtp_ssl.SSLEmailBackend'
    EMAIL_USE_SSL = True
    EMAIL_HOST = 'smtp.zoho.com'
    EMAIL_HOST_USER = 'dio@streetbarz.com'
    EMAIL_HOST_PASSWORD = 'password'
    

    设置 server.set_debuglevel(1) 后,发现我的 DEFAULT_FROM_EMAIL 和 EMAIL_HOST_USER 不一样

    DEFAULT_FROM_EMAIL = "dio@streetbarz.com"
    

    添加解决了问题。

    【讨论】:

    • @diofeher 谢谢你!这也解决了我的问题!你应该得到一个拥抱、一杯咖啡、一杯啤酒或其他东西!
    • 这对我有用,只是在确保发件人地址相同之前,无论我多么努力......
    【解决方案3】:

    如果您想通过电子邮件获取错误报告,B.Welsh 的回答并不能解决问题。

    所以对于任何需要它的人:

    Zoho 的 TLS 端口为 587,如其 SMTP Server Configuration Page 中所定义。 该页面还指出,您不能使用与您正在使用的电子邮件不同的“发件人”,否则将无法通过。

    settings.py 中有您需要的代码,以便通过电子邮件获取错误报告:

    DEBUG = False
    TEMPLATE_DEBUG = DEBUG
    ADMINS = (('Yourname', 'youremail@yourdomain.com'),)
    SERVER_EMAIL = constants.SENDER_EMAIL
    
    EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
    EMAIL_HOST = 'smtp.zoho.com'
    EMAIL_PORT = 587
    EMAIL_USE_TLS = True
    EMAIL_HOST_USER = constants.SENDER_EMAIL
    EMAIL_HOST_PASSWORD = constants.EMAIL_PASSWORD
    

    【讨论】:

      【解决方案4】:

      我有一种使用 django 1.6.8 发送的方法。首先,您必须安装 GitHub 中提供的 django-smtp-ssl。运行代码:

      pip install django-smtp-ssl
      

      并将以下内容添加到您的 settings.py:

      EMAIL_BACKEND = 'django_smtp_ssl.SSLEmailBackend'   
      EMAIL_HOST = 'mail.example.com'   
      EMAIL_PORT = 465
      

      查看链接https://github.com/bancek/django-smtp-ssl

      【讨论】:

        【解决方案5】:

        我发现 Zoho 不喜欢标准的 django.core.mail.send_mail 方法。该问题似乎与内容类型有关。有多种方法可以解决这个问题,我的方法是切换到 EmailMessage,它具有更丰富的界面并允许您在标头中传递 Content-type。

        从这里切换:

        from django.core import mail
        mail.send_mail(subject='Hello',
                       message='Body goes here',
                       from_email='user@example.com',
                       recipient_list=['user@example.com'])
        

        到这里:

        from django.core.mail import EmailMessage
        email = EmailMessage(
            subject='Hello',
            body='Body goes here',
            from_email='user@example.com',
            to=['user@example.com'],
            reply_to=['user@example.com'],
            headers={'Content-Type': 'text/plain'},
        )
        email.send()
        

        其他 Zoho 邮件设置:

        EMAIL_HOST = 'smtp.zoho.com'
        EMAIL_HOST_USER = 'user@example.com'
        EMAIL_HOST_PASSWORD = 'password'
        EMAIL_PORT = 587
        EMAIL_USE_TLS = True
        EMAIL_USE_SSL = False
        

        这解决了我的 Zoho 邮件发送问题,并且与 django-yubin 等其他队列插件兼容。

        【讨论】:

          【解决方案6】:

          与问题有点无关,但请注意,Zoho Mail 在其免费计划中不再提供 IMAP/POP 支持。希望我可以通过这篇文章为你们中的一些人节省一些调试时间。

          ```
          FREE PLAN
          Up to 25 Users
          5GB* /User, 25MB Attachment Limit
          Webmail access only+. Single domain hosting.
          ```
          

          +IMAP/POP 支持仅适用于付费计划。

          https://www.zoho.com/workplace/pricing.html?src=zmail

          旧的免费计划(2018 年之前注册???)似乎仍然提供 IMAP/POP 支持

          来源:https://help.zoho.com/portal/community/topic/zoho-free-tier-pop-imap-activesync-no-longer-free

          【讨论】:

            【解决方案7】:

            根据这个link的讨论,我们还需要检查正确的smtp url。 就我而言,我使用的是 smtp.zoho.com,但正确的选择是 smtp.zoho.in。希望有帮助。登录 zoho 并查看域名 url 后可以发现。

            【讨论】:

              【解决方案8】:

              尝试 1 而不是 True:

              EMAIL_USE_TLS = 1
              EMAIL_PORT = 465
              EMAIL_HOST = 'smtp.zoho.com'
              EMAIL_HOST_USER = 'me@mydomain.com'
              EMAIL_HOST_PASSWORD = 'XXXXXX'
              

              或者尝试一个备用端口:

              EMAIL_USE_TLS = 1
              EMAIL_PORT = 587
              EMAIL_HOST = 'smtp.zoho.com'
              EMAIL_HOST_USER = 'me@mydomain.com'
              EMAIL_HOST_PASSWORD = 'XXXXXX'
              

              【讨论】:

                【解决方案9】:

                您的 stmp 电子邮件后端类可能是旧的。转到

                python/site-packages/django/core/mail/stmp.py

                文件并确保您有 USE_SSL 作为选项。如果没有,只需将整个文件替换为一个文件即可。干得好。在 ZOHO 为我工作。

                stmp.py file

                请原谅这个回复的格式不好,这是我对 SO...的第一个贡献......

                【讨论】:

                  【解决方案10】:

                  Zoho Mail 的 SMTP 配置设置 - TLS 使用端口 587 和 ssl 465。所以 如果您使用 EMAIL_USE_TLS = True,请使用EMAIL_PORT = 587

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 2014-01-21
                    • 2019-05-15
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2018-10-17
                    • 2013-11-28
                    • 2011-04-06
                    相关资源
                    最近更新 更多