Django + uWSGI +发送电子邮件的神秘问题 [英] Mysterious issue with Django + uWSGI + send email

查看:133
本文介绍了Django + uWSGI +发送电子邮件的神秘问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过大约1.5个小时的艰苦调试,我在这里给您写信.

I'm here to write you after about 1.5 hour of hard debugging.


首先,这些是感兴趣的文件:


First of all, these are the interested files:

from django.core.mail import EmailMultiAlternatives
import threading

class EmailThread(threading.Thread):
    def __init__(self, subject, body, from_email, recipient_list, fail_silently, html):
        self.subject = subject
        self.body = body
        self.recipient_list = recipient_list
        self.from_email = from_email
        self.fail_silently = fail_silently
        self.html = html
        threading.Thread.__init__(self)

    def run(self):
        msg = EmailMultiAlternatives(self.subject, self.body, self.from_email, self.recipient_list)
        if self.html:
            msg.attach_alternative(self.html, "text/html")
        print "sending message"
        msg.send(self.fail_silently)
        print "sent."

def send_html_mail(subject, body, from_email, recipient_list, fail_silently=False, html=None, *args, **kwargs):
    EmailThread(subject, body, from_email, [recipient_list], fail_silently, html).start()

/project/app/views.py

[...]
import utils

def my_view(request):
    [...]
    utils.send_html_mail('subject', '<h1>cool things.</h1>', 'Test <test@example.org>', 'my_email@example.org', html='<h1>cool things.</h1>')

/project/app/settings.py

# I use Amazon SES

[...]
EMAIL_BACKEND = 'backends.smtp.SSLEmailBackend'
EMAIL_HOST = 'amazon-server'
EMAIL_PORT = 465
EMAIL_HOST_USER = 'user'
EMAIL_HOST_PASSWORD = 'pass'
EMAIL_USE_TLS = True
[...]

/project/backends/smtp.py

# taken from https://gist.github.com/1486891

import smtplib

from django.core.mail.utils import DNS_NAME
from django.core.mail.backends.smtp import EmailBackend

class SSLEmailBackend(EmailBackend):
    def open(self):
        if self.connection:
            return False
        try:
            self.connection = smtplib.SMTP_SSL(self.host, self.port,
                                           local_hostname=DNS_NAME.get_fqdn())
            if self.username and self.password:
                self.connection.login(self.username, self.password)
            return True
        except:
            if not self.fail_silently:
                raise

好,现在是问题所在:

在本地主机中:

  • 从视图中发送电子邮件有效
  • 从django shell发送电子邮件工作
  • sending the email from the view works
  • sending the email from the django shell works

在我服务器上的部署应用中:

in the deploy app on my server:

  • 从视图中发送电子邮件没用
  • 从django shell发送电子邮件工作
  • sending the email from the view DOESN'T WORKS
  • sending the email from the django shell works

本地主机中的Django shell输出:

Django shell output in localhost:

# utils.send_html_mail('subject', '<h1>cool things.</h1>', 'Test <test@example.org>', 'my_email@example.org', html='<h1>cool things.</h1>')
sending email
sent.

部署应用中的Django shell输出:

Django shell output in deploy app:

# utils.send_html_mail('subject', '<h1>cool things.</h1>', 'Test <test@example.org>', 'my_email@example.org', html='<h1>cool things.</h1>')
sending email

相同的代码,相同的提交.问题出在print "sent."之前的方法msg.send(self.fail_silently)中,但这是什么?我没有想法.

Same code, same commit. The problem is in the method msg.send(self.fail_silently) just before the print "sent." probably, but what is it? I don't have ideas.

那你呢?

推荐答案

您是否已使用--enable-threads在uWSGI中启用了线程?

have you enabled threads in uWSGI with --enable-threads ?

这篇关于Django + uWSGI +发送电子邮件的神秘问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆