在Django

时间:2018-05-20 12:13:12

标签: python django python-3.x email django-email

我有一个博客帖子对象。创建博客文章后,我有一个发送电子邮件的信号。我想向多个订阅者发送电子邮件。 我使用谷歌邮件作为邮件服务器。 我在设置文件中输入了一些使用邮件的属性:

EMAIL_HOST = settings['EMAIL']['HOST']
EMAIL_PORT = settings['EMAIL']['PORT']
EMAIL_HOST_USER = settings['EMAIL']['USER']
EMAIL_HOST_PASSWORD = settings['EMAIL']['PASSWORD']
EMAIL_USE_TLS = True

发送邮件的功能。功能是一项芹菜任务。

@shared_task
def email_dispatch(heading, text):
    recipients = TheUser.objects.filter(subscription=True)

    for recipient in recipients:
        try:
            html_content = render_to_string('mails/email_dispatch.html', {'text': text})
            text_content = strip_tags(html_content)
            subject = '{}'.format(heading)

            email = EmailMultiAlternatives(subject, text_content, to=[recipient.id_user.email])
            email.attach_alternative(html_content, 'text/html')
            email.send()

            logger.info('Successful processed "{}", sent to: "{}"'.format(count_processed, recipient.id_user.username))

        except NoReverseMatch:
            logger.info('Unexpected username: "{}"'.format(recipient.id_user.username))

    logger.info('Email dispatching has been finished.')

乍一看,一切正常。当芹菜任务开始执行时,它会正确调度。但在收到大约80封电子邮件之后我收到了

  

smtplib.SMTPServerDisconnected:连接意外关闭

不幸的是,在这种情况下,我没有找到任何有关Google Mail限制的信息。也许我在Django方面设置错误或者在这种情况下使用了错误的函数。

感谢您的帮助。

0 个答案:

没有答案