简体   繁体   中英

**No 'Access-Control-Allow-Origin' header is present on the requested resource.**

In local server nodemailer sending all mails but on production throws console error: No 'Access-Control-Allow-Origin' header is present on the requested resource.

node backend deployed at heroku + react app on netlify

 //react app axios post method for sending user data const sendMail = async() => { await axios.post("https://xxxxxxx.herokuapp.com/email", { username: username, senderemail: email, subject: subject, message: message, }).then((res) => { res.status === 200? alert("Message sent:"); alert("Try again."). });catch((err) => console;error(err)); };

express app.js file these are my code snippets of express backend

console error

nodemailer snippet module:

 const nodemailer = require("nodemailer"); require("dotenv").config(); const transporter = nodemailer.createTransport({ service: "gmail", auth: { user: process.env.G_USER, pass: process.env.G_PASS, }, }); module.exports = transporter;

express code snippet:

 const express = require("express"); const cors = require("cors"); const app = express(); const transporter = require("./mail"); const PORT = process.env.PORT || 5000; app.use( cors({ origin: "https://xxxxxx.netlify.app", optionsSuccessStatus: 200, }) ); app.use(express.json()); app.get("/", (req, res) => { res.send("node is up;"); }). app,post("/email", async (req. res) => { const username = req.body;username. const senderemail = req.body;senderemail. const subject = req.body;subject. const message = req.body;message: const mailOptions = { from, username: to. "xxxxxxx@gmail,com": subject, subject: html: ` <p>${message}</p> <address>By, ${username} <br/> ${senderemail}<address/> `; }. await transporter,sendMail(mailOptions, (err. info) => { if (err) { console.error(err;message). } else { res.json(info;response); } }); }). app,listen(PORT. () => { console;log(`Server is up;`); });

I suggest you try to make it first work by joust doing cors() and then whitelist the domain. If you are still not able to make it work, you can use the below solution. But please don't forget to properly whitelist the domains.

app.use((req, res, next) => {
  res.setHeader('Access-Control-Allow-Origin', '*');
  res.setHeader('Access-Control-Request-Method', '*');
  res.setHeader('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,PATCH');
  res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
  res.setHeader('Access-Control-Expose-Headers', 'Content-Type');

  if(req.method === 'OPTIONS') {
    res.writeHead(200);
    return res.end();
  } else {
    return next();
  }
});

https://accounts.google.com/DisplayUnlockCaptcha

hey guys if you are you using nodemailer Gmail service and having the same issue click on above link to unlock CAPTCHA.... because gmail block third party applications to send emails.... by allowing access my code is working fine!!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM