使用meteor触发sendgrid模板电子邮件

Abi*_*bal 5 sendgrid meteor sendgrid-templates

我正在使用sendgrid发送电子邮件.我想将模板作为电子邮件发送给用户.下面的代码只是发送简单的基于文本的电子邮件,而不是定义标题部分和使用模板ID.

if (Meteor.isServer) {
    Email.send({
        from: "from@mailinator.com",
        to: "abc@mail.com",
        subject: "Subject",
        text: "Here is some text",
        headers: {"X-SMTPAPI": {
            "filters" : {
                "template" : {
                    "settings" : {
                        "enable" : 1,
                        "Content-Type" : "text/html",
                        "template_id": "3fca3640-b47a-4f65-8693-1ba705b9e70e"
                    }
                }
            }
        }
        }



    });
}
Run Code Online (Sandbox Code Playgroud)

非常感谢您的帮助.

最好

小智 4

要发送 SendGrid 事务模板,您有不同的选择

1) 通过SendGrid SMPT API

在这种情况下,我们可以使用 Meteor 电子邮件包(正如您所尝试的那样)。

要添加流星电子邮件包,我们需要输入销售:

meteor add email
Run Code Online (Sandbox Code Playgroud)

在这种情况下,根据SendGrid 文档

text属性被替换到文本模板的<%body%>html中,并替换到HTML 模板的<%body%>中。如果该text属性存在,但不存在html,则生成的电子邮件将仅包含模板的文本版本,而不包含 HTML 版本。

因此,在您的代码中,您还需要提供http属性,仅此而已。

这可能是您的服务器代码

// Send via the SendGrid SMTP API, using meteor email package
Email.send({
  from: Meteor.settings.sendgrid.sender_email,
  to: userEmail,
  subject: "your template subject here",
  text: "template plain text here",
  html: "template body content here",
  headers: {
    'X-SMTPAPI': {
      "filters": {
        "templates": {
          "settings": {
            "enable": 1,
            "template_id": 'c040acdc-f938-422a-bf67-044f85f5aa03'
          }
        }
      }
    }
  }
});
Run Code Online (Sandbox Code Playgroud)

2) 通过 SendGrid Web API v3

您可以使用meteor http包来使用 SendGrid Web API v3(此处为文档)。在这种情况下我们可以使用 Meteor http 包。

要在 shell 中添加 Meteor http 包类型:

meteor add http
Run Code Online (Sandbox Code Playgroud)

然后在您的服务器代码中您可以使用

meteor add email
Run Code Online (Sandbox Code Playgroud)