Skip to content

Instantly share code, notes, and snippets.

@vuk
Created June 21, 2021 07:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vuk/74d5fbf548b74c6a8fcd6fb765e3a95d to your computer and use it in GitHub Desktop.
Save vuk/74d5fbf548b74c6a8fcd6fb765e3a95d to your computer and use it in GitHub Desktop.
NodeJS Mailgun
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