Skip to content

Instantly share code, notes, and snippets.

@cbejensen
Last active September 30, 2019 01:51
Show Gist options
  • Save cbejensen/4890e234549b819403ab1e114ee5cc96 to your computer and use it in GitHub Desktop.
Save cbejensen/4890e234549b819403ab1e114ee5cc96 to your computer and use it in GitHub Desktop.
Nodemailer with cors not working
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}!`))
@PierreCrb
Copy link

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) => {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment