Compare commits
57 Commits
Author | SHA1 | Date | |
---|---|---|---|
31d8baa99c | |||
3322398901 | |||
ece6b6feda | |||
108f483823 | |||
0412cecfe5 | |||
4cb32e8460 | |||
3abc973d88 | |||
b08b9abb38 | |||
13e064207f | |||
5d4b0eff68 | |||
29a7472b3c | |||
5490be1033 | |||
6ee7c6af76 | |||
aa1750524b | |||
da7c57165e | |||
708c05d1e3 | |||
58b7f602bd | |||
23507b83fc | |||
c160cfd94e | |||
920399a495 | |||
168cb9d5d7 | |||
f61e119912 | |||
f5c3fc6ee6 | |||
6643b9c3dd | |||
157bf6a893 | |||
5d5e2076d8 | |||
815b455dbb | |||
9d5907a7a7 | |||
0212d476c2 | |||
2b8aadc816 | |||
1149ee6759 | |||
fa012409ff | |||
40bd96bf41 | |||
f3591bdc67 | |||
ece9508161 | |||
761d7e78de | |||
0c44cf09b4 | |||
aee7c80e26 | |||
c60702fb49 | |||
354c38a429 | |||
f477cad6e9 | |||
631aaadb43 | |||
a831cf7e9a | |||
e6125c9a13 | |||
a203965e29 | |||
4305b53e35 | |||
b7c657a930 | |||
0a758cdb60 | |||
b97740094b | |||
8947c6e8de | |||
46e2f54974 | |||
6beb085488 | |||
a32b31b86d | |||
1371bd0a55 | |||
fa23bab37b | |||
2e77b3672a | |||
d734ac821e |
20
.gitignore
vendored
Normal file
20
.gitignore
vendored
Normal file
@ -0,0 +1,20 @@
|
||||
.nogit/
|
||||
|
||||
# artifacts
|
||||
coverage/
|
||||
public/
|
||||
pages/
|
||||
|
||||
# installs
|
||||
node_modules/
|
||||
|
||||
# caches
|
||||
.yarn/
|
||||
.cache/
|
||||
.rpt2_cache
|
||||
|
||||
# builds
|
||||
dist/
|
||||
dist_*/
|
||||
|
||||
# custom
|
112
.gitlab-ci.yml
112
.gitlab-ci.yml
@ -1,59 +1,137 @@
|
||||
image: hosttoday/ht-docker-node:npmts
|
||||
# gitzone ci_default
|
||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||
|
||||
cache:
|
||||
paths:
|
||||
- .npmci_cache/
|
||||
key: '$CI_BUILD_STAGE'
|
||||
|
||||
stages:
|
||||
- test
|
||||
- release
|
||||
- trigger
|
||||
- pages
|
||||
- security
|
||||
- test
|
||||
- release
|
||||
- metadata
|
||||
|
||||
testLEGACY:
|
||||
stage: test
|
||||
# ====================
|
||||
# security stage
|
||||
# ====================
|
||||
mirror:
|
||||
stage: security
|
||||
script:
|
||||
- npmci test legacy
|
||||
- npmci git mirror
|
||||
only:
|
||||
- tags
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
auditProductionDependencies:
|
||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||
stage: security
|
||||
script:
|
||||
- npmci npm prepare
|
||||
- npmci command npm install --production --ignore-scripts
|
||||
- npmci command npm config set registry https://registry.npmjs.org
|
||||
- npmci command npm audit --audit-level=high --only=prod --production
|
||||
tags:
|
||||
- docker
|
||||
|
||||
auditDevDependencies:
|
||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||
stage: security
|
||||
script:
|
||||
- npmci npm prepare
|
||||
- npmci command npm install --ignore-scripts
|
||||
- npmci command npm config set registry https://registry.npmjs.org
|
||||
- npmci command npm audit --audit-level=high --only=dev
|
||||
tags:
|
||||
- docker
|
||||
allow_failure: true
|
||||
|
||||
testLTS:
|
||||
# ====================
|
||||
# test stage
|
||||
# ====================
|
||||
|
||||
testStable:
|
||||
stage: test
|
||||
script:
|
||||
- npmci test lts
|
||||
- npmci npm prepare
|
||||
- npmci node install stable
|
||||
- npmci npm install
|
||||
- npmci npm test
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- docker
|
||||
|
||||
testSTABLE:
|
||||
testBuild:
|
||||
stage: test
|
||||
script:
|
||||
- npmci test stable
|
||||
- npmci npm prepare
|
||||
- npmci node install stable
|
||||
- npmci npm install
|
||||
- npmci command npm run build
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- docker
|
||||
|
||||
release:
|
||||
stage: release
|
||||
script:
|
||||
- npmci publish
|
||||
- npmci node install stable
|
||||
- npmci npm publish
|
||||
only:
|
||||
- tags
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
# ====================
|
||||
# metadata stage
|
||||
# ====================
|
||||
codequality:
|
||||
stage: metadata
|
||||
allow_failure: true
|
||||
only:
|
||||
- tags
|
||||
script:
|
||||
- npmci command npm install -g tslint typescript
|
||||
- npmci npm prepare
|
||||
- npmci npm install
|
||||
- npmci command "tslint -c tslint.json ./ts/**/*.ts"
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- priv
|
||||
|
||||
trigger:
|
||||
stage: trigger
|
||||
stage: metadata
|
||||
script:
|
||||
- npmci trigger
|
||||
only:
|
||||
- tags
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
pages:
|
||||
image: hosttoday/ht-docker-node:npmpage
|
||||
stage: pages
|
||||
stage: metadata
|
||||
script:
|
||||
- npmci command npmpage --publish gitlab
|
||||
- npmci node install lts
|
||||
- npmci command npm install -g @gitzone/tsdoc
|
||||
- npmci npm prepare
|
||||
- npmci npm install
|
||||
- npmci command tsdoc
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
only:
|
||||
- tags
|
||||
artifacts:
|
||||
expire_in: 1 week
|
||||
paths:
|
||||
- public
|
||||
allow_failure: true
|
||||
|
8
.gitlab/.gitlab-webide.yml
Normal file
8
.gitlab/.gitlab-webide.yml
Normal file
@ -0,0 +1,8 @@
|
||||
terminal:
|
||||
image: registry.gitlab.com/hosttoday/ht-docker-node:stable
|
||||
before_script:
|
||||
- npm install -g npm-check
|
||||
variables:
|
||||
RAILS_ENV: "test"
|
||||
NODE_ENV: "test"
|
||||
|
4
.snyk
Normal file
4
.snyk
Normal file
@ -0,0 +1,4 @@
|
||||
# Snyk (https://snyk.io) policy file, patches or ignores known vulnerabilities.
|
||||
version: v1.13.5
|
||||
ignore: {}
|
||||
patch: {}
|
29
.vscode/launch.json
vendored
Normal file
29
.vscode/launch.json
vendored
Normal file
@ -0,0 +1,29 @@
|
||||
{
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "current file",
|
||||
"type": "node",
|
||||
"request": "launch",
|
||||
"args": [
|
||||
"${relativeFile}"
|
||||
],
|
||||
"runtimeArgs": ["-r", "@gitzone/tsrun"],
|
||||
"cwd": "${workspaceRoot}",
|
||||
"protocol": "inspector",
|
||||
"internalConsoleOptions": "openOnSessionStart"
|
||||
},
|
||||
{
|
||||
"name": "test.ts",
|
||||
"type": "node",
|
||||
"request": "launch",
|
||||
"args": [
|
||||
"test/test.ts"
|
||||
],
|
||||
"runtimeArgs": ["-r", "@gitzone/tsrun"],
|
||||
"cwd": "${workspaceRoot}",
|
||||
"protocol": "inspector",
|
||||
"internalConsoleOptions": "openOnSessionStart"
|
||||
}
|
||||
]
|
||||
}
|
26
.vscode/settings.json
vendored
Normal file
26
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,26 @@
|
||||
{
|
||||
"json.schemas": [
|
||||
{
|
||||
"fileMatch": ["/npmextra.json"],
|
||||
"schema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"npmci": {
|
||||
"type": "object",
|
||||
"description": "settings for npmci"
|
||||
},
|
||||
"gitzone": {
|
||||
"type": "object",
|
||||
"description": "settings for gitzone",
|
||||
"properties": {
|
||||
"projectType": {
|
||||
"type": "string",
|
||||
"enum": ["website", "element", "service", "npm", "wcc"]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
19
license
Normal file
19
license
Normal file
@ -0,0 +1,19 @@
|
||||
Copyright (c) 2019 Lossless GmbH (hello@lossless.com)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
18
npmextra.json
Normal file
18
npmextra.json
Normal file
@ -0,0 +1,18 @@
|
||||
{
|
||||
"gitzone": {
|
||||
"projectType": "npm",
|
||||
"module": {
|
||||
"githost": "gitlab.com",
|
||||
"gitscope": "mojoio",
|
||||
"gitrepo": "mailgun",
|
||||
"shortDescription": "an api abstraction package for mailgun",
|
||||
"npmPackagename": "@mojoio/mailgun",
|
||||
"license": "MIT",
|
||||
"projectDomain": "mojo.io"
|
||||
}
|
||||
},
|
||||
"npmci": {
|
||||
"npmGlobalTools": [],
|
||||
"npmAccessLevel": "public"
|
||||
}
|
||||
}
|
11217
package-lock.json
generated
Normal file
11217
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
58
package.json
58
package.json
@ -1,22 +1,46 @@
|
||||
{
|
||||
"name": "mgun",
|
||||
"version": "1.0.1",
|
||||
"description": "easy mailgun API wrapper",
|
||||
"main": "dist/index.js",
|
||||
"scripts": {
|
||||
"test": "(npmts)"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+ssh://git@gitlab.com/mojoio/mgun.git"
|
||||
},
|
||||
"keywords": [
|
||||
"mailgun"
|
||||
],
|
||||
"name": "@mojoio/mailgun",
|
||||
"version": "1.0.30",
|
||||
"private": false,
|
||||
"description": "an api abstraction package for mailgun",
|
||||
"main": "dist_ts/index.js",
|
||||
"typings": "dist_ts/index.d.ts",
|
||||
"author": "Lossless GmbH",
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://gitlab.com/mojoio/mgun/issues"
|
||||
"scripts": {
|
||||
"test": "(tstest test/)",
|
||||
"build": "(tsbuild --web)",
|
||||
"format": "(gitzone format)"
|
||||
},
|
||||
"homepage": "https://gitlab.com/mojoio/mgun#README"
|
||||
"devDependencies": {
|
||||
"@gitzone/tsbuild": "^2.1.25",
|
||||
"@gitzone/tstest": "^1.0.44",
|
||||
"@pushrocks/qenv": "^4.0.10",
|
||||
"@pushrocks/tapbundle": "^3.2.9",
|
||||
"@types/node": "^14.0.27",
|
||||
"tslint": "^6.1.3",
|
||||
"tslint-config-prettier": "^1.15.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"@pushrocks/smartfile": "^8.0.0",
|
||||
"@pushrocks/smartmail": "^1.0.18",
|
||||
"@pushrocks/smartrequest": "^1.1.47",
|
||||
"@pushrocks/smartsmtp": "^1.0.8",
|
||||
"@pushrocks/smartstring": "^3.0.18"
|
||||
},
|
||||
"files": [
|
||||
"ts/**/*",
|
||||
"ts_web/**/*",
|
||||
"dist/**/*",
|
||||
"dist_*/**/*",
|
||||
"dist_ts/**/*",
|
||||
"dist_ts_web/**/*",
|
||||
"assets/**/*",
|
||||
"cli.js",
|
||||
"npmextra.json",
|
||||
"readme.md"
|
||||
],
|
||||
"browserslist": [
|
||||
"last 1 chrome versions"
|
||||
]
|
||||
}
|
||||
|
3
qenv.yml
Normal file
3
qenv.yml
Normal file
@ -0,0 +1,3 @@
|
||||
required:
|
||||
- MAILGUN_API_TOKEN
|
||||
- MAILGUN_SMTP_CREDENTIALS
|
39
readme.md
Normal file
39
readme.md
Normal file
@ -0,0 +1,39 @@
|
||||
# @mojoio/mailgun
|
||||
an api abstraction package for mailgun
|
||||
|
||||
## Availabililty and Links
|
||||
* [npmjs.org (npm package)](https://www.npmjs.com/package/@mojoio/mailgun)
|
||||
* [gitlab.com (source)](https://gitlab.com/mojoio/mailgun)
|
||||
* [github.com (source mirror)](https://github.com/mojoio/mailgun)
|
||||
* [docs (typedoc)](https://mojoio.gitlab.io/mailgun/)
|
||||
|
||||
## Status for master
|
||||
|
||||
Status Category | Status Badge
|
||||
-- | --
|
||||
GitLab Pipelines | [](https://lossless.cloud)
|
||||
GitLab Pipline Test Coverage | [](https://lossless.cloud)
|
||||
npm | [](https://lossless.cloud)
|
||||
Snyk | [](https://lossless.cloud)
|
||||
TypeScript Support | [](https://lossless.cloud)
|
||||
node Support | [](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
||||
Code Style | [](https://lossless.cloud)
|
||||
PackagePhobia (total standalone install weight) | [](https://lossless.cloud)
|
||||
PackagePhobia (package size on registry) | [](https://lossless.cloud)
|
||||
BundlePhobia (total size when bundled) | [](https://lossless.cloud)
|
||||
Platform support | [](https://lossless.cloud) [](https://lossless.cloud)
|
||||
|
||||
## Usage
|
||||
|
||||
Use TypeScript for best in class intellisense.
|
||||
|
||||
## Contribution
|
||||
|
||||
We are always happy for code contributions. If you are not the code contributing type that is ok. Still, maintaining Open Source repositories takes considerable time and thought. If you like the quality of what we do and our modules are useful to you we would appreciate a little monthly contribution: You can [contribute one time](https://lossless.link/contribute-onetime) or [contribute monthly](https://lossless.link/contribute). :)
|
||||
|
||||
For further information read the linked docs at the top of this readme.
|
||||
|
||||
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
|
||||
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)
|
||||
|
||||
[](https://maintainedby.lossless.com)
|
55
test/test.ts
Normal file
55
test/test.ts
Normal file
@ -0,0 +1,55 @@
|
||||
import { expect, tap } from '@pushrocks/tapbundle';
|
||||
import { Qenv } from '@pushrocks/qenv';
|
||||
import * as smartmail from '@pushrocks/smartmail';
|
||||
|
||||
const testQenv = new Qenv('./', './.nogit');
|
||||
|
||||
import * as mailgun from '../ts/index';
|
||||
import { IMailgunMessage } from '../ts/index';
|
||||
|
||||
let testMailgunAccount: mailgun.MailgunAccount;
|
||||
let testSmartmail: smartmail.Smartmail<IMailgunMessage>;
|
||||
|
||||
tap.test('should create a mailgun account', async () => {
|
||||
testMailgunAccount = new mailgun.MailgunAccount(testQenv.getEnvVarOnDemand('MAILGUN_API_TOKEN'));
|
||||
testMailgunAccount.addSmtpCredentials(testQenv.getEnvVarOnDemand('MAILGUN_SMTP_CREDENTIALS'));
|
||||
expect(testMailgunAccount).to.be.instanceOf(mailgun.MailgunAccount);
|
||||
});
|
||||
|
||||
tap.test('should create a smartmail', async () => {
|
||||
testSmartmail = new smartmail.Smartmail({
|
||||
body: 'hi there. This is the body.',
|
||||
from: 'Lossless GmbH <noreply@mail.lossless.com>',
|
||||
subject: 'TestMessage from @mojoio/mailgun test',
|
||||
});
|
||||
expect(testSmartmail).to.be.instanceOf(smartmail.Smartmail);
|
||||
});
|
||||
|
||||
tap.test('should send a smartmail', async () => {
|
||||
await testMailgunAccount.sendSmartMail(testSmartmail, 'Sandbox Team <sandbox@mail.git.zone>');
|
||||
});
|
||||
|
||||
tap.test('should send a smartmail with empty body', async () => {
|
||||
const emptyBodySmartmail = new smartmail.Smartmail<IMailgunMessage>({
|
||||
body: '',
|
||||
from: 'Lossless GmbH <noreply@mail.lossless.one>',
|
||||
subject: 'A message with no body from @mojoio/mailgun test',
|
||||
});
|
||||
await testMailgunAccount.sendSmartMail(
|
||||
emptyBodySmartmail,
|
||||
'Sandbox Team <sandbox@mail.git.zone>'
|
||||
);
|
||||
});
|
||||
|
||||
tap.test('should retrieve a mail using a retrieval url', async () => {
|
||||
const result = await testMailgunAccount.retrieveSmartMailFromMessageUrl(
|
||||
'https://sw.api.mailgun.net/v3/domains/mail.lossless.one/messages/AgMFnnnAKC8xp_dDa79LyoxhloxtaVmnRA=='
|
||||
);
|
||||
if (result) {
|
||||
result.options.subject = 'hi there. This is a testmail with attachment';
|
||||
result.options.from = 'noreply@mail.lossless.com';
|
||||
await testMailgunAccount.sendSmartMail(result, 'Sandbox Team <sandbox@mail.git.zone>');
|
||||
}
|
||||
});
|
||||
|
||||
tap.start();
|
2
ts/index.ts
Normal file
2
ts/index.ts
Normal file
@ -0,0 +1,2 @@
|
||||
export * from './mailgun.classes.account';
|
||||
export * from './interfaces';
|
2
ts/interfaces/index.ts
Normal file
2
ts/interfaces/index.ts
Normal file
@ -0,0 +1,2 @@
|
||||
export * from './message';
|
||||
export * from './notification';
|
30
ts/interfaces/message.ts
Normal file
30
ts/interfaces/message.ts
Normal file
@ -0,0 +1,30 @@
|
||||
export interface IMailgunMessage {
|
||||
Received: string;
|
||||
From: string;
|
||||
'X-Envelope-From': string;
|
||||
recipients: string;
|
||||
'X-Google-Dkim-Signature': string;
|
||||
To: string;
|
||||
'message-headers': [[string, string]];
|
||||
'Dkim-Signature': string;
|
||||
'content-id-map': any;
|
||||
subject: 'test2';
|
||||
'stripped-html': string;
|
||||
'X-Mailgun-Incoming': 'Yes';
|
||||
'X-Received': string;
|
||||
'X-Gm-Message-State': string;
|
||||
'body-plain': string;
|
||||
attachments: Array<{
|
||||
url: string;
|
||||
'content-type': string;
|
||||
name: string;
|
||||
size: number;
|
||||
}>;
|
||||
'body-html': string;
|
||||
'Mime-Version': string;
|
||||
Date: string;
|
||||
'Message-Id': string;
|
||||
'Content-Type': string;
|
||||
'X-Google-Smtp-Source': string;
|
||||
Subject: string;
|
||||
}
|
47
ts/interfaces/notification.ts
Normal file
47
ts/interfaces/notification.ts
Normal file
@ -0,0 +1,47 @@
|
||||
export interface IMailgunNotification {
|
||||
timestamp: string;
|
||||
token: string;
|
||||
signature: string;
|
||||
domain: string;
|
||||
'Received-Spf': string;
|
||||
From: string;
|
||||
'Return-Path': string;
|
||||
'Arc-Seal': string[];
|
||||
'Delivered-To': string;
|
||||
'X-Google-Dkim-Signature': string;
|
||||
To: string;
|
||||
'Dkim-Signature': string;
|
||||
subject: string;
|
||||
from: string;
|
||||
'X-Received': string[];
|
||||
'Ironport-Sdr': string;
|
||||
'Arc-Authentication-Results': string[];
|
||||
'Arc-Message-Signature': string[];
|
||||
Date: string;
|
||||
'Message-Id': string;
|
||||
'Mime-Version': string;
|
||||
Received: string[];
|
||||
'Authentication-Results': string[];
|
||||
'X-Ipas-Result': string;
|
||||
'message-url': string;
|
||||
'message-headers': string;
|
||||
'Reply-To': string;
|
||||
recipient: string;
|
||||
sender: string;
|
||||
'X-Mailgun-Incoming': string;
|
||||
'X-Forwarded-For': string;
|
||||
'X-Gm-Message-State': string;
|
||||
'X-Google-Smtp-Source': string;
|
||||
'X-Envelope-From': string;
|
||||
'Content-Type': string;
|
||||
'X-Forwarded-To': string;
|
||||
Subject: string;
|
||||
attachments: string;
|
||||
'body-plain': string;
|
||||
'stripped-text': string;
|
||||
'stripped-html': string;
|
||||
'stripped-signature': string;
|
||||
|
||||
// Lossless specific
|
||||
'X-Lossless-Auth': string;
|
||||
}
|
177
ts/mailgun.classes.account.ts
Normal file
177
ts/mailgun.classes.account.ts
Normal file
@ -0,0 +1,177 @@
|
||||
import * as plugins from './mailgun.plugins';
|
||||
import * as interfaces from './interfaces';
|
||||
|
||||
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://');
|
||||
needsBaseUrlPrefix ? (requestUrl = `${this.baseUrl}${routeArg}`) : null;
|
||||
console.log(requestUrl);
|
||||
const requestOptions: plugins.smartrequest.ISmartRequestOptions = {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
Authorization: `Basic ${plugins.smartstring.base64.encode(`api:${this.apiToken}`)}`,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
keepAlive: false,
|
||||
};
|
||||
let response: plugins.smartrequest.IExtendedIncomingMessage;
|
||||
if (!binaryArg) {
|
||||
response = await plugins.smartrequest.request(requestUrl, requestOptions);
|
||||
} else {
|
||||
response = await plugins.smartrequest.getBinary(requestUrl, requestOptions);
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
public async postFormData(routeArg: string, formFields: plugins.smartrequest.IFormField[]) {
|
||||
const requestUrl = `${this.baseUrl}${routeArg}`; // TODO;
|
||||
const response = await plugins.smartrequest.postFormData(
|
||||
requestUrl,
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Basic ${plugins.smartstring.base64.encode(`api:${this.apiToken}`)}`,
|
||||
},
|
||||
keepAlive: false,
|
||||
},
|
||||
formFields
|
||||
);
|
||||
return response;
|
||||
}
|
||||
|
||||
/**
|
||||
* sends a SmartMail
|
||||
*/
|
||||
public async sendSmartMail(
|
||||
smartmailArg: plugins.smartmail.Smartmail<interfaces.IMailgunMessage>,
|
||||
toArg: string,
|
||||
dataArg = {}
|
||||
) {
|
||||
const domain = smartmailArg.options.from.split('@')[1].replace('>', '');
|
||||
const formFields: plugins.smartrequest.IFormField[] = [
|
||||
{
|
||||
name: 'from',
|
||||
type: 'string',
|
||||
payload: smartmailArg.options.from,
|
||||
},
|
||||
{
|
||||
name: 'to',
|
||||
type: 'string',
|
||||
payload: toArg,
|
||||
},
|
||||
{
|
||||
name: 'subject',
|
||||
type: 'string',
|
||||
payload: smartmailArg.getSubject(dataArg),
|
||||
},
|
||||
{
|
||||
name: 'html',
|
||||
type: 'string',
|
||||
payload: smartmailArg.getBody(dataArg),
|
||||
},
|
||||
];
|
||||
|
||||
console.log(smartmailArg.attachments);
|
||||
|
||||
for (const attachment of smartmailArg.attachments) {
|
||||
formFields.push({
|
||||
name: 'attachment',
|
||||
type: 'Buffer',
|
||||
payload: attachment.contentBuffer,
|
||||
fileName: attachment.parsedPath.base,
|
||||
});
|
||||
}
|
||||
|
||||
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(
|
||||
'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) {
|
||||
const response = await this.getRequest(messageUrlArg);
|
||||
if (response.statusCode === 404) {
|
||||
console.log(response.body.message);
|
||||
return null;
|
||||
}
|
||||
const responseBody: interfaces.IMailgunMessage = response.body;
|
||||
const smartmail = new plugins.smartmail.Smartmail<interfaces.IMailgunMessage>({
|
||||
from: responseBody.From,
|
||||
body: responseBody['body-html'],
|
||||
subject: responseBody.Subject,
|
||||
creationObjectRef: responseBody,
|
||||
});
|
||||
|
||||
// lets care about attachments
|
||||
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);
|
||||
smartmail.addAttachment(
|
||||
new plugins.smartfile.Smartfile({
|
||||
path: `./${attachmentName}`,
|
||||
base: `./${attachmentName}`,
|
||||
contentBuffer: attachmentContents.body,
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return smartmail;
|
||||
}
|
||||
|
||||
public async retrieveSmartMailFromNotifyPayload(notifyPayloadArg: any) {
|
||||
return await this.retrieveSmartMailFromMessageUrl(notifyPayloadArg['message-url']);
|
||||
}
|
||||
}
|
8
ts/mailgun.plugins.ts
Normal file
8
ts/mailgun.plugins.ts
Normal file
@ -0,0 +1,8 @@
|
||||
// @pushrocks scope
|
||||
import * as smartfile from '@pushrocks/smartfile';
|
||||
import * as smartmail from '@pushrocks/smartmail';
|
||||
import * as smartrequest from '@pushrocks/smartrequest';
|
||||
import * as smartsmtp from '@pushrocks/smartsmtp';
|
||||
import * as smartstring from '@pushrocks/smartstring';
|
||||
|
||||
export { smartfile, smartmail, smartrequest, smartsmtp, smartstring };
|
16
tslint.json
16
tslint.json
@ -1,3 +1,17 @@
|
||||
{
|
||||
"extends": "tslint-config-standard"
|
||||
"extends": ["tslint:latest", "tslint-config-prettier"],
|
||||
"rules": {
|
||||
"semicolon": [true, "always"],
|
||||
"no-console": false,
|
||||
"ordered-imports": false,
|
||||
"object-literal-sort-keys": false,
|
||||
"member-ordering": {
|
||||
"options":{
|
||||
"order": [
|
||||
"static-method"
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
"defaultSeverity": "warning"
|
||||
}
|
||||
|
Reference in New Issue
Block a user