33 * Demo script to send an email using the Mailgun API.
44 * ==============================================================
55 * Instructions:
6- *
6+ *
77 * 1. Create a file `mailgun.json` in the root directory with the
88 * following keys to configure the test script:
99 * ```
1515 * recipient: "xxx", // The email recipient.
1616 * }
1717 * ```
18- *
18+ *
1919 * 2. Run this script with `node ./demo` to send the email.
2020 * ==============================================================
2121 */
@@ -26,55 +26,54 @@ const mailgun = require('mailgun.js');
2626const path = require ( 'path' ) ;
2727
2828const {
29- key,
30- domain,
31- host,
32- sender,
33- recipient
29+ key,
30+ domain,
31+ sender,
32+ recipient
3433} = require ( './mailgun.json' ) ;
3534
3635const mailgunClient = mailgun . client ( { username : 'api' , key : key } ) ;
3736const filePath = ( file ) => path . resolve ( __dirname , '../spec/templates/' , file ) ;
3837const config = {
39- sender : sender ,
40- templates : {
41- passwordResetEmail : {
42- subjectPath : filePath ( 'password_reset_email_subject.txt' ) ,
43- textPath : filePath ( 'password_reset_email.txt' ) ,
44- htmlPath : filePath ( 'password_reset_email.html' )
45- } ,
46- verificationEmail : {
47- subjectPath : filePath ( 'verification_email_subject.txt' ) ,
48- textPath : filePath ( 'verification_email.txt' ) ,
49- htmlPath : filePath ( 'verification_email.html' )
50- } ,
51- customEmail : {
52- subjectPath : filePath ( 'custom_email_subject.txt' ) ,
53- textPath : filePath ( 'custom_email.txt' ) ,
54- htmlPath : filePath ( 'custom_email.html' ) ,
55- placeholders : {
56- username : "DefaultUser" ,
57- appName : "DefaultApp"
58- } ,
59- extra : {
60- 61- }
62- }
38+ sender : sender ,
39+ templates : {
40+ passwordResetEmail : {
41+ subjectPath : filePath ( 'password_reset_email_subject.txt' ) ,
42+ textPath : filePath ( 'password_reset_email.txt' ) ,
43+ htmlPath : filePath ( 'password_reset_email.html' )
6344 } ,
64- apiCallback : async ( { payload } ) => {
65- const mailgunPayload = ApiPayloadConverter . mailgun ( payload ) ;
66- await mailgunClient . messages . create ( domain , mailgunPayload ) ;
45+ verificationEmail : {
46+ subjectPath : filePath ( 'verification_email_subject.txt' ) ,
47+ textPath : filePath ( 'verification_email.txt' ) ,
48+ htmlPath : filePath ( 'verification_email.html' )
49+ } ,
50+ customEmail : {
51+ subjectPath : filePath ( 'custom_email_subject.txt' ) ,
52+ textPath : filePath ( 'custom_email.txt' ) ,
53+ htmlPath : filePath ( 'custom_email.html' ) ,
54+ placeholders : {
55+ username : "DefaultUser" ,
56+ appName : "DefaultApp"
57+ } ,
58+ extra : {
59+ 60+ }
6761 }
62+ } ,
63+ apiCallback : async ( { payload } ) => {
64+ const mailgunPayload = ApiPayloadConverter . mailgun ( payload ) ;
65+ await mailgunClient . messages . create ( domain , mailgunPayload ) ;
66+ }
6867} ;
6968
7069const adapter = new ApiMailAdapter ( config ) ;
7170
7271adapter . sendMail ( {
73- templateName : 'customEmail' ,
74- recipient : recipient ,
75- placeholders : {
76- appName : "ExampleApp" ,
77- username : "ExampleUser"
78- } ,
79- direct : true
80- } ) ;
72+ templateName : 'customEmail' ,
73+ recipient : recipient ,
74+ placeholders : {
75+ appName : "ExampleApp" ,
76+ username : "ExampleUser"
77+ } ,
78+ direct : true
79+ } ) ;
0 commit comments