Adding a name to the “from” field in SendGrid in Node.js

本小妞迷上赌 提交于 2019-11-29 03:57:41

You can set the from parameter in a couple of ways:

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

or you can create an Email object and fill in the stuff on that:

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() { 
  // ... 
});

You might want to take a few minutes and go over the README document on the Github page. It has pretty detailed info on how to use the library and the various features it offers.

Updated example with the syntax used in the latest version of the Sendgrid Node.js library.

sendgrid.send({
  to: 'you@yourdomain.com',
  from: {
      email: 'example@example.com',
      name: 'Sender Name'
  },
  subject: 'Hello World',
  text: 'My first email through SendGrid'
});

If you are using the nodejs Helper library, use the following arguments:

from_email = new helper.Email("email@domain.com", "Email Name");
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!