mailgun/ts/mailgun.classes.account.ts

79 lines
2.0 KiB
TypeScript
Raw Permalink Normal View History

2019-10-23 18:00:04 +00:00
import * as plugins from './mailgun.plugins';
export class MailgunAccount {
2019-10-26 21:55:50 +00:00
public baseUrl = 'https://api.mailgun.net/v3';
public apiToken: string;
2019-10-23 18:00:04 +00:00
2019-10-26 21:55:50 +00:00
constructor(apiTokenArg: string) {
this.apiToken = apiTokenArg;
}
public getRequest(routeArg: string) {
const requestUrl = `${this.baseUrl}${routeArg}`; // TODO;
const response = plugins.smartrequest.request(routeArg, {
method: 'GET',
headers: {
Authorization: `Basic ${plugins.smartstring.base64.encode(
`api:key-a049e048b7029b9621c41b5682fadee9`
)}`,
'Content-Type': 'application/json'
}
});
}
2019-10-23 18:00:04 +00:00
2019-10-26 21:55:50 +00:00
public async postFormData(routeArg: string, formFields: plugins.smartrequest.IFormField[]) {
const requestUrl = `${this.baseUrl}${routeArg}`; // TODO;
const response = await plugins.smartrequest.postFormData(
routeArg,
{
headers: {
Authorization: `Basic ${plugins.smartstring.base64.encode(
`api:key-a049e048b7029b9621c41b5682fadee9`
)}`
}
},
formFields
);
return response;
}
2019-10-23 18:00:04 +00:00
2019-10-26 21:55:50 +00:00
/**
* sends a SmartMail
*/
2019-10-27 21:53:21 +00:00
public sendSmartMail(smartmailArg: plugins.smartmail.Smartmail, toArg: string, dataArg = {}) {
2019-10-26 21:55:50 +00:00
const domain = smartmailArg.options.from.split('@')[1];
2019-10-27 21:53:21 +00:00
const formFields: plugins.smartrequest.IFormField[] = [
2019-10-26 21:55:50 +00:00
{
name: 'from',
type: 'string',
payload: smartmailArg.options.from
2019-10-27 21:53:21 +00:00
},
{
name: 'to',
type: 'string',
payload: toArg
},
{
name: 'subject',
type: 'string',
payload: smartmailArg.getSubject(dataArg)
},
{
name: 'html',
type: 'string',
payload: smartmailArg.getBody(dataArg)
2019-10-26 21:55:50 +00:00
}
2019-10-27 21:53:21 +00:00
];
for (const attachment of smartmailArg.attachments) {
formFields.push({
name: 'attachment',
type: 'Buffer',
payload: attachment.contentBuffer
})
}
this.postFormData(`/${domain}/messages`, formFields);
2019-10-23 18:00:04 +00:00
}
2019-10-26 21:55:50 +00:00
}