Created
June 21, 2021 07:21
-
-
Save vuk/74d5fbf548b74c6a8fcd6fb765e3a95d to your computer and use it in GitHub Desktop.
NodeJS Mailgun
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
import Mailgun from 'mailgun.js'; | |
import formData from 'form-data'; | |
export class Util { | |
public static async sendEmail( | |
fromAddress: string, | |
toAddress: string, | |
subject: string, | |
html: string, | |
text: string, | |
from?: string | |
): Promise<any> { | |
const mailgun = new Mailgun(formData); | |
const mg = mailgun.client({ | |
username: 'api', | |
key: process.env.MAILGUN_API_KEY, | |
public_key: process.env.MAILGUN_PUBLIC_VALIDATION_KEY, | |
}); | |
const data = { | |
from: from ? `${from} <${fromAddress}>` : `${fromAddress}`, | |
to: [toAddress], | |
subject, | |
html, | |
text, | |
}; | |
console.log("sending email ", data); | |
return mg.messages.create(process.env.MAILGUN_DOMAIN, data); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment