React + NodeJS - Nodemailer Google Cloud Functions
I am trying to make a reactJS app, and would like to when people ended up filling the form to send some emails.
for that, I have firstly created a sendGrid function but did not like so much the delay and the email templates (and the fact that is more than 100 mails per day.
then I read that I could use Nodemailer + (or without) express + firebase and google cloud functions. I wrote this function:
exports.mailSun = functions.https.onRequest((req, res) => {
const { name, dest } = req.query;
var transporter = nodemailer.createTransport({
service: 'Gmail',
host: 'smtp.gmail.com',
port: 465,
secure: true,
auth: {
user: '[email protected]',
pass: 'xxx'
}
});
const mailOptions = {
from: '[email protected]', // sender address
to: '[email protected]', // list of receivers
subject: 'Elavtal', // Subject line
html: '<p>Grattis för ditt nya avtal. Elbolag kommer att kontakta dig inom kort.</p>'// plain text body
};
return transporter.sendMail(mailOptions, function (err, info) {
if(err)
console.log(err)
else
console.log(info);
});
});
then I tried
exports.sendMail= functions.https.onRequest((req, res) => {
cors(req, res, () => {
// getting dest email by query string
const { name, dest } = req.query;
var transporter = nodemailer.createTransport({
service: 'Gmail',
host: "smtp.gmail.com",
port: 465,
secure: true,
auth: {
user: '[email protected]',
pass: 'xxx'
}
});
const mailOptions = {
from: 'Sunny <[email protected]>', // Something like: Jane Doe <[email protected]>
to: '[email protected]',
subject: 'nytt Avtal', // email subject
text: `<p Ni har fått ett nytt avtal. Logga in på Sunny för att se kunddetaljer.</p> ` // email content in HTML
};
// returning result
return transporter.sendMail(mailOptions, (erro, info) => {
if(erro){
return res.send(erro.toString());
}
return res.send('Sent');
});
});
});
can someone help me with what is wrong?
Answer
Instead of using the callback version of the sendMail()
method, use the Promise version, as follows:
exports.mailSun = functions.https.onRequest((req, res) => {
const { name, dest } = req.query;
var transporter = nodemailer.createTransport({
service: 'Gmail',
host: 'smtp.gmail.com',
port: 465,
secure: true,
auth: {
user: '[email protected]',
pass: 'xxx'
}
});
const mailOptions = {
from: '[email protected]', // sender address
to: '[email protected]', // list of receivers
subject: 'Elavtal', // Subject line
html: '<p>Grattis för ditt nya avtal. Elbolag kommer att kontakta dig inom kort.</p>'// plain text body
};
return transporter.sendMail(mailOptions)
.then(info => {
res.send("email sent");
})
.catch(err => {
console.log(err);
res.status(500).send("Error sending mail")
})
});
See the doc
If callback argument is not set then the method returns a Promise object. Nodemailer itself does not use Promises internally but it wraps the return into a Promise for convenience. If callback is not defined for sendMail then the method returns a native Promise object instead
Related Questions
- → Maximum call stack exceeded when instantiating class inside of a module
- → Browserify api: how to pass advanced option to script
- → Node.js Passing object from server.js to external modules?
- → gulp-rename makes copies, but does not replace
- → requiring RX.js in node.js
- → Remove an ObjectId from an array of objectId
- → Can not connect to Redis
- → React: How to publish page on server using React-starter-kit
- → Express - better pattern for passing data between middleware functions
- → Can't get plotly + node.js to stream data coming through POST requests
- → IsGenerator implementation
- → Async/Await not waiting
- → (Socket.io on nodejs) Updating div with mysql data stops without showing error