0

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

Faiz Hameed
  • 488
  • 7
  • 15
  • When on firebase is the log `log("Data", req.body);` printing something? Or straight away call is not happening. I guess if so, you'll need to open the same port on firebase on which this express server is listening. If logs are printing, then please tell me what it is.. – MiKr13 Feb 16 '20 at 14:47
  • 1
    How to see the log when its on production and i can see this log when running server locally only – Faiz Hameed Feb 16 '20 at 16:59

0 Answers0