【问题标题】:Error sending email using Django with Celery使用带有 Celery 的 Django 发送电子邮件时出错
【发布时间】:2014-11-28 14:22:11
【问题描述】:

我正在尝试发送电子邮件,如果通过网络服务器执行以下操作,则效果很好。但是,当我尝试将任务发送到 Celery 时,我总是收到一个断言错误返回,告诉我“to”需要是一个列表或元组。

我不希望通过网络服务器发送电子邮件,因为它会减慢速度,所以如果有人能帮我解决这个问题,我将不胜感激。

from celery import Celery
from django.core.mail import send_mail, EmailMessage

app = Celery('tasks', backend='amqp', broker='amqp://')

@app.task
def send_mail_link():
    subject = 'Thanks'
    message = 'body'
    recipients = ['someemail@gmail.com']
    email = EmailMessage(subject=subject, body=message, from_email='info@example.com', to=recipients)
    email.send()

【问题讨论】:

    标签: python django rabbitmq celery


    【解决方案1】:

    我不是 100% 确定原因,但我做了一些更改,现在它可以正常工作了。

    我删除了 send_mail 的导入并将方法名称从 send_mail_link() 更改为 send_link()。我还重新启动了 Celery worker,现在一切正常。

    新代码是:

    from celery import Celery
    from django.core.mail import EmailMessage
    
    app = Celery('tasks', backend='amqp', broker='amqp://')
    
    @app.task
    def send_link():
        subject = 'Thanks'
        message = 'body'
        recipients = ['someemail@gmail.com']
        email = EmailMessage(subject=subject, body=message, from_email='info@example.com', to=recipients)
        email.send()
    

    希望将来有人会觉得这很有帮助。

    【讨论】:

    • 这在生产中不起作用。 Celery 因错误而反击。 ContentDisallowed: Refusing to deserialize untrusted content of type pickle (application/x-python-serialize)。我需要暂停!!!!
    • 请改用@app.task(serializer='json')。查看celery.readthedocs.org/en/latest/userguide/… 了解有关序列化程序的更多信息以及为什么默认不支持pickle。
    猜你喜欢
    • 1970-01-01
    • 2019-06-04
    • 2018-10-19
    • 1970-01-01
    • 2014-09-22
    • 2021-08-10
    • 2021-06-17
    • 2017-02-26
    • 1970-01-01
    相关资源
    最近更新 更多