fix(core): update
This commit is contained in:
		| @@ -5,10 +5,28 @@ export class MailgunAccount { | ||||
|   public baseUrl = 'https://api.mailgun.net/v3'; | ||||
|   public apiToken: string; | ||||
|  | ||||
|   public smartSmtps: {[domain: string]: plugins.smartsmtp.Smartsmtp} = {}; | ||||
|  | ||||
|   constructor(apiTokenArg: string) { | ||||
|     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) { | ||||
|     let requestUrl = routeArg; | ||||
|     const needsBaseUrlPrefix = !routeArg.startsWith('https://'); | ||||
| @@ -18,9 +36,9 @@ export class MailgunAccount { | ||||
|       method: 'GET', | ||||
|       headers: { | ||||
|         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; | ||||
|     if (!binaryArg) { | ||||
| @@ -37,9 +55,9 @@ export class MailgunAccount { | ||||
|       requestUrl, | ||||
|       { | ||||
|         headers: { | ||||
|           Authorization: `Basic ${plugins.smartstring.base64.encode(`api:${this.apiToken}`)}` | ||||
|           Authorization: `Basic ${plugins.smartstring.base64.encode(`api:${this.apiToken}`)}`, | ||||
|         }, | ||||
|         keepAlive: false | ||||
|         keepAlive: false, | ||||
|       }, | ||||
|       formFields | ||||
|     ); | ||||
| @@ -59,35 +77,25 @@ export class MailgunAccount { | ||||
|       { | ||||
|         name: 'from', | ||||
|         type: 'string', | ||||
|         payload: smartmailArg.options.from | ||||
|         payload: smartmailArg.options.from, | ||||
|       }, | ||||
|       { | ||||
|         name: 'to', | ||||
|         type: 'string', | ||||
|         payload: toArg | ||||
|         payload: toArg, | ||||
|       }, | ||||
|       { | ||||
|         name: 'subject', | ||||
|         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); | ||||
|  | ||||
|     for (const attachment of smartmailArg.attachments) { | ||||
| @@ -95,16 +103,29 @@ export class MailgunAccount { | ||||
|         name: 'attachment', | ||||
|         type: 'Buffer', | ||||
|         payload: attachment.contentBuffer, | ||||
|         fileName: attachment.parsedPath.base | ||||
|         fileName: attachment.parsedPath.base, | ||||
|       }); | ||||
|     } | ||||
|  | ||||
|     const response = await this.postFormData(`/${domain}/messages`, formFields); | ||||
|     if (response.statusCode === 200) { | ||||
|       return response.body; | ||||
|     if (smartmailArg.getBody(dataArg)) { | ||||
|       console.log('All requirements for API met'); | ||||
|       const response = await this.postFormData(`/${domain}/messages`, formFields); | ||||
|       if (response.statusCode === 200) { | ||||
|         console.log(`Sent mail with subject ${smartmailArg.getSubject(dataArg)} to ${toArg} using the mailgun API`); | ||||
|         return response.body; | ||||
|       } else { | ||||
|         console.log(response.body); | ||||
|         throw new Error('could not send email'); | ||||
|       } | ||||
|     } else { | ||||
|       console.log(response.body); | ||||
|       throw new Error('could not send email'); | ||||
|       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`); | ||||
|     } | ||||
|   } | ||||
|  | ||||
| @@ -119,7 +140,7 @@ export class MailgunAccount { | ||||
|       from: responseBody.From, | ||||
|       body: responseBody['body-html'], | ||||
|       subject: responseBody.Subject, | ||||
|       creationObjectRef: responseBody | ||||
|       creationObjectRef: responseBody, | ||||
|     }); | ||||
|  | ||||
|     // lets care about attachments | ||||
| @@ -131,7 +152,7 @@ export class MailgunAccount { | ||||
|           new plugins.smartfile.Smartfile({ | ||||
|             path: `./${attachmentName}`, | ||||
|             base: `./${attachmentName}`, | ||||
|             contentBuffer: attachmentContents.body | ||||
|             contentBuffer: attachmentContents.body, | ||||
|           }) | ||||
|         ); | ||||
|       } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user