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

后端 未结 4 1746
粉色の甜心
粉色の甜心 2020-12-15 05:55

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 <

相关标签:
4条回答
  • 2020-12-15 06:04

    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>`
    });
    
    0 讨论(0)
  • 2020-12-15 06:08

    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.

    0 讨论(0)
  • 2020-12-15 06:11

    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'
    });
    
    0 讨论(0)
  • 2020-12-15 06:15

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

    from_email = new helper.Email("email@domain.com", "Email Name");
    
    0 讨论(0)
提交回复
热议问题