44

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.

6 Answers 6

135
Answer recommended by Twilio Collective

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

sendgrid.send({
  to: '[email protected]',
  from: {
      email: '[email protected]',
      name: 'Sender Name'
  },
  subject: 'Hello World',
  text: 'My first email through SendGrid'
});
8
  • Thanks for that, same problem and I couldn't find the name field in the documentation, in-fact the docs aren't particularly easy to navigate around.
    – JMac
    Commented Feb 2, 2018 at 4:35
  • 1
    I agree. I have issued a pull request trying to clarify this in the documentation somewhat, but to no avail so far. Commented Feb 3, 2018 at 16:38
  • 2
    I found the docs here github.com/sendgrid/sendgrid-nodejs/blob/master/use-cases/…
    – ptimson
    Commented Jun 17, 2019 at 18:17
  • 3
    This should be the accepted answer for the Sendgrid API V3
    – Trev14
    Commented Sep 20, 2019 at 14:15
  • 1
    This works. The accepted answer no longer works.
    – Or Assayag
    Commented Dec 5, 2020 at 17:18
23

Although the updated use cases don't include the from key

https://github.com/sendgrid/sendgrid-nodejs/blob/master/docs/use-cases/flexible-address-fields.md

This one worked for me

  to: '[email protected]',
  from: {
      name: 'Sender',
      email: '[email protected]',
  },
  subject: 'Hello World',
  html: `<html><p>Hello World</p></html>`
});
2
  • 2
    Excellent answer!
    – hestellezg
    Commented May 26, 2020 at 5:43
  • missing a comma ---- name: 'Sender', (fixed it)
    – Fergus
    Commented Dec 30, 2023 at 5:18
18

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.

6
  • 1
    Thanks. I read that README and for some reason, I didn't see the fromname field in the docs while trying to find something about it. Next time I'll try Ctrl+F :) Commented May 26, 2013 at 19:49
  • what is user and key. I am thinking that is key is api key but what is user? that is username or any other Commented Nov 24, 2016 at 10:47
  • 13
    This doesn't work anymore. See @incinerator answer below.
    – Pier
    Commented Jan 16, 2018 at 16:07
  • 3
    This doesn't work anymore. Look stackoverflow.com/a/47903145/2803872 answer.
    – vohrahul
    Commented Jan 14, 2019 at 8:32
  • @Swift this doesn't work anymore. Can you please mention this in your answer? Commented Oct 18, 2020 at 9:56
11

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

from_email = new helper.Email("[email protected]", "Email Name");
6

from github on the node library you can use either of the methods below to have a send from email and name

from: {
   name: 'Name Here',
   email: 'email here'
}

or

from: "Cool Name <[email protected]>"
0

For strapijs users, in plugins you can change like below

module.exports = ({ env }) => ({
  // ...
  email: {
    config: {
      provider: 'sendgrid',
      providerOptions: {
        apiKey: env('SENDGRID_API_KEY'),
      },
      settings: {
        defaultFrom: 'The Name You Want <[email protected]>',
        defaultReplyTo: 'The Name You Want <[email protected]>',
      },
    },
  },
  // ...
});

Not the answer you're looking for? Browse other questions tagged or ask your own question.