python - Django + uWSGI + 发送电子邮件的神秘问题

标签 python django uwsgi amazon-ses django-email

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


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

/project/app/utils.py

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

好的,现在问题是:

在本地主机中:

  • 从 View 发送电子邮件有效
  • 从 django shell 发送电子邮件有效

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

  • 从 View 发送电子邮件不起作用
  • 从 django shell 发送电子邮件有效

localhost 中的 Django shell 输出:

# 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 输出:

# 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) 中,但它是什么?我没有什么想法。

你呢?

最佳答案

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

关于python - Django + uWSGI + 发送电子邮件的神秘问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8549371/

相关文章:

python - uwsgi_param 和 proxy_set_header 的区别

python - matplotlib:在样式表中设置标题颜色

python - 在 django 中需要帮助与 memcached 和 friend 一起进行中级/高级缓存

python - UWSGI 杀死 worker 的速度太快了

python - 不用activate使用anaconda环境? (例如在 Crontab 中)

python - 畅达构建错误 : No matching distribution found for pytest-runner

来自另一个文件/模块的 Python 模拟补丁

python - 是否可以在中间件中过滤掉一些数据库行?

python - 为没有多对多关系的 friend 创建架构?

python - uwsgi 的构建轮失败