【问题标题】:Django send_mail through gmail very slowDjango通过gmail发送邮件非常慢
【发布时间】:2015-10-18 06:00:23
【问题描述】:

当我尝试通过 ./manage.py shell 发送时,发送一封电子邮件需要几分钟时间。当我在浏览器中提交表单后尝试发送用户验证电子邮件时,浏览器超时并显示 504,但最终发送了电子邮件。会发生什么?

settings.py

...
EMAIL_HOST = 'smtp.gmail.com'                                                   
EMAIL_HOST_USER = 'myemail@gmail.com'                                      
EMAIL_PORT = 587                                                                
EMAIL_USE_TLS = True                                                            
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER                                            
EMAIL_HOST_PASSWORD = os.environ.get('PASSWORD')   
...

views.py

class SignUpView(CreateView):                                                   
    model = User                                                                
    template_name = 'eventMap/register.html'                                    
    form_class = RegistrationForm                                               
    success_url="/"                                                             

    def form_valid(self, form):                                                 
                form.save()                                                     
                username = form.cleaned_data['username']                        
                email = form.cleaned_data['email']                              
                salt = hashlib.sha1(str(random.random())).hexdigest()[:5]          
                activation_key = hashlib.sha1(salt+email).hexdigest()           
                key_expires = datetime.datetime.today() + datetime.timedelta(2) 

                #Get user by username                                           
                user=User.objects.get(username=username)                        

                # Create and save user profile                                  
                new_profile = UserProfile(user=user, activation_key=activation_key,
                        key_expires=key_expires)                                
                new_profile.save()                                              

                # Send email with activation key                                
                email_subject = 'Account confirmation'                          
                email_body = "Hey %s, thanks for signing up. To activate your account, click this link within \
                48hours http://mywebsite.com/accounts/confirm/%s" % (username, activation_key)

                send_mail(email_subject, email_body, 'myemail@gmail.com',  
                        ['mytestemail@gmail.com'], fail_silently=False)   

                return super(SignUpView, self).form_valid(form) 

我在这篇文章中看到了类似的内容,但日志中没有提到任何关于不合格主机名等的内容 /var/log/mail.log

Jul 27 16:26:04 django postfix/qmgr[5975]: CAF7C1226F2: from=<>, size=3063, nrcpt=1 (queue active)
Jul 27 16:26:34 django postfix/smtp[12874]: connect to example.com[2606:2800:220:1:248:1893:25c8:1946]:25: Connection timed out
Jul 27 16:27:04 django postfix/smtp[12874]: connect to example.com[93.184.216.34]:25: Connection timed out
Jul 27 16:27:04 django postfix/smtp[12874]: CAF7C1226F2: to=<myemail@example.com>, relay=none, delay=368178, delays=368118/0.02/60/0, dsn=4.4.1, status=deferred (connect to example.com[93.184.216.34]:25: Connection timed out)

【问题讨论】:

  • 我也遇到过这个问题。使用来自 django 1.6 的代码库,电子邮件会在几秒钟内发送,django 1.8 上的相同代码库需要 1-5 分钟。我决定只打开一个新线程来解决这个问题。这是我正在使用的代码github.com/ui/django_asynchronous_send_mail

标签: python django email smtp gmail


【解决方案1】:

对我来说,电子邮件托管在 ScalaHosting 上。 使用 Telenet 测试连接表明端口 26 非常快。在我使用非常慢的 25 端口之前。

我建议使用 telnet 测试您的 SMTP 服务器,以选择正确且快速的端口。

示例:

telnet mx-s2.vivawebhost.com 26

【讨论】:

    【解决方案2】:

    我有一个类似的情况,延迟和超时是由系统尝试通过 IPv6 连接到 gmail 引起的。

    这个thread 有助于揭开谜团。

    TLDR:

    在主机上尝试使用 telnet 连接到 gmail。

    telnet smtp.gmail.com 587
    

    如果它首先尝试通过 IPv6 连接(并阻塞几分钟),然后在 IPv4 上成功连接,那么这很可能是您的问题。

    解决办法:

    import socket
    EMAIL_HOST = socket.gethostbyname('smtp.gmail.com')
    

    这可确保 python 从一开始就通过 IPv4 连接到 gmail。

    更好的解决方案:

    找出您的主机无法通过 IPv6 连接到 gmail 的原因并解决该问题。也许调整防火墙规则以拒绝数据包而不是丢弃它们。

    【讨论】:

    • 这个答案需要更多的学分。感谢您的帮助
    • 天哪,你救了我的命。我希望我能给你的不仅仅是一个赞成票......这值得更多的学分是的。
    • 顺便说一句,谷歌工作人员告诉这可能是由于缺乏 SPF 记录造成的。您可能也想检查一下。
    【解决方案3】:

    您可能不想通过 gmail 发送电子邮件。对于像 Gmail 这样的标准 smtp 服务,这并不是真正的预期用途。

    您是否尝试过将电子邮件通知配置为通过像 Mandrill 这样的邮件分发服务开始运行?

    有一些 Django 应用程序(例如 Djrill)使用标准 Django 电子邮件函数(例如 send_mail())“正常工作”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-24
      • 1970-01-01
      • 2016-08-31
      • 1970-01-01
      • 1970-01-01
      • 2012-01-07
      • 2023-03-19
      相关资源
      最近更新 更多