mailgun/ts/mailgun.classes.account.ts

147 lines
4.3 KiB
TypeScript
Raw Normal View History

2019-10-23 18:00:04 +00:00
import * as plugins from './mailgun.plugins';
2020-01-13 08:09:37 +00:00
import * as interfaces from './interfaces';
2019-10-23 18:00:04 +00:00
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;
}
2020-01-13 08:09:37 +00:00
public async getRequest(routeArg: string, binaryArg: boolean = false) {
2020-01-11 19:01:59 +00:00
let requestUrl = routeArg;
2020-01-13 08:09:37 +00:00
const needsBaseUrlPrefix = !routeArg.startsWith('https://');
2020-06-18 22:00:00 +00:00
needsBaseUrlPrefix ? (requestUrl = `${this.baseUrl}${routeArg}`) : null;
2020-01-13 08:09:37 +00:00
console.log(requestUrl);
const requestOptions: plugins.smartrequest.ISmartRequestOptions = {
2019-10-26 21:55:50 +00:00
method: 'GET',
headers: {
2020-06-18 22:00:00 +00:00
Authorization: `Basic ${plugins.smartstring.base64.encode(`api:${this.apiToken}`)}`,
2019-10-26 21:55:50 +00:00
'Content-Type': 'application/json'
2020-08-11 14:47:25 +00:00
},
keepAlive: false
2020-01-13 08:09:37 +00:00
};
let response: plugins.smartrequest.IExtendedIncomingMessage;
if (!binaryArg) {
response = await plugins.smartrequest.request(requestUrl, requestOptions);
} else {
response = await plugins.smartrequest.getBinary(requestUrl, requestOptions);
}
2020-01-11 19:01:59 +00:00
return response;
2019-10-26 21:55:50 +00:00
}
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: {
2020-06-18 22:00:00 +00:00
Authorization: `Basic ${plugins.smartstring.base64.encode(`api:${this.apiToken}`)}`
2020-08-11 14:47:25 +00:00
},
keepAlive: false
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
*/
2020-06-18 22:00:00 +00:00
public async sendSmartMail(
smartmailArg: plugins.smartmail.Smartmail<interfaces.IMailgunMessage>,
toArg: string,
dataArg = {}
) {
2020-04-26 23:24:03 +00:00
const domain = smartmailArg.options.from.split('@')[1].replace('>', '');
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)
2020-08-11 14:47:25 +00:00
}
];
if (smartmailArg.getBody(dataArg)) {
formFields.push({
2019-10-27 21:53:21 +00:00
name: 'html',
type: 'string',
payload: smartmailArg.getBody(dataArg)
2020-08-11 14:47:25 +00:00
});
} else {
console.log('message has no body');
formFields.push({
name: 'html',
type: 'string',
payload: 'The sender did not provide a bodytext.'
});
}
2019-10-27 21:53:21 +00:00
2020-01-13 08:09:37 +00:00
console.log(smartmailArg.attachments);
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);
2019-10-28 15:15:16 +00:00
if (response.statusCode === 200) {
return response.body;
} else {
2020-01-11 19:01:59 +00:00
console.log(response.body);
2019-10-28 15:15:16 +00:00
throw new Error('could not send email');
}
2019-10-23 18:00:04 +00:00
}
2020-01-11 19:01:59 +00:00
public async retrieveSmartMailFromMessageUrl(messageUrlArg: string) {
const response = await this.getRequest(messageUrlArg);
2020-01-21 10:04:28 +00:00
if (response.statusCode === 404) {
console.log(response.body.message);
return null;
}
2020-01-13 08:09:37 +00:00
const responseBody: interfaces.IMailgunMessage = response.body;
2020-01-13 16:04:25 +00:00
const smartmail = new plugins.smartmail.Smartmail<interfaces.IMailgunMessage>({
2020-01-13 08:09:37 +00:00
from: responseBody.From,
2020-06-18 22:00:00 +00:00
body: responseBody['body-html'],
2020-01-13 08:09:37 +00:00
subject: responseBody.Subject,
2020-01-23 16:55:20 +00:00
creationObjectRef: responseBody
2020-01-13 08:09:37 +00:00
});
// lets care about attachments
2020-01-21 10:04:28 +00:00
if (responseBody.attachments && responseBody.attachments instanceof Array) {
for (const attachmentInfo of responseBody.attachments) {
const attachmentName = attachmentInfo.name;
const attachmentContents = await this.getRequest(attachmentInfo.url, true);
2020-06-18 22:00:00 +00:00
smartmail.addAttachment(
new plugins.smartfile.Smartfile({
path: `./${attachmentName}`,
base: `./${attachmentName}`,
contentBuffer: attachmentContents.body
})
);
2020-01-21 10:04:28 +00:00
}
2020-01-13 08:09:37 +00:00
}
2020-01-11 19:01:59 +00:00
2020-01-13 08:09:37 +00:00
return smartmail;
}
2020-06-18 22:00:00 +00:00
2020-01-11 19:01:59 +00:00
public async retrieveSmartMailFromNotifyPayload(notifyPayloadArg: any) {
return await this.retrieveSmartMailFromMessageUrl(notifyPayloadArg['message-url']);
}
2019-10-26 21:55:50 +00:00
}