0

这是我的 send_email 方法

class EmailTemplate(models.Model):
    type = models.PositiveSmallIntegerField(choices=_UserProfile.NOTIFICATIONS_TYPES.get_choices(), unique=True, db_index=True)
    subject = models.CharField(max_length=255)
    body = models.TextField()


    def __unicode__(self):
        return self.get_type_display()


    def send_email(self, context, to_email, use_thread=True):
        template = Template(self.body)
        c = Context(context) 
        html = template.render(c)

        sender = 'Jack <%s>' % settings.DEFAULT_FROM_EMAIL

        email_message = EmailMessage(self.subject, html, sender, to_email)
        email_message.content_subtype = 'html'

        if use_thread:
            t = threading.Thread(target=email_message.send, kwargs={'fail_silently': False})
            t.setDaemon(True)
            t.start()
        else:
            email_message.send()

这些是我的电子邮件设置

DEFAULT_FROM_EMAIL = 'Jack <info@jack.com>'
EMAIL_HOST = 'smtp.live.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'info@jack.com'
EMAIL_HOST_PASSWORD = 'password'

当我尝试发送电子邮件时,我收到以下错误

[Tue Jul 23 22:33:28 2013] [error] Exception in thread Thread-1:
[Tue Jul 23 22:33:28 2013] [error] Traceback (most recent call last):
[Tue Jul 23 22:33:28 2013] [error]   File "/usr/lib/python2.7/threading.py", line 552, in __bootstrap_inner
[Tue Jul 23 22:33:28 2013] [error]     self.run()
[Tue Jul 23 22:33:28 2013] [error]   File "/usr/lib/python2.7/threading.py", line 505, in run
[Tue Jul 23 22:33:28 2013] [error]     self.__target(*self.__args, **self.__kwargs)
[Tue Jul 23 22:33:28 2013] [error]   File "/home/jack/sites/universal_tutors/env/lib/python2.7/site-packages/django/core/mail/message.py", line 248, in send
[Tue Jul 23 22:33:28 2013] [error]     return self.get_connection(fail_silently).send_messages([self])
[Tue Jul 23 22:33:28 2013] [error]   File "/home/jack/sites/universal_tutors/env/lib/python2.7/site-packages/django/core/mail/backends/smtp.py", line 92, in send_messages
[Tue Jul 23 22:33:28 2013] [error]     sent = self._send(message)
[Tue Jul 23 22:33:28 2013] [error]   File "/home/jack/sites/universal_tutors/env/lib/python2.7/site-packages/django/core/mail/backends/smtp.py", line 110, in _send
[Tue Jul 23 22:33:28 2013] [error]     email_message.message().as_string())
[Tue Jul 23 22:33:28 2013] [error]   File "/usr/lib/python2.7/smtplib.py", line 720, in sendmail
[Tue Jul 23 22:33:28 2013] [error]     raise SMTPSenderRefused(code, resp, from_addr)
[Tue Jul 23 22:33:28 2013] [error] SMTPSenderRefused: (501, '5.5.4 Invalid Email address', 'Jack <Jack>')

当我使用 gmail smptp 设置时,它可以正常工作,但使用 microsoft mail 时出现错误。不知道我犯了什么错误。

4

1 回答 1

1

这是你的问题:

sender = 'Jack <%s>' % settings.DEFAULT_FROM_EMAIL

既然你有:

DEFAULT_FROM_EMAIL = 'Jack <info@jack.com>'

你最终sender成为:

Jack <Jack <info@jack.com>>

这不是一个有效的电子邮件地址。

于 2013-07-24T13:57:53.470 回答