我想使用SendGrid API在“发件人”字段中添加一个名称,但是我不知道该怎么做。 我尝试将sendgrid.send
中的“from”参数设置为Name <example@example.com>
,但是没有起作用。谢谢。
使用最新版本的Sendgrid Node.js库中使用的语法更新示例。
sendgrid.send({
to: 'you@yourdomain.com',
from: {
email: 'example@example.com',
name: 'Sender Name'
},
subject: 'Hello World',
text: 'My first email through SendGrid'
});
尽管更新的用例不包括from
键
https://github.com/sendgrid/sendgrid-nodejs/blob/master/docs/use-cases/flexible-address-fields.md
这个对我有效
to: 'user@userdomain.com',
from: {
name: 'Sender'
email: 'me@mydomain.com',
},
subject: 'Hello World',
html: `<html><p>Hello World</p></html>`
});
您可以通过以下几种方式设置“from”参数:
var SendGrid = require('sendgrid').SendGrid;
var sendgrid = new SendGrid(user, key);
sendgrid.send({
to: 'you@yourdomain.com',
from: 'example@example.com', // Note that we set the `from` parameter here
fromname: 'Name', // We set the `fromname` parameter here
subject: 'Hello World',
text: 'My first email through SendGrid'
}, function(success, message) {
if (!success) {
console.log(message);
}
});
或者您可以创建一个 Email
对象并填写相关内容:
var Email = require('sendgrid').Email;
var email = new Email({
to: 'you@yourdomain.com',
from: 'example@example.com',
fromname: 'Name',
subject: 'What was Wenger thinking sending Walcott on that early?',
text: 'Did you see that ludicrous display last night?'
});
sendgrid.send(email, function() {
// ...
});
你可能想花几分钟时间浏览Github页面上的README文档。它对如何使用库以及提供的各种功能有相当详细的信息。
如果您正在使用nodejs助手库,请使用以下参数:
from_email = new helper.Email("email@domain.com", "Email Name");
从GitHub上的节点库中,您可以使用以下任一方法来设置发送电子邮件和名称:
from: {
name: 'Name Here',
email: 'email here'
}
from: "Cool Name <some@email.com>"
对于strapijs用户,在插件中您可以像下面这样进行更改
module.exports = ({ env }) => ({
// ...
email: {
config: {
provider: 'sendgrid',
providerOptions: {
apiKey: env('SENDGRID_API_KEY'),
},
settings: {
defaultFrom: 'The Name You Want <myemail@protonmail.com>',
defaultReplyTo: 'The Name You Want <myemail@protonmail.com>',
},
},
},
// ...
});