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
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'
});
-
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.– JMacCommented Feb 2, 2018 at 4:35 -
1I 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
-
2I found the docs here github.com/sendgrid/sendgrid-nodejs/blob/master/use-cases/…– ptimsonCommented Jun 17, 2019 at 18:17
-
3
-
1
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
-
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.
-
1Thanks. 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
-
3This doesn't work anymore. Look stackoverflow.com/a/47903145/2803872 answer.– vohrahulCommented 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
If you are using the nodejs Helper library, use the following arguments:
from_email = new helper.Email("[email protected]", "Email Name");
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]>"
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]>',
},
},
},
// ...
});