I have created a backend server using express and deployed to firebase. which allows me to receive emails from my contact page in my portfolio.
I am able to receive emails when I run both client and backend server locally. But when I deploy to firebase and tried with the firebase app URL, it's not working. Then I tried hosting my app in Github and tried the contact page so that request is from SSL page(https). But still, I get error 500 I tried the solution mentioned and didn't work sending-email-via-node-js-using-nodemailer-is-not-working
My express js codes
index.js
const express = require("express");
const functions = require("firebase-functions");
const path = require("path");
const sendMail = require("./mail");
const log = console.log;
var cors = require("cors");
const app = express();
app.use(cors({ origin: true }));
// data parsing
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
app.post("/email", (req, res) => {
// expect to receive some data from client
const { name, email, message } = req.body;
log("Data", req.body);
sendMail(email, name, message, function(err, data) {
if (err) {
res.status(500).json({ message: "Internal Error" });
} else {
res.json({ message: "success", data });
}
});
//email, name, text
});
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "../views", "index.html"));
});
exports.app = functions.https.onRequest(app);
mail.js
const mailGun = require("nodemailer-mailgun-transport");
const auth = {
auth: {
api_key: "xxxxxxxxx",
domain: "xxxxxxxxx"
}
};
const transporter = nodemailer.createTransport(mailGun(auth));
const sendMail = (email, name, message, callBack) => {
const mailOptions = {
from: email,
to: "faizhameedv@gmail.com",
subject: `your website mail from ${name}`,
text: message
};
transporter.sendMail(mailOptions, function(err, data) {
if (err) {
console.log("err", err);
callBack(err, null);
} else {
callBack(null, data);
console.log("Message Sent!!");
}
});
};
module.exports = sendMail;
It works when I run locally using firebase serve --only functions,hosting
and then when I deploy it to firebase which is successful. But I cant req the new URL given by firebase with /email