Created
February 3, 2020 19:29
-
-
Save olegpolyakov/adbb45744cdc7df40055212e5bdcf1d3 to your computer and use it in GitHub Desktop.
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 request = require('request'); | |
module.exports = (MAILGUN_API_KEY, MAILGUN_API_URL) => ({ | |
send({ from, to, subject, text, html, template, variables }) { | |
return new Promise((resolve, reject) => { | |
request.post(`${MAILGUN_API_URL}/messages`, { | |
auth: { | |
user: 'api', | |
pass: MAILGUN_API_KEY | |
}, | |
form: { | |
from, | |
to, | |
subject, | |
text, | |
html, | |
template, | |
'h:X-Mailgun-Variables': JSON.stringify(variables) | |
} | |
}, (error, response, body) => { | |
if (error) { | |
reject(error); | |
} else if (response.statusCode !== 200) { | |
reject(new Error(response.statusMessage)); | |
} else { | |
try { | |
resolve(JSON.parse(body)); | |
} catch (error) { | |
reject(error); | |
} | |
} | |
}); | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment