Sendgrid-nodejs: Send a Single Email to a Single Recipient

Created on 6 Oct 2016  ·  4Comments  ·  Source: sendgrid/sendgrid-nodejs

Acceptance Criteria:

  • A email object that represents the response body payload
  • A mail object that handles sending email objects, data validation and error handling

Reference:

hacktoberfest help wanted community enhancement

Most helpful comment

Hello Pablo,

First, I suggest you review the entire "Mail Helper Enhancement (v3 mail/send)" project.

We want to refactor the mail helper so that it makes executing the use cases defined in this project as simple as possible.

For example now our hello world to send a single email looks like this:

var helper = require('sendgrid').mail;
var from_email = new helper.Email('[email protected]');
var to_email = new helper.Email('[email protected]');
var subject = 'Hello World from the SendGrid Node.js Library!';
var content = new helper.Content('text/plain', 'Hello, Email!');
var mail = new helper.Mail(from_email, subject, to_email, content);

var sg = require('sendgrid')(process.env.SENDGRID_API_KEY);
var request = sg.emptyRequest({
  method: 'POST',
  path: '/v3/mail/send',
  body: mail.toJSON(),
});

sg.API(request, function(error, response) {
  console.log(response.statusCode);
  console.log(response.body);
  console.log(response.headers);
});

One way to improve it could be:

var mail = require('sendgrid').mail;
mail.AddFromEmail('[email protected]');
mail.AddToEmail('[email protected]');
mail.AddSubject('Hello World from the SendGrid Node.js Library!');
mail.AddContent('text/plain', 'Hello Email!');

var sendgrid = require('sendgrid')(process.env.SENDGRID_API_KEY);
sendgrid.send(mail, function(error, response) {
  if(error) {
      console.log(error.message);
      console.log(error.response.statusCode);
      console.log(error.response.body);
      console.log(error.response.headers);
  } else {
    console.log(response);
  }
});

All 4 comments

Hi @thinkingserious!

Trying to find my fourth PR for this year #hacktoberfest I've seen those exclusive pins and I would like to give it a go.

As I've never used Sendgrid before I'm a bit lost on where to start and understanding the issue. Is about extending the mail helper? Documenting how to send a single mail? I would love some clarifications.

Cheers, Pablo.

Hello Pablo,

First, I suggest you review the entire "Mail Helper Enhancement (v3 mail/send)" project.

We want to refactor the mail helper so that it makes executing the use cases defined in this project as simple as possible.

For example now our hello world to send a single email looks like this:

var helper = require('sendgrid').mail;
var from_email = new helper.Email('[email protected]');
var to_email = new helper.Email('[email protected]');
var subject = 'Hello World from the SendGrid Node.js Library!';
var content = new helper.Content('text/plain', 'Hello, Email!');
var mail = new helper.Mail(from_email, subject, to_email, content);

var sg = require('sendgrid')(process.env.SENDGRID_API_KEY);
var request = sg.emptyRequest({
  method: 'POST',
  path: '/v3/mail/send',
  body: mail.toJSON(),
});

sg.API(request, function(error, response) {
  console.log(response.statusCode);
  console.log(response.body);
  console.log(response.headers);
});

One way to improve it could be:

var mail = require('sendgrid').mail;
mail.AddFromEmail('[email protected]');
mail.AddToEmail('[email protected]');
mail.AddSubject('Hello World from the SendGrid Node.js Library!');
mail.AddContent('text/plain', 'Hello Email!');

var sendgrid = require('sendgrid')(process.env.SENDGRID_API_KEY);
sendgrid.send(mail, function(error, response) {
  if(error) {
      console.log(error.message);
      console.log(error.response.statusCode);
      console.log(error.response.body);
      console.log(error.response.headers);
  } else {
    console.log(response);
  }
});

Ok, so it's a complete refactor. I'll try to find some time to give it a try.

In progress here: #378

Was this page helpful?
0 / 5 - 0 ratings

Related issues

polkhovsky picture polkhovsky  ·  3Comments

Chrischuck picture Chrischuck  ·  3Comments

amlcodes picture amlcodes  ·  4Comments

mikemaccana picture mikemaccana  ·  4Comments

TobiahRex picture TobiahRex  ·  3Comments