我正在使用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"
}
}
}
}
}
});
}我们将非常感谢您的帮助。
最好的
发布于 2017-11-22 05:09:11
要发送SendGrid事务模板,您可以选择不同的选项
1)通过SendGrid SMPT API
在这种情况下,我们可以使用Meteor电子邮件包(正如您所尝试的)。
要添加meteor电子邮件包,我们需要键入sell:
meteor add email在这种情况下,根据SendGrid docs
将
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'
}
}
}
}
}
});2)通过SendGrid Web API v3
您可以通过meteor http包使用SendGrid Web API v3 (here docs)。在这种情况下,我们可以使用Meteor http包。
要添加Meteor http包,请在shell中键入:
meteor add http然后,在服务器代码中,您可以使用
// 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);https://stackoverflow.com/questions/47217847
复制相似问题