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(
|
2019-10-28 14:55:04 +00:00
|
|
|
this.apiToken
|
2019-10-26 21:55:50 +00:00
|
|
|
)}`,
|
|
|
|
'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(
|
2019-10-28 14:55:04 +00:00
|
|
|
requestUrl,
|
2019-10-26 21:55:50 +00:00
|
|
|
{
|
|
|
|
headers: {
|
|
|
|
Authorization: `Basic ${plugins.smartstring.base64.encode(
|
2019-10-28 15:10:20 +00:00
|
|
|
this.apiToken
|
2019-10-26 21:55:50 +00:00
|
|
|
)}`
|
|
|
|
}
|
|
|
|
},
|
|
|
|
formFields
|
|
|
|
);
|
|
|
|
return response;
|
|
|
|
}
|
2019-10-23 18:00:04 +00:00
|
|
|
|
2019-10-26 21:55:50 +00:00
|
|
|
/**
|
|
|
|
* sends a SmartMail
|
|
|
|
*/
|
2019-10-28 14:55:04 +00:00
|
|
|
public async 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',
|
2019-10-28 14:55:04 +00:00
|
|
|
payload: attachment.contentBuffer,
|
|
|
|
fileName: attachment.parsedPath.base
|
|
|
|
});
|
2019-10-27 21:53:21 +00:00
|
|
|
}
|
|
|
|
|
2019-10-28 14:55:04 +00:00
|
|
|
const response = await this.postFormData(`/${domain}/messages`, formFields);
|
|
|
|
console.log(response);
|
2019-10-23 18:00:04 +00:00
|
|
|
}
|
2019-10-26 21:55:50 +00:00
|
|
|
}
|