将名称添加到Node.js中SendGrid中的"from"字段

Ian*_*nao 11 node.js sendgrid

我想使用SendGrid API为我的"from"字段添加一个名称,但我不知道如何做到这一点.我尝试将"from"参数设置为sendgrid.send,Name <example@example.com>但这不起作用.谢谢.

Inc*_*tor 29

使用最新版本的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'
});
Run Code Online (Sandbox Code Playgroud)

  • 这应该是 Sendgrid API V3 可接受的答案 (3认同)
  • 我在这里找到了文档 https://github.com/sendgrid/sendgrid-nodejs/blob/master/use-cases/flexible-address-fields.md (2认同)

Swi*_*ift 13

您可以通过以下几种方式设置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);
  }
});
Run Code Online (Sandbox Code Playgroud)

或者你可以创建一个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() { 
  // ... 
});
Run Code Online (Sandbox Code Playgroud)

您可能需要花几分钟时间浏览Github页面上的README文档.它有关于如何使用库及其提供的各种功能的非常详细的信息.

  • 这不再起作用了.请参阅下面的@incinerator答案. (4认同)
  • 这不再起作用了。看 /sf/answers/3353220181/ 答案。 (3认同)

小智 10

尽管更新的用例不包括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>`
});
Run Code Online (Sandbox Code Playgroud)


Bir*_*ick 8

如果您使用的是nodejs Helper库,请使用以下参数:

from_email = new helper.Email("email@domain.com", "Email Name");
Run Code Online (Sandbox Code Playgroud)


Bri*_*ink 6

从节点库上的 github 您可以使用以下任一方法从电子邮件和姓名发送

from: {
   name: 'Name Here',
   email: 'email here'
}
Run Code Online (Sandbox Code Playgroud)

或者

from: "Cool Name <some@email.com>"
Run Code Online (Sandbox Code Playgroud)