Reputation: 187
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?
Upvotes: 0
Views: 1558
Reputation: 83163
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
Upvotes: 0
Reputation: 185
I'm guessing you are trying to login with login / password but you are creating your transport with oauth2 settings moreover you should remove the from key in the options as gmail will override it with your email (I don't know if it create an error tho). I also noticed that you use the text key for your mail options but sending html while there is an html key available (maybe it is causing some issue).
const gmailEmail = functions.config().gmail.email;
const gmailPassword = functions.config().gmail.password;
// create transport
const mailTransport = nodemailer.createTransport({
service: "gmail",
auth: {
user: gmailEmail,
pass: gmailPassword
}
});
exports.sendMail= functions.https.onRequest((req, res) => {
cors(req, res, () => {
const mailOptions = {
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');
});
});
});
There is also some config to do with your google account
accept less secure apps google security
turn off capatcha unlock captcha
You can test your function and see errors inside google cloud functions console gcp / functions it helped me debug the same issue.
Upvotes: 1