1

i'm using the following code to send two specific emails, one for the reservation client and other for me:

sendgrid.send({
        to:         "[email protected]",
        from:       "[email protected]",
        subject:    "Register confirmation",
        html:           "Some HTML",
    },
    function(err, json) {
        if (err) {
            return console.error(err);
        } 
        else {
            next();
        }
});

sendgrid.send({
        to:         "[email protected]",
        from:       "[email protected]",
        subject:    "NEW RESERVATION!",
        html:       "SOME TEXT OR HTML",
    },
    function(err, json) {
        if (err) {
            return console.error(err);
        } 
        else {
            next();
        }
});

Can i improve this? There is some much duplication.

2
  • 2
    A wrapper is about all i can think of, that would manage the return of the promise and whatnot, something like send_message(params); Commented Jan 11, 2015 at 2:34
  • Sendgrid API V3 Hope this helps stackoverflow.com/a/50479562/2392211 Commented May 23, 2018 at 3:59

2 Answers 2

2

You could specify yourself as a recipient and you would get the exact email your client is receiving. If you use the SMTPAPI, the user wouldn't see your email address and neither would you see theirs. To use that in the Node.js lib, you can do so this way:

var sendgrid = require('sendgrid')('api_user', 'api_pwd');
var email = sendgrid.Email();
email.smtpapi.addTo('[email protected]');
email.smtpapi.addTo('[email protected]');
email.setSubject('Register confirmation');
email.setHtml('booking numbah');
email.setFrom('[email protected]');
sendgrid.send(email, function(err, json) {
   console.log(arguments);
});

Hope that helps!

Sign up to request clarification or add additional context in comments.

Comments

2

Just replace the API key you get from https://app.sendgrid.com/settings/api_keys. (Create API Key)

..and install @sendgrid/mail package of course.

npm i @sendgrid/mail --save

const sgMail = require("@sendgrid/mail");

const SENGRID_API_KEY = 'Here_Paste_Your_Key'; 
sgMail.setApiKey(SENGRID_API_KEY);

const msg = {
    to: '[email protected]',
    from: '[email protected]',
    subject: "Test Subject",
    text: 'Hello World!',
    html: '<strong>Hello World!</strong>',
};

sgMail.send(msg, function(err, info) {
    if (err) {
        console.log("Email Not Sent.");
    } else {
        console.log("Email Sent Success.");
    }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.