fix(core): update

This commit is contained in:
Philipp Kunz 2020-08-13 00:32:05 +00:00
parent 3abc973d88
commit 4cb32e8460
6 changed files with 83 additions and 37 deletions

23
package-lock.json generated
View File

@ -2153,6 +2153,16 @@
"which": "^1.3.1" "which": "^1.3.1"
} }
}, },
"@pushrocks/smartsmtp": {
"version": "1.0.6",
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartsmtp/-/smartsmtp-1.0.6.tgz",
"integrity": "sha512-W+XyCflolJdzFokSFpFX653+5tYd5vig3K0uFKVNH5wJYSrRr9E/+o2HsjA3L0xXQSnSRCUBtR7gE7C3l6QLhA==",
"requires": {
"@pushrocks/smartmail": "^1.0.18",
"@types/nodemailer": "^6.4.0",
"nodemailer": "^6.4.11"
}
},
"@pushrocks/smartstring": { "@pushrocks/smartstring": {
"version": "3.0.18", "version": "3.0.18",
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartstring/-/smartstring-3.0.18.tgz", "resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartstring/-/smartstring-3.0.18.tgz",
@ -2563,6 +2573,14 @@
"resolved": "https://verdaccio.lossless.one/@types%2fnode/-/node-14.0.27.tgz", "resolved": "https://verdaccio.lossless.one/@types%2fnode/-/node-14.0.27.tgz",
"integrity": "sha512-kVrqXhbclHNHGu9ztnAwSncIgJv/FaxmzXJvGXNdcCpV1b8u1/Mi6z6m0vwy0LzKeXFTPLH0NzwmoJ3fNCIq0g==" "integrity": "sha512-kVrqXhbclHNHGu9ztnAwSncIgJv/FaxmzXJvGXNdcCpV1b8u1/Mi6z6m0vwy0LzKeXFTPLH0NzwmoJ3fNCIq0g=="
}, },
"@types/nodemailer": {
"version": "6.4.0",
"resolved": "https://verdaccio.lossless.one/@types%2fnodemailer/-/nodemailer-6.4.0.tgz",
"integrity": "sha512-KY7bFWB0MahRZvVW4CuW83qcCDny59pJJ0MQ5ifvfcjNwPlIT0vW4uARO4u1gtkYnWdhSvURegecY/tzcukJcA==",
"requires": {
"@types/node": "*"
}
},
"@types/parcel-bundler": { "@types/parcel-bundler": {
"version": "1.12.1", "version": "1.12.1",
"resolved": "https://verdaccio.lossless.one/@types%2fparcel-bundler/-/parcel-bundler-1.12.1.tgz", "resolved": "https://verdaccio.lossless.one/@types%2fparcel-bundler/-/parcel-bundler-1.12.1.tgz",
@ -7909,6 +7927,11 @@
"integrity": "sha512-gsO4vjEdQaTusZAEebUWp2a5d7dF5DYoIpDG7WySnk7BuZDW+GPpHXoXXuYawRBr/9t5q54tirPz79kFIWg4dA==", "integrity": "sha512-gsO4vjEdQaTusZAEebUWp2a5d7dF5DYoIpDG7WySnk7BuZDW+GPpHXoXXuYawRBr/9t5q54tirPz79kFIWg4dA==",
"dev": true "dev": true
}, },
"nodemailer": {
"version": "6.4.11",
"resolved": "https://verdaccio.lossless.one/nodemailer/-/nodemailer-6.4.11.tgz",
"integrity": "sha512-BVZBDi+aJV4O38rxsUh164Dk1NCqgh6Cm0rQSb9SK/DHGll/DrCMnycVDD7msJgZCnmVa8ASo8EZzR7jsgTukQ=="
},
"normalize-html-whitespace": { "normalize-html-whitespace": {
"version": "1.0.0", "version": "1.0.0",
"resolved": "https://verdaccio.lossless.one/normalize-html-whitespace/-/normalize-html-whitespace-1.0.0.tgz", "resolved": "https://verdaccio.lossless.one/normalize-html-whitespace/-/normalize-html-whitespace-1.0.0.tgz",

View File

@ -25,6 +25,7 @@
"@pushrocks/smartfile": "^8.0.0", "@pushrocks/smartfile": "^8.0.0",
"@pushrocks/smartmail": "^1.0.18", "@pushrocks/smartmail": "^1.0.18",
"@pushrocks/smartrequest": "^1.1.47", "@pushrocks/smartrequest": "^1.1.47",
"@pushrocks/smartsmtp": "^1.0.6",
"@pushrocks/smartstring": "^3.0.18" "@pushrocks/smartstring": "^3.0.18"
}, },
"files": [ "files": [

View File

@ -1,2 +1,3 @@
required: required:
- MAILGUN_API_TOKEN - MAILGUN_API_TOKEN
- MAILGUN_SMTP_CREDENTIALS

View File

@ -12,6 +12,7 @@ let testSmartmail: smartmail.Smartmail<IMailgunMessage>;
tap.test('should create a mailgun account', async () => { tap.test('should create a mailgun account', async () => {
testMailgunAccount = new mailgun.MailgunAccount(testQenv.getEnvVarOnDemand('MAILGUN_API_TOKEN')); testMailgunAccount = new mailgun.MailgunAccount(testQenv.getEnvVarOnDemand('MAILGUN_API_TOKEN'));
testMailgunAccount.addSmtpCredentials(testQenv.getEnvVarOnDemand('MAILGUN_SMTP_CREDENTIALS'));
expect(testMailgunAccount).to.be.instanceOf(mailgun.MailgunAccount); expect(testMailgunAccount).to.be.instanceOf(mailgun.MailgunAccount);
}); });
@ -21,7 +22,7 @@ tap.test('should create a smartmail', async () => {
from: 'Lossless GmbH <noreply@mail.lossless.com>', from: 'Lossless GmbH <noreply@mail.lossless.com>',
subject: 'TestMessage from @mojoio/mailgun test' subject: 'TestMessage from @mojoio/mailgun test'
}); });
return testSmartmail; expect(testSmartmail).to.be.instanceOf(smartmail.Smartmail);
}); });
tap.test('should send a smartmail', async () => { tap.test('should send a smartmail', async () => {
@ -31,12 +32,10 @@ tap.test('should send a smartmail', async () => {
tap.test('should send a smartmail with empty body', async () => { tap.test('should send a smartmail with empty body', async () => {
const emptyBodySmartmail = new smartmail.Smartmail<IMailgunMessage>({ const emptyBodySmartmail = new smartmail.Smartmail<IMailgunMessage>({
body: '', body: '',
from: 'Lossless GmbH <noreply@mail.lossless.com>', from: 'Lossless GmbH <noreply@mail.lossless.one>',
subject: 'A message with no body from @mojoio/mailgun test' subject: 'A message with no body from @mojoio/mailgun test'
}); });
console.log('ok');
await testMailgunAccount.sendSmartMail(emptyBodySmartmail, 'Sandbox Team <sandbox@mail.git.zone>'); await testMailgunAccount.sendSmartMail(emptyBodySmartmail, 'Sandbox Team <sandbox@mail.git.zone>');
console.log('hey');
}); });
tap.test('should retrieve a mail using a retrieval url', async () => { tap.test('should retrieve a mail using a retrieval url', async () => {

View File

@ -5,10 +5,28 @@ export class MailgunAccount {
public baseUrl = 'https://api.mailgun.net/v3'; public baseUrl = 'https://api.mailgun.net/v3';
public apiToken: string; public apiToken: string;
public smartSmtps: {[domain: string]: plugins.smartsmtp.Smartsmtp} = {};
constructor(apiTokenArg: string) { constructor(apiTokenArg: string) {
this.apiToken = apiTokenArg; this.apiToken = apiTokenArg;
} }
/**
* allows adding smtp credentials
* Format: [domain]|[username]|[password]
*/
public addSmtpCredentials (credentials: string) {
const credentialArray = credentials.split('|');
if (credentialArray.length !== 3) {
throw new Error('credentials are in the wrong format');
}
this.smartSmtps[credentialArray[0]] = new plugins.smartsmtp.Smartsmtp({
smtpServer: 'smtp.mailgun.org',
smtpUser: credentialArray[1],
smtpPassword: credentialArray[2]
});
}
public async getRequest(routeArg: string, binaryArg: boolean = false) { public async getRequest(routeArg: string, binaryArg: boolean = false) {
let requestUrl = routeArg; let requestUrl = routeArg;
const needsBaseUrlPrefix = !routeArg.startsWith('https://'); const needsBaseUrlPrefix = !routeArg.startsWith('https://');
@ -18,9 +36,9 @@ export class MailgunAccount {
method: 'GET', method: 'GET',
headers: { headers: {
Authorization: `Basic ${plugins.smartstring.base64.encode(`api:${this.apiToken}`)}`, Authorization: `Basic ${plugins.smartstring.base64.encode(`api:${this.apiToken}`)}`,
'Content-Type': 'application/json' 'Content-Type': 'application/json',
}, },
keepAlive: false keepAlive: false,
}; };
let response: plugins.smartrequest.IExtendedIncomingMessage; let response: plugins.smartrequest.IExtendedIncomingMessage;
if (!binaryArg) { if (!binaryArg) {
@ -37,9 +55,9 @@ export class MailgunAccount {
requestUrl, requestUrl,
{ {
headers: { headers: {
Authorization: `Basic ${plugins.smartstring.base64.encode(`api:${this.apiToken}`)}` Authorization: `Basic ${plugins.smartstring.base64.encode(`api:${this.apiToken}`)}`,
}, },
keepAlive: false keepAlive: false,
}, },
formFields formFields
); );
@ -59,35 +77,25 @@ export class MailgunAccount {
{ {
name: 'from', name: 'from',
type: 'string', type: 'string',
payload: smartmailArg.options.from payload: smartmailArg.options.from,
}, },
{ {
name: 'to', name: 'to',
type: 'string', type: 'string',
payload: toArg payload: toArg,
}, },
{ {
name: 'subject', name: 'subject',
type: 'string', type: 'string',
payload: smartmailArg.getSubject(dataArg) payload: smartmailArg.getSubject(dataArg),
} },
{
name: 'html',
type: 'string',
payload: smartmailArg.getBody(dataArg),
},
]; ];
if (smartmailArg.getBody(dataArg)) {
formFields.push({
name: 'html',
type: 'string',
payload: smartmailArg.getBody(dataArg)
});
} else {
console.log('message has no body');
formFields.push({
name: 'html',
type: 'string',
payload: 'The sender did not provide a bodytext.'
});
}
console.log(smartmailArg.attachments); console.log(smartmailArg.attachments);
for (const attachment of smartmailArg.attachments) { for (const attachment of smartmailArg.attachments) {
@ -95,17 +103,30 @@ export class MailgunAccount {
name: 'attachment', name: 'attachment',
type: 'Buffer', type: 'Buffer',
payload: attachment.contentBuffer, payload: attachment.contentBuffer,
fileName: attachment.parsedPath.base fileName: attachment.parsedPath.base,
}); });
} }
if (smartmailArg.getBody(dataArg)) {
console.log('All requirements for API met');
const response = await this.postFormData(`/${domain}/messages`, formFields); const response = await this.postFormData(`/${domain}/messages`, formFields);
if (response.statusCode === 200) { if (response.statusCode === 200) {
console.log(`Sent mail with subject ${smartmailArg.getSubject(dataArg)} to ${toArg} using the mailgun API`);
return response.body; return response.body;
} else { } else {
console.log(response.body); console.log(response.body);
throw new Error('could not send email'); throw new Error('could not send email');
} }
} else {
console.log('An empty body was provided. This does not work via the API, but using SMTP instead.');
const wantedSmartsmtp = this.smartSmtps[domain];
if (!wantedSmartsmtp) {
console.log('did not find appropriate smtp credentials');
return;
}
wantedSmartsmtp.sendSmartMail(smartmailArg, toArg);
console.log(`Sent mail with subject ${smartmailArg.getSubject(dataArg)} to ${toArg} using an smtp transport over Mailgun`);
}
} }
public async retrieveSmartMailFromMessageUrl(messageUrlArg: string) { public async retrieveSmartMailFromMessageUrl(messageUrlArg: string) {
@ -119,7 +140,7 @@ export class MailgunAccount {
from: responseBody.From, from: responseBody.From,
body: responseBody['body-html'], body: responseBody['body-html'],
subject: responseBody.Subject, subject: responseBody.Subject,
creationObjectRef: responseBody creationObjectRef: responseBody,
}); });
// lets care about attachments // lets care about attachments
@ -131,7 +152,7 @@ export class MailgunAccount {
new plugins.smartfile.Smartfile({ new plugins.smartfile.Smartfile({
path: `./${attachmentName}`, path: `./${attachmentName}`,
base: `./${attachmentName}`, base: `./${attachmentName}`,
contentBuffer: attachmentContents.body contentBuffer: attachmentContents.body,
}) })
); );
} }

View File

@ -2,6 +2,7 @@
import * as smartfile from '@pushrocks/smartfile'; import * as smartfile from '@pushrocks/smartfile';
import * as smartmail from '@pushrocks/smartmail'; import * as smartmail from '@pushrocks/smartmail';
import * as smartrequest from '@pushrocks/smartrequest'; import * as smartrequest from '@pushrocks/smartrequest';
import * as smartsmtp from '@pushrocks/smartsmtp';
import * as smartstring from '@pushrocks/smartstring'; import * as smartstring from '@pushrocks/smartstring';
export { smartfile, smartmail, smartrequest, smartstring }; export { smartfile, smartmail, smartrequest, smartsmtp, smartstring };