微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决Django SendGrid如何在EmailMultiAlternatives邮件对象中传递unique_args

SendGrid提供了将unique_args与电子邮件一起传递的功能,以便在Event webhook.中标识电子邮件 但是问题是我无法弄清楚如何用Django中的电子邮件发送这些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,[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,[to,],headers={'X-SMTPAPI': header.json_string()},)
    msg.send()

您还需要通过pip install smtpapi

安装smtpapi软件包 ,
    from django.core.mail import EmailMultiAlternatives
    from smtpapi import SMTPAPIHeader
    smtp_header = SMTPAPIHeader()
    smtp_header.set_unique_args({'customerAccountNumber': '12345','hello@FROM.com','AABC@TO.NET'
    text_content = 'This is message.'
    msg = EmailMultiAlternatives(
        subject,headers={'X-SMTPAPI': smtp_header.json_string()},)
    msg.send()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。