Skip to content

Instantly share code, notes, and snippets.

@webdeb
Created November 5, 2020 23:51
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • 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

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