AttributeError: 'EmailMessage' 对象在 django app 中尝试使用 smtplib 发送电子邮件时没有属性 'get_all'

AttributeError: 'EmailMessage' object has no attribute 'get_all' when trying to send email with smtplib in django app

提问人:leauradmin 提问时间:11/10/2023 更新时间:11/10/2023 访问量:21

问:

我在我的 django Web 应用程序中使用芹菜来发送邮件。代码如下:

@shared_task
def envoi_email(subject,body,to):
    smtp_host = os.environ.get('EMAIL_HOST')
    smtp_port = os.environ.get('EMAIL_PORT')
    email_from = os.environ.get('EMAIL_USER')
    pwd = os.environ.get('EMAIL_PASSWORD')
    msg = EmailMessage(subject,body,email_from,[to])
    msg.content_subtype = "html"
    server = smtplib.SMTP(smtp_host)
    server.connect(smtp_host,smtp_port)
    server.ehlo()
    server.login(email_from, pwd)
    server.send_message(msg)
    server.quit()
    logging.info(f"Mail envoyé vers {to}")

我收到的消息:

[2023-11-10 07:07:58,508: ERROR/ForkPoolWorker-8] Task tasks.tasks.envoi_email[62cf0d90-ef4e-4f85-b7aa-bebd023289ae] raised unexpected: AttributeError("'EmailMessage' object has no attribute 'get_all'")
celery-worker  | Traceback (most recent call last):
celery-worker  |   File "/py/lib/python3.9/site-packages/celery/app/trace.py", line 477, in trace_task
celery-worker  |     R = retval = fun(*args, **kwargs)
celery-worker  |   File "/py/lib/python3.9/site-packages/celery/app/trace.py", line 760, in __protected_call__
celery-worker  |     return self.run(*args, **kwargs)
celery-worker  |   File "/electron/tasks/tasks.py", line 216, in envoi_email
celery-worker  |     server.send_message(msg)
celery-worker  |   File "/usr/local/lib/python3.9/smtplib.py", line 944, in send_message
celery-worker  |     resent = msg.get_all('Resent-Date')
celery-worker  | AttributeError: 'EmailMessage' object has no attribute 'get_all'

需要帮助

Django HTML 电子邮件 smtplib

评论


答: 暂无答案