Reputation:
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.
Upvotes: 1
Views: 1402
Reputation: 608
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.");
}
});
Upvotes: 2
Reputation: 116
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!
Upvotes: 2