【问题标题】:SENDGRID: send an email using transactional template using python?SENDGRID:使用 python 使用事务模板发送电子邮件?
【发布时间】:2018-05-29 07:19:56
【问题描述】:

我正在尝试使用事务模板 sendgrid 发送电子邮件。

我可以发送一封简单的邮件。

from_email = Email("useremail@gmail.com")
subject = "Welcome"
to_email = Email("toemail@gmail.com")
content = ("text/plane","Text here")
mail = Mail(from_email, subject, to_email, content)

我创建了一个模板,我想用它来发送电子邮件。我该怎么做?

我正在使用 template_id 参数并通过 Mail(),但它不起作用。

template_id = "13b8f94f-bcae-4ec6-b752-70d6cb59f932"

我检查了具有 self._template_id 参数的类 Mail(object)。 Mail() 类中的字段如下:

if self.template_id is not None:
    mail["template_id"] = self.template_id

我在这里错过了什么?

我只想使用我创建的模板发送邮件。

【问题讨论】:

    标签: python sendgrid sendgrid-templates


    【解决方案1】:

    您不能将其作为参数发送。您可以通过以下方式将其设置为普通设置器。

    mail.template_id = "13b8f94f-bcae-4ec6-b752-70d6cb59f932"
    

    您可以在 sendgrid 包的mail_example.py 文件中找到相同的实现

    使用替代/个性化

    #add this code to your method where you have initialized Mail() object
    personalization = get_mock_personalization_dict()
    mail.add_personalization(build_personalization(personalization))
    mail.add_personalization(build_personalization(personalization))
    
    #Example of a Personalization Object
    def get_mock_personalization_dict():
        mock_pers = dict()
        mock_pers['substitutions'] = [Substitution("%name%", "Example User"),
                                  Substitution("%city%", "Denver")]
    
    #Updates the mail object with personalization variable
    def build_personalization(personalization):
        for substitution in personalization['substitutions']:
             personalization.add_substitution(substitution)
    

    【讨论】:

    • 我在上面试过了,但是我保存模板的格式,我没有收到相同格式的邮件。
    • 您可能需要正确编辑模板。在本地尝试模板,然后他们将 HTML 粘贴到 Sendgrid 中。如果您使用 Outlook 来检查您的邮件,那么您需要将您的邮件放在<table><tr><td> 结构中,以便正确查看邮件。
    • 另外,你能告诉我如何在我的模板中添加用户名(使用替换)吗?
    • 我已经更新了上面的代码以使用替换。示例代码已从 mail_example.py 复制。你可以在那里找到所有的细节。
    • 用于个性化替换['substitutions']: TypeError: 'NoneType' object has no attribute 'getitem' 我添加了代码,遇到了上述错误。
    【解决方案2】:

    如果您使用的是最新的 sendgrid python 库 (~6.1.0),您需要遵循他们的 github 自述文件中的文档。https://github.com/sendgrid/sendgrid-python/blob/master/use_cases/transactional_templates.md

    您需要通过 message.dynamic_template_data 将动态数据作为 python dict 传递。还可以使用 sendgrid 邮件助手类中的 From、To、Subject 对象。

        message.dynamic_template_data = 
        {
            'subject': 'Testing Templates',
            'name': 'Some One',
            'city': 'Denver' 
        }

    这里是完整的代码sn-p..

    import os 
    import json 
    from sendgrid import SendGridAPIClient 
    from sendgrid.helpers.mail import Mail
    
    message = Mail(
        from_email='from_email@example.com',
        to_emails='to@example.com',
        html_content='<strong>and easy to do anywhere, even with Python</strong>') message.dynamic_template_data = {
        'subject': 'Testing Templates',
        'name': 'Some One',
        'city': 'Denver' } 
    message.template_id = 'd-f43daeeaef504760851f727007e0b5d0' 
    try:
        sendgrid_client = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
        response = sendgrid_client.send(message)
        print(response.status_code)
        print(response.body)
        print(response.headers) 
    except Exception as e:
        print(e.message)

    【讨论】:

      猜你喜欢
      • 2016-07-12
      • 2018-11-08
      • 1970-01-01
      • 2021-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-14
      • 2015-05-15
      相关资源
      最近更新 更多