1

約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

さて、問題は次のとおりです。

ローカルホストで:

  • ビューからのメールの送信は機能します
  • djangoシェルからのメール送信は機能します

サーバーのデプロイアプリで:

  • ビューからメールを送信しても機能しません
  • djangoシェルからのメール送信は機能します

ローカルホストでのDjangoシェル出力:

# 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シェル出力:

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

同じコード、同じコミット。問題はおそらくmsg.send(self.fail_silently)直前の方法にprint "sent."ありますが、それは何ですか?アイデアがありません。

あなたも?

4

2 に答える 2

0

threadingアクティブ化の問題があるためuWSGI、これをアプリの uwsgi 構成に追加してみてください。

lazy = true

したがって、子ワーカーがフォークされた後にコードがロードされます。

于 2011-12-18T04:07:01.413 に答える