【问题标题】:how to use designed template in django using sendgrid API?如何使用 sendgrid API 在 django 中使用设计的模板?
【发布时间】:2015-07-20 09:06:42
【问题描述】:

我已将发送网格与我的 Django 应用程序集成,并且邮件也成功发送。但现在我想从我的 django 应用程序中发送带有设计模板的电子邮件。我也阅读了文档,但不知道如何以编程方式使用它。这是我第一次使用 send-grid。请任何人都可以帮助我找出如何从 django 应用程序发送发送网格模板的方式。

【问题讨论】:

  • 您可能想尝试模板服务,例如sendwithus。它们支持 Jinja 模板(几乎与 Django 相同),并且有一个Python API Client
  • @bvanvugt 模板表示在 sendgrid 帐户中创建的设计电子邮件模板,我必须获取并发送电子邮件。我也挖掘了 sendgrid 文档,但不知道如何使用。

标签: python django sendmail sendgrid


【解决方案1】:

您可以使用 SendGrid 的模板引擎在 SendGrid 中存储模板。然后在通过 SendGrid API 发送电子邮件时引用该模板 ID,您可以 see an example of that code in the sendgrid-python library

这是一个完整的示例,它使用了一个 SendGrid API 密钥(您可以通过 reading this guide 了解如何进行设置):

import sendgrid

sg = sendgrid.SendGridClient('sendgrid_apikey')

message = sendgrid.Mail()
message.add_to('John Doe <john@email.com>')
message.set_subject('Example')
message.set_html('Body')
message.set_text('Body')
message.set_from('Doe John <doe@email.com>')

# This next section is all to do with Template Engine

# You pass substitutions to your template like this
message.add_substitution('-thing_to_sub-', 'Hello! I am in a template!')

# Turn on the template option
message.add_filter('templates', 'enable', '1')

# Tell SendGrid which template to use
message.add_filter('templates', 'template_id', 'TEMPLATE-ALPHA-NUMERIC-ID')

# Get back a response and status
status, msg = sg.send(message)

【讨论】:

  • 谢谢@MartynDavies,我这样做了,邮件也发送到了收件人ID。但是模板没有显示,在邮件中我收到了这个错误。检测到的错误是:模板错误:'http_get 返回非 20x 响应:'404 Not Found' body='{"error": "active version not found"}'
  • 这意味着您没有在您的 SendGrid 帐户中设置具有指定 ID 的模板,或者您创建的模板尚未激活。您必须确保模板引擎中有一个活动模板,您可以在此处的概述中看到:sendgrid.com/docs/User_Guide/Templates/index.html
  • 非常感谢,现在完成了。我没有激活模板是我的错误。再次感谢
  • 我又面临一个问题,你能告诉我出了什么问题吗?现在电子邮件已发送到 TO 列表,但正在发生的替换值未显示在 TO 列表的其他电子邮件 ID 中。具有替换值的邮件仅发送到 TO 列表的第一个电子邮件 id,对于其他邮件不显示。我也在 sendgrid 社区问过问题。链接为:community.sendgrid.com/sendgrid/topics/…
  • @MegaBytes - 让我做一些测试,我会告诉你的。您在“收件人”字段中输入了多少收件人?
【解决方案2】:

首先需要 Sendgrid-Python 接口:

pip install sendgrid

然后试试这个:

import os
from sendgrid.helpers.mail import Mail
from sendgrid import SendGridAPIClient

FROM_EMAIL = 'Your_Name@SendGridTest.com'

# update to your dynamic template id from the UI
TEMPLATE_ID = 'd-d027f2806c894df38c59a9dec5460594'

# list of emails and preheader names, update with yours
TO_EMAILS = [('your_email@domain.com', 'Sourabh MbeforL'),]


def SendDynamic():
    message = Mail(
        from_email=FROM_EMAIL,
        to_emails=TO_EMAILS)

    # pass custom values for our HTML placeholders
    message.dynamic_template_data = {
        'subject': 'SendGrid Development',
        'place': 'New York City',
        'event': 'Twilio Signal'
    }
    message.template_id = TEMPLATE_ID
    try:
        sg = SendGridAPIClient(os.environ.get('YOUR_SENDGRID_API_KEY')) ## (^_^) This face is just to grab your attention for api key needed
        response = sg.send(message)
        code, body, headers = response.status_code, response.body, response.headers
        print(f"Response code: {code}")
        print(f"Response headers: {headers}")
        print(f"Response body: {body}")
        print("Dynamic Messages Sent!")
    except Exception as e:
        print("Error: {0}".format(e))
    return str(response.status_code)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-08
    • 1970-01-01
    • 2020-04-28
    • 2015-09-07
    • 2012-10-14
    • 1970-01-01
    • 2011-06-17
    • 1970-01-01
    相关资源
    最近更新 更多