Skip to content

Instantly share code, notes, and snippets.

@dev-drprasad
Created April 25, 2019 10:05
Show Gist options
  • Save dev-drprasad/01a5e16e07ab62df142bc79aa5e786b2 to your computer and use it in GitHub Desktop.
Save dev-drprasad/01a5e16e07ab62df142bc79aa5e786b2 to your computer and use it in GitHub Desktop.
Send email via mailgun using NodeJS standard library
const https = require('https');
const qs = require('querystring');
const MAILGUN_API_TOKEN = '<api-token>';
const MAILGUN_API_PATH = '<api-path>'; // API url without hostname
const MAILGUN_FROM_EMAIL = '<from@email.com>';
const options = {
hostname: 'api.mailgun.net',
path: MAILGUN_API_PATH,
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
auth: 'api:' + MAILGUN_API_TOKEN,
}
const req = https.request(options, (res) => {
let result = '';
res.on("data", (b) => {
result += b.toString();
});
res.on('end', () => {
console.log('result :', result);
});
});
req.on('error', (err) => {
console.log('err :', err);
});
const payload = qs.stringify({
from: MAILGUN_FROM_EMAIL,
to: 'to@email.com',
subject: "Ahoy, man!",
text: 'YoYoYo!',
});
req.write(payload)
req.end();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment