Search code examples
node.jssendgrid

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


I want to add a name to my "from" field using the SendGrid API, but I do not know how to do this. I tried setting the "from" parameter in sendgrid.send to Name <[email protected]> but that didn't work. Thanks.


Solution

  • 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: '[email protected]',
      from: '[email protected]',  // 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: '[email protected]',
      from: '[email protected]',
      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.