I have an error. I am using nodemailer to send emails from my Firebase application.
This what my code looks like:
const functions = require('firebase-functions');
const admin = require("firebase-admin");
const nodemailer = require('nodemailer');
admin.initializeApp();
//THESE SETTINGS WORK ON LOCAL AND LIVE. BUT I DONT WANT TO USE THEM
// const transporter = nodemailer.createTransport({
// service: 'gmail',
// auth: {
// user: 'GMAIL HERE',
// pass: 'GMAIL PW HERE'
// },
// })
//THESE SETTINGS WORK ON LOCAL, BUT NOT ON LIVE.
const transporter = nodemailer.createTransport({
host: "smtp.mycompanyname.com",
port: 25,
secureConnection: false, // TLS requires secureConnection to be false
logger: true,
debug: true,
secure: false,
requireTLS: true,
auth: {
user: "USERNAME HERE",
pass: "PASSWORD HERE"
},
tls: { rejectUnauthorized: false }
})
exports.sendConfirmationEmail = functions.https.onCall((data, context) => {
var email_adress = data.data.email;
var email = {
from: 'E-Mail Adress Goes Here',
to: email_adress,
subject: 'BlaBlaBla',
text: 'BlaBlaBla',
html: 'BlaBlaBla'
};
// Function to send e-mail to the user
transporter.sendMail(email, function(err, info) {
if (err) {
console.log(err);
return { success: false };
} else {
return { success: true }
}
});
})
Now. If I use the GMail settings. All works fine. It sends E-Mails. However, my company has an own SMTP Server. The SMTP works for the Firebase Authentication E-Mails. It is successfully sending those emails.
The SMTP server also works when I paste the above configuration in my local environment. However, when I run this in the Firebase Cloud function it gives me the following error:
10:24:43.479 AM
sendConfirmationEmail
[2020-09-25 08:24:43] DEBUG [Cq6p67HnXLA] Closing connection to the server using "destroy"
10:24:43.479 AM
sendConfirmationEmail
[2020-09-25 08:24:43] ERROR Send Error: Connection timeout
10:24:44.673 AM
sendConfirmationEmail
{ Error: Connection timeout
10:24:44.673 AM
sendConfirmationEmail
at SMTPConnection._formatError (/workspace/node_modules/nodemailer/lib/smtp-connection/index.js:784:19)
10:24:44.674 AM
sendConfirmationEmail
at SMTPConnection._onError (/workspace/node_modules/nodemailer/lib/smtp-connection/index.js:770:20)
10:24:44.674 AM
sendConfirmationEmail
at Timeout._connectionTimeout.setTimeout (/workspace/node_modules/nodemailer/lib/smtp-connection/index.js:235:22)
I've tried to play around with the different nodemailer options, but so far not a lot of success. It also makes it hard that on local it works, but when I deploy it doesn't.
Any ideas?