39

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 <example@example.com> but that didn't work. Thanks.

Ian Macalinao
  • 1,608
  • 3
  • 20
  • 30

6 Answers6

119

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'
});
Incinerator
  • 2,589
  • 3
  • 23
  • 30
  • 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 Feb 02 '18 at 04:35
  • 1
    I agree. I have issued a [pull request](https://github.com/sendgrid/sendgrid-nodejs/pull/645) trying to clarify this in the documentation somewhat, but to no avail so far. – Incinerator Feb 03 '18 at 16:38
  • Don't know if it was like this a few months ago, but this is documented in [github](https://github.com/sendgrid/sendgrid-nodejs/blob/master/packages/mail/USE_CASES.md#flexible-email-address-fields) now – Jarvis Johnson Sep 19 '18 at 20:54
  • Update: the PR mentioned above has finally been merged, almost a year later. – Incinerator Oct 30 '18 at 18:47
  • 2
    I found the docs here https://github.com/sendgrid/sendgrid-nodejs/blob/master/use-cases/flexible-address-fields.md – ptimson Jun 17 '19 at 18:17
  • 3
    This should be the accepted answer for the Sendgrid API V3 – Trev14 Sep 20 '19 at 14:15
  • 1
    This works. The accepted answer no longer works. – Or Assayag Dec 05 '20 at 17:18
  • I think you need to set up a verified single sender for this to work. https://app.sendgrid.com/settings/sender_auth/senders – MadMac Mar 30 '21 at 22:31
17

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.

Swift
  • 13,118
  • 5
  • 56
  • 80
17

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: 'user@userdomain.com',
  from: {
      name: 'Sender'
      email: 'me@mydomain.com',
  },
  subject: 'Hello World',
  html: `<html><p>Hello World</p></html>`
});
11

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

from_email = new helper.Email("email@domain.com", "Email Name");
Biruel Rick
  • 776
  • 1
  • 8
  • 17
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 <some@email.com>"
BrinkDaDrink
  • 1,717
  • 2
  • 23
  • 32
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 <myemail@protonmail.com>',
        defaultReplyTo: 'The Name You Want <myemail@protonmail.com>',
      },
    },
  },
  // ...
});
zacknight95
  • 51
  • 1
  • 9