Last active
May 13, 2020 16:26
-
-
Save peterkracik/6f26481b93206db3a664f3896c6eed0f to your computer and use it in GitHub Desktop.
node.js send mail with mailgun
This file contains hidden or 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 nodemailer = require('nodemailer'); | |
| const mg = require('nodemailer-mailgun-transport'); | |
| // Mailgun authentication | |
| const auth = { | |
| auth: { | |
| api_key: process.env.API_KEY, // Mailgun API key | |
| domain: process.env.DOMAIN // Mailgun domain ie. mg.mydomain.com | |
| }, | |
| host: 'api.eu.mailgun.net' // for non-eu only api.moailgun.net | |
| } | |
| const nodemailerMailgun = nodemailer.createTransport(mg(auth)); // mailgun instance | |
| exports.send = (req, res) => { | |
| res.set('Access-Control-Allow-Origin', '*'); // don't forget to change this to your domain ! | |
| /** | |
| * if preflight request (OPTIONS), accept and return 204 | |
| */ | |
| if (req.method === 'OPTIONS') { | |
| // Send response to OPTIONS requests | |
| res.set('Access-Control-Allow-Methods', 'GET'); | |
| res.set('Access-Control-Allow-Headers', 'Content-Type'); | |
| res.set('Access-Control-Max-Age', '3600'); | |
| res.status(204).send(''); | |
| } else { // else process the request | |
| const data = req.body; // data passed as POST | |
| nodemailerMailgun.sendMail({ | |
| from: process.env.FROM, // environment variable for sender | |
| to: process.env.TO, // An array if you have multiple recipients. | |
| cc: process.env.CC || "", // cc or empty | |
| subject: data.subject, // subject (from the post request) | |
| html: data.message, // html email body (from the post request ) | |
| }, (err, info) => { | |
| if (err) { | |
| res.status(500).send({error:err, info: info}); // send 500 if not successful | |
| } | |
| else { | |
| res.status(200).send(info); // send 200 if successful | |
| } | |
| }); | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment