繁体   English   中英

Django SendGrid如何在EmailMultiAlternatives邮件中传递unique_args object

[英]Django SendGrid how to pass unique_args in EmailMultiAlternatives mail object

SendGrid 提供了通过 email 传递unique_args的能力,以便在事件 webhook 中识别 email。 但问题是我无法弄清楚如何使用 Django 中的 email 发送这些 unique_args。这就是我目前正在尝试的方式:

from django.core.mail import EmailMultiAlternatives

header ={
  "unique_args": {
    "customerAccountNumber": "55555",
    "activationAttempt": "1",
    "New Argument 1": "New Value 1",
    "New Argument 2": "New Value 2",
    "New Argument 3": "New Value 3",
    "New Argument 4": "New Value 4"
  }
}

subject, from_email, to = 'hello', 'EXAMPLE@FROM.com', 'EXAMPLE@TO.NET'
text_content = 'This is an important message.'
msg = EmailMultiAlternatives(
subject,
text_content,
from_email,
[to,],
headers=header,
)
msg.send()

好的,所以最后我找到了解决方案。 最后,我想出了如何使用 Django EmailMultiAlternatives 将 unique_args 发送到 SendGrid。 这是我的工作解决方案:

    from django.core.mail import EmailMultiAlternatives
    from smtpapi import SMTPAPIHeader
    header = SMTPAPIHeader()
    header.set_unique_args({'customerAccountNumber': '12345','activationAttempt': '1'})

    subject, from_email, to = 'hello', 'EXAMPLE@FROM.com', 'EXAMPLE@TO.NET'
    text_content = 'This is an important message.'
    msg = EmailMultiAlternatives(
        subject,
        text_content,
        from_email,
        [to,],
        headers={'X-SMTPAPI': header.json_string()},
    )
    msg.send()

你还需要安装一个smtpapi package by pip install smtpapi

    from django.core.mail import EmailMultiAlternatives
    from smtpapi import SMTPAPIHeader
    smtp_header = SMTPAPIHeader()
    smtp_header.set_unique_args({'customerAccountNumber': '12345','activationAttempt': '1'})

    subject, from_email, to = 'hello', 'hello@FROM.com', 'AABC@TO.NET'
    text_content = 'This is message.'
    msg = EmailMultiAlternatives(
        subject,
        text_content,
        from_email,
        [to,],
        headers={'X-SMTPAPI': smtp_header.json_string()},
    )
    msg.send()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM