Reputation: 42957
I am working on a Firebase project that uses Google Cloud Functions. I am some problem trying to implement a cloud function that uses nodemailer in order to send an e-mail.
I have something like this in my code:
import * as functions from 'firebase-functions';
import admin = require('firebase-admin');
import nodemailer = require('nodemailer');
admin.initializeApp();
const transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 465,
secure: true,
auth: {
user: '[email protected]',
pass: 'my-password'
}
});
const mailOptions = {
from: `[email protected]`,
to: `[email protected]`,
subject: 'contact form message',
text: 'TEST TEST TEST'
/*
html: `<h1>Order Confirmation</h1>
<p> <b>Email: </b>TEST </p>`
*/
};
export const sendEmailNotification = functions.https.onRequest((request, response) => {
console.log("sendEmailNotification START 2");
transporter.sendMail(mailOptions, (error, data) => {
if (error) {
console.log(error);
response.status(500).json({message: 'ERROR SENDING MAIL !!!'});
}
console.log("Sent!");
response.status(200).json({message: 'NOTIFICATION MAIL SENT !!!'});
});
});
So as you can see I prepared the transporter object setting the gmail confifuration inserting user and password as authentication.
Then the maulOption object contains the e-mail to be sent.
Finnally I defined a GET function that called should send the e-mail.
The problem is that it is not working and I am obtaining the following error into the Firebase cloud function log:
Error: Can't set headers after they are sent.
at validateHeader (_http_outgoing.js:491:11)
at ServerResponse.setHeader (_http_outgoing.js:498:3)
at ServerResponse.header (/worker/node_modules/express/lib/response.js:767:10)
at ServerResponse.send (/worker/node_modules/express/lib/response.js:170:12)
at ServerResponse.json (/worker/node_modules/express/lib/response.js:267:15)
at transporter.sendMail (/srv/lib/index.js:72:30)
at transporter.send.args (/srv/node_modules/nodemailer/lib/mailer/index.js:226:21)
at connection.login.err (/srv/node_modules/nodemailer/lib/smtp-transport/index.js:282:36)
at SMTPConnection._actionAUTHComplete (/srv/node_modules/nodemailer/lib/smtp-connection/index.js:1513:20)
at SMTPConnection._responseActions.push.str (/srv/node_modules/nodemailer/lib/smtp-connection/index.js:540:26)
What it means? What is wrong? What am I missing? I am going crazy...how can I fix my code in order to correctly send this e-mail?
EDIT-1: After that I solved the header error I am obtaining this new error message:
{ Error: Invalid login: 535-5.7.8 Username and Password not accepted. Learn more at
535 5.7.8 https://support.google.com/mail/?p=BadCredentials t12sm13191635ios.12 - gsmtp
at SMTPConnection._formatError (/srv/node_modules/nodemailer/lib/smtp-connection/index.js:774:19)
at SMTPConnection._actionAUTHComplete (/srv/node_modules/nodemailer/lib/smtp-connection/index.js:1513:34)
at SMTPConnection._responseActions.push.str (/srv/node_modules/nodemailer/lib/smtp-connection/index.js:540:26)
at SMTPConnection._processResponse (/srv/node_modules/nodemailer/lib/smtp-connection/index.js:932:20)
at SMTPConnection._onData (/srv/node_modules/nodemailer/lib/smtp-connection/index.js:739:14)
at TLSSocket.SMTPConnection._onSocketData.chunk (/srv/node_modules/nodemailer/lib/smtp-connection/index.js:189:44)
at emitOne (events.js:116:13)
at TLSSocket.emit (events.js:211:7)
at addChunk (_stream_readable.js:263:12)
at readableAddChunk (_stream_readable.js:250:11)
code: 'EAUTH',
response: '535-5.7.8 Username and Password not accepted. Learn more at\n535 5.7.8 https://support.google.com/mail/?p=BadCredentials t12sm13191635ios.12 - gsmtp',
responseCode: 535,
command: 'AUTH PLAIN' }
EDIT-2: As suggested I actevated also the Less secure app access but now I obtain this error message into the Firebase cloud function log:
{ Error: Invalid login: 534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbs
534-5.7.14 XR1smQTqgOc7rRSUWEDTn9TfUIueWg5Fyd78-WkCR0YjjNDoMp91N1f4Lwy0EE9YKy720
534-5.7.14 Eg9dGeLWkuox0l8KKoOB2GdveIVKleuQ7mO5oYJT6ZMLdk-EzDxujYyknc_YmpkV>
534-5.7.14 Please log in via your web browser and then try again.
534-5.7.14 Learn more at
534 5.7.14 https://support.google.com/mail/answer/78754 m26sm16711541ill.21 - gsmtp
at SMTPConnection._formatError (/srv/node_modules/nodemailer/lib/smtp-connection/index.js:774:19)
at SMTPConnection._actionAUTHComplete (/srv/node_modules/nodemailer/lib/smtp-connection/index.js:1513:34)
at SMTPConnection._responseActions.push.str (/srv/node_modules/nodemailer/lib/smtp-connection/index.js:540:26)
at SMTPConnection._processResponse (/srv/node_modules/nodemailer/lib/smtp-connection/index.js:932:20)
at SMTPConnection._onData (/srv/node_modules/nodemailer/lib/smtp-connection/index.js:739:14)
at TLSSocket.SMTPConnection._onSocketData.chunk (/srv/node_modules/nodemailer/lib/smtp-connection/index.js:189:44)
at emitOne (events.js:116:13)
at TLSSocket.emit (events.js:211:7)
at addChunk (_stream_readable.js:263:12)
at readableAddChunk (_stream_readable.js:250:11)
code: 'EAUTH',
response: '534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbs\n534-5.7.14 XR1smQTqgOc7rRSUWEDTn9TfUIueWg5Fyd78-WkCR0YjjNDoMp91N1f4Lwy0EE9YKy720\n534-5.7.14 Eg9dGeLWkuox0l8KKoOB2GdveIVKleuQ7mO5oYJT6ZMLdk-EzDxujYyknc_YmpkV>\n534-5.7.14 Please log in via your web browser and then try again.\n534-5.7.14 Learn more at\n534 5.7.14 https://support.google.com/mail/answer/78754 m26sm16711541ill.21 - gsmtp',
responseCode: 534,
command: 'AUTH PLAIN' }
Upvotes: 0
Views: 299
Reputation: 3642
If your account is using 2FA, then you need to use app password, instead of your regular password you use on web. You can read more about it here.
There might be another way to give access by enabling less secure apps but as name suggest it is not recommended, but for testing purposes you can try to enable it. Read more about it here.
EDIT
You might also need to Unlock Captcha in order to gain access. Try this link.
Upvotes: 1
Reputation: 30675
The fix should be pretty simple; At the moment, you're setting the response status twice, this is what is causing the issue in the logs.
Changing the transporter.sendMail callback like to should fix this:
transporter.sendMail(mailOptions, (error, data) => {
if (error) {
console.log(error);
response.status(500).json({message: 'ERROR SENDING MAIL !!!'});
} else {
console.log("Sent!");
response.status(200).json({message: 'NOTIFICATION MAIL SENT !!!'});
}
});
I suspect there's another error sending the mail as well, but it won't be logged until you're able to fix this one.
I believe you'll also need to configure the gmail account to accept logins from nodemailer.
Upvotes: 1