python - Django send_mail 通过 gmail 很慢

标签 python django email smtp gmail

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

设置.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')   
...

View .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)

最佳答案

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

thread对揭开谜团很有用。

简单描述:

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

telnet smtp.gmail.com 587

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

解决方法:

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

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

更好的解决方案:

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

关于python - Django send_mail 通过 gmail 很慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31663454/

相关文章:

python - 在python中交换字节的有效方法

python - 如何使用 Corpus.slice 添加带有日期的列

Django + PostgreSQL : creating a database (what privileges to grant)

delphi - 如何在代码中将自定义应用程序设置为 Windows 中的默认电子邮件客户端

python - 理解装饰器函数中的作用域和 *args 和 **kwargs

java - 为什么 java/javascript/python 强制在方法名称后使用 (),即使它不带参数?

django - 在 Nginx 后面通过 HTTPS 访问 Django Admin

Django 1.2 - Pb 在模板中带有表单(WSGIRequest)

php - 空白多部分电子邮件

facebook - 如何获取 Facebook 好友列表电子邮件地址?