Created
April 25, 2019 10:05
-
-
Save dev-drprasad/01a5e16e07ab62df142bc79aa5e786b2 to your computer and use it in GitHub Desktop.
Send email via mailgun using NodeJS standard library
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 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 = '<[email protected]>'; | |
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: '[email protected]', | |
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