使用meteor触发sendgrid模板电子邮件 [英] Trigger sendgrid template email using meteor

查看:80
本文介绍了使用meteor触发sendgrid模板电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using sendgrid to send an email. I want to send template as an email to users. Below code is just sending the simple text based email instead of defining the headers part and using the template 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"
                    }
                }
            }
        }
        }



    });
}

非常感谢您的帮助.

最佳

推荐答案

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

To send SendGrid transactional templates you have different options

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

In this case we can use Meteor email package (as you were trying).

要添加meteor电子邮件包,我们需要输入sell:

To add meteor email package we need to type in the sell:

meteor add email

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

In this case, according to SendGrid docs:

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

The text property is substituted into the <%body%> of the text template and html is substituted into the <%body%> of the HTML template. If the text property is present, but not html, then the resulting email will only contain the text version of the template, not the HTML version.

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

So in your code you need to provide http property too, that's all.

这可能是您的服务器代码:

// 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'
          }
        }
      }
    }
  }
});

2) 通过 SendGrid Web API v3

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

2) Via the SendGrid Web API v3

You can use meteor http package to use SendGrid Web API v3 (here docs). In this case we can use the Meteor http package.

在 shell 中添加 Meteor http 包类型:

To add Meteor http package type in the shell:

meteor add http

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

Then in your server code you can use

// Send via the SendGrid Web API v3, using meteor http package
var endpoint, options, result;

endpoint = 'https://api.sendgrid.com/v3/mail/send';

options = {
  headers: {
    "Authorization": `Bearer ${Meteor.settings.sendgrid.api_key}`,
    "Content-Type": "application/json"
  },
  data: {
    personalizations: [
      {
        to: [
          {
            email: userEmail
          }
        ],
        subject: 'the template subject'
      }
    ],
    from: {
      email: Meteor.settings.sendgrid.sender_email
    },
    content: [
      {
        type: "text/html",
        value: "your body content here"
      }
    ],
    template_id: 'c040acdc-f938-422a-bf67-044f85f5aa03'
  }
};

result = HTTP.post(endpoint, options);

这篇关于使用meteor触发sendgrid模板电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆