【问题标题】:Django SendGrid how to pass unique_args in EmailMultiAlternatives mail objectDjango SendGrid 如何在 EmailMultiAlternatives 邮件对象中传递 unique_args
【发布时间】:2020-10-28 15:01:04
【问题描述】:

SendGrid 提供了通过电子邮件传递unique_args 的功能,以便识别Event webhook. 中的电子邮件 但问题是我无法弄清楚如何在 Django 中使用电子邮件发送这些 un​​ique_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 sendgrid django-email


    【解决方案1】:

    好的,所以我终于找到了解决方案。 最后,我想出了如何使用 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()
    

    你还需要通过pip install smtpapi安装一个smtpapi包

    【讨论】:

      【解决方案2】:
          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()
      

      【讨论】:

        猜你喜欢
        • 2016-08-17
        • 2012-02-14
        • 1970-01-01
        • 2019-11-11
        • 1970-01-01
        • 2014-10-10
        • 1970-01-01
        • 2013-08-05
        • 1970-01-01
        相关资源
        最近更新 更多