Last active
September 30, 2019 01:51
-
-
Save cbejensen/4890e234549b819403ab1e114ee5cc96 to your computer and use it in GitHub Desktop.
Nodemailer with cors not working
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express') | |
const bodyParser = require('body-parser') | |
const cors = require('cors') | |
const app = express() | |
const nodemailer = require('nodemailer') | |
const port = 8000 | |
app.use(bodyParser.json()) | |
app.options('*', cors()) | |
const transporter = nodemailer.createTransport({ | |
service: 'gmail', | |
auth: { | |
user: 'EMAIL_HERE', | |
pass: 'PASSWORD_HERE' | |
} | |
}) | |
let mailOptions | |
app.post('/api/1/email-alert/wish-comment/', (req, res) => { | |
const data = req.body | |
mailOptions = { | |
from: 'EMAIL_HERE', | |
to: data.emailTo, | |
subject: data.wishTitle, | |
html: `<h1>New comment on ${data.wishTitle}</h1> | |
<p>${data.message}</p>` | |
} | |
transporter.sendMail(mailOptions, function(err, info) { | |
if (err) res.send(err) | |
else res.send(info) | |
}) | |
}) | |
app.listen(port, () => console.log(`Example app listening on port ${port}!`)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Replace the line 23 : app.post('/api/1/email-alert/wish-comment/', (req, res) => {
By : app.post('/api/1/email-alert/wish-comment/', cors(), (req, res) => {