Skip to content

Instantly share code, notes, and snippets.

@webdeb
Created November 5, 2020 23:51
Show Gist options
  • Save webdeb/837886746bf43e46effb269c68f0f5ce to your computer and use it in GitHub Desktop.
Save webdeb/837886746bf43e46effb269c68f0f5ce to your computer and use it in GitHub Desktop.
Simple deno mailgun client
export default class MailgunClient {
apiKey: string;
domain: string;
defaults?: {
from?: string;
subject?: string;
};
constructor(apiKey: string, domain: string, defaults?: Record<string, any>) {
this.apiKey = apiKey;
this.domain = domain;
this.defaults = defaults || {};
}
async send(data: any) {
data = { ...this.defaults, ...data };
const form = new FormData();
Object.keys(data).forEach((k: string) => form.append(k, data[k]));
return fetch(`https://api.mailgun.net/v3/${this.domain}/messages`, {
method: "POST",
headers: {
Authorization: `Basic ${btoa("api:" + this.apiKey)}`,
},
body: form,
});
}
}
@webdeb
Copy link
Author

webdeb commented Nov 5, 2020

import MailgunClient from "https://gist.githubusercontent.com/webdeb/837886746bf43e46effb269c68f0f5ce/raw/1eff4888e78ea1d8d2998c62284d1f7711ce205c/Mailgun.ts"

const defaults = {
  subject: "A message from your site",
  from: "hello@example.com"
}

const mail = new MailgunClient(apiKey, domain, defaults)
mail.send({ text: "Hello", to: "user@example.com" })

@webdeb
Copy link
Author

webdeb commented Nov 5, 2020

Feel free to use, update, contribute, whatever

@charlesfries
Copy link

Awesome! Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment