Created
November 5, 2020 23:51
-
-
Save webdeb/837886746bf43e46effb269c68f0f5ce to your computer and use it in GitHub Desktop.
Simple deno mailgun client
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
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, | |
}); | |
} | |
} |
Author
webdeb
commented
Nov 5, 2020
•
Feel free to use, update, contribute, whatever
Awesome! Thank you
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment