@deenruv/email-plugin 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +23 -0
- package/README.md +85 -0
- package/dev-mailbox.html +218 -0
- package/lib/index.d.ts +13 -0
- package/lib/index.js +30 -0
- package/lib/index.js.map +1 -0
- package/lib/src/attachment-utils.d.ts +3 -0
- package/lib/src/attachment-utils.js +66 -0
- package/lib/src/attachment-utils.js.map +1 -0
- package/lib/src/common.d.ts +4 -0
- package/lib/src/common.js +17 -0
- package/lib/src/common.js.map +1 -0
- package/lib/src/constants.d.ts +2 -0
- package/lib/src/constants.js +6 -0
- package/lib/src/constants.js.map +1 -0
- package/lib/src/dev-mailbox.d.ts +14 -0
- package/lib/src/dev-mailbox.js +116 -0
- package/lib/src/dev-mailbox.js.map +1 -0
- package/lib/src/email-processor.d.ts +21 -0
- package/lib/src/email-processor.js +109 -0
- package/lib/src/email-processor.js.map +1 -0
- package/lib/src/email-send-event.d.ts +18 -0
- package/lib/src/email-send-event.js +24 -0
- package/lib/src/email-send-event.js.map +1 -0
- package/lib/src/event-listener.d.ts +19 -0
- package/lib/src/event-listener.js +25 -0
- package/lib/src/event-listener.js.map +1 -0
- package/lib/src/generator/email-generator.d.ts +25 -0
- package/lib/src/generator/email-generator.js +3 -0
- package/lib/src/generator/email-generator.js.map +1 -0
- package/lib/src/generator/handlebars-mjml-generator.d.ts +19 -0
- package/lib/src/generator/handlebars-mjml-generator.js +78 -0
- package/lib/src/generator/handlebars-mjml-generator.js.map +1 -0
- package/lib/src/generator/noop-email-generator.d.ts +11 -0
- package/lib/src/generator/noop-email-generator.js +13 -0
- package/lib/src/generator/noop-email-generator.js.map +1 -0
- package/lib/src/generator/react-email-generator.d.ts +7 -0
- package/lib/src/generator/react-email-generator.js +40 -0
- package/lib/src/generator/react-email-generator.js.map +1 -0
- package/lib/src/handler/default-email-handlers.d.ts +32 -0
- package/lib/src/handler/default-email-handlers.js +111 -0
- package/lib/src/handler/default-email-handlers.js.map +1 -0
- package/lib/src/handler/event-handler.d.ts +276 -0
- package/lib/src/handler/event-handler.js +396 -0
- package/lib/src/handler/event-handler.js.map +1 -0
- package/lib/src/handler/mock-events.d.ts +5 -0
- package/lib/src/handler/mock-events.js +119 -0
- package/lib/src/handler/mock-events.js.map +1 -0
- package/lib/src/plugin.d.ts +301 -0
- package/lib/src/plugin.js +428 -0
- package/lib/src/plugin.js.map +1 -0
- package/lib/src/sender/email-sender.d.ts +45 -0
- package/lib/src/sender/email-sender.js +3 -0
- package/lib/src/sender/email-sender.js.map +1 -0
- package/lib/src/sender/nodemailer-email-sender.d.ts +37 -0
- package/lib/src/sender/nodemailer-email-sender.js +151 -0
- package/lib/src/sender/nodemailer-email-sender.js.map +1 -0
- package/lib/src/template-loader/file-based-template-loader.d.ts +17 -0
- package/lib/src/template-loader/file-based-template-loader.js +37 -0
- package/lib/src/template-loader/file-based-template-loader.js.map +1 -0
- package/lib/src/template-loader/react-email-template-loader.d.ts +6 -0
- package/lib/src/template-loader/react-email-template-loader.js +14 -0
- package/lib/src/template-loader/react-email-template-loader.js.map +1 -0
- package/lib/src/template-loader/template-loader.d.ts +42 -0
- package/lib/src/template-loader/template-loader.js +3 -0
- package/lib/src/template-loader/template-loader.js.map +1 -0
- package/lib/src/types.d.ts +453 -0
- package/lib/src/types.js +3 -0
- package/lib/src/types.js.map +1 -0
- package/package.json +51 -0
- package/templates/email-address-change/body.hbs +20 -0
- package/templates/email-verification/body.hbs +20 -0
- package/templates/order-confirmation/body.hbs +133 -0
- package/templates/partials/footer.hbs +10 -0
- package/templates/partials/header.hbs +18 -0
- package/templates/password-reset/body.hbs +24 -0
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.mockEmailAddressChangeEvent = exports.mockPasswordResetEvent = exports.mockAccountRegistrationEvent = exports.mockOrderStateTransitionEvent = void 0;
|
|
4
|
+
const generated_shop_types_1 = require("@deenruv/common/lib/generated-shop-types");
|
|
5
|
+
const core_1 = require("@deenruv/core");
|
|
6
|
+
exports.mockOrderStateTransitionEvent = new core_1.OrderStateTransitionEvent("ArrangingPayment", "PaymentSettled", {}, new core_1.Order({
|
|
7
|
+
id: "6",
|
|
8
|
+
currencyCode: core_1.CurrencyCode.USD,
|
|
9
|
+
createdAt: "2018-10-31T11:18:29.261Z",
|
|
10
|
+
updatedAt: "2018-10-31T15:24:17.000Z",
|
|
11
|
+
orderPlacedAt: "2018-10-31T13:54:17.000Z",
|
|
12
|
+
code: "T3EPGJKTVZPBD6Z9",
|
|
13
|
+
state: "ArrangingPayment",
|
|
14
|
+
active: true,
|
|
15
|
+
customer: new core_1.Customer({
|
|
16
|
+
id: "3",
|
|
17
|
+
firstName: "Test",
|
|
18
|
+
lastName: "Customer",
|
|
19
|
+
emailAddress: "test@test.com",
|
|
20
|
+
}),
|
|
21
|
+
lines: [
|
|
22
|
+
new core_1.OrderLine({
|
|
23
|
+
id: "5",
|
|
24
|
+
featuredAsset: {
|
|
25
|
+
preview: "/mailbox/placeholder-image",
|
|
26
|
+
},
|
|
27
|
+
productVariant: new core_1.ProductVariant({
|
|
28
|
+
id: "2",
|
|
29
|
+
name: "Curvy Monitor 24 inch",
|
|
30
|
+
sku: "C24F390",
|
|
31
|
+
}),
|
|
32
|
+
quantity: 1,
|
|
33
|
+
listPrice: 14374,
|
|
34
|
+
listPriceIncludesTax: true,
|
|
35
|
+
adjustments: [
|
|
36
|
+
{
|
|
37
|
+
adjustmentSource: "Promotion:1",
|
|
38
|
+
type: generated_shop_types_1.AdjustmentType.PROMOTION,
|
|
39
|
+
amount: -1000,
|
|
40
|
+
description: "$10 off computer equipment",
|
|
41
|
+
},
|
|
42
|
+
],
|
|
43
|
+
taxLines: [],
|
|
44
|
+
}),
|
|
45
|
+
new core_1.OrderLine({
|
|
46
|
+
id: "6",
|
|
47
|
+
featuredAsset: {
|
|
48
|
+
preview: "/mailbox/placeholder-image",
|
|
49
|
+
},
|
|
50
|
+
productVariant: new core_1.ProductVariant({
|
|
51
|
+
id: "4",
|
|
52
|
+
name: "Hard Drive 1TB",
|
|
53
|
+
sku: "IHD455T1",
|
|
54
|
+
}),
|
|
55
|
+
quantity: 1,
|
|
56
|
+
listPrice: 3799,
|
|
57
|
+
listPriceIncludesTax: true,
|
|
58
|
+
adjustments: [],
|
|
59
|
+
taxLines: [],
|
|
60
|
+
}),
|
|
61
|
+
],
|
|
62
|
+
subTotal: 15144,
|
|
63
|
+
subTotalWithTax: 18173,
|
|
64
|
+
shipping: 1000,
|
|
65
|
+
shippingLines: [
|
|
66
|
+
new core_1.ShippingLine({
|
|
67
|
+
listPrice: 1000,
|
|
68
|
+
listPriceIncludesTax: true,
|
|
69
|
+
taxLines: [{ taxRate: 20, description: "shipping tax" }],
|
|
70
|
+
shippingMethod: {
|
|
71
|
+
code: "express-flat-rate",
|
|
72
|
+
name: "Express Shipping",
|
|
73
|
+
description: "Express Shipping",
|
|
74
|
+
id: "2",
|
|
75
|
+
},
|
|
76
|
+
}),
|
|
77
|
+
],
|
|
78
|
+
surcharges: [],
|
|
79
|
+
shippingAddress: {
|
|
80
|
+
fullName: "Test Customer",
|
|
81
|
+
company: "",
|
|
82
|
+
streetLine1: "6000 Pagac Land",
|
|
83
|
+
streetLine2: "",
|
|
84
|
+
city: "Port Kirsten",
|
|
85
|
+
province: "Avon",
|
|
86
|
+
postalCode: "ZU32 9CP",
|
|
87
|
+
country: "Cabo Verde",
|
|
88
|
+
phoneNumber: "",
|
|
89
|
+
},
|
|
90
|
+
payments: [],
|
|
91
|
+
}));
|
|
92
|
+
exports.mockAccountRegistrationEvent = new core_1.AccountRegistrationEvent({}, new core_1.User({
|
|
93
|
+
verified: false,
|
|
94
|
+
authenticationMethods: [
|
|
95
|
+
new core_1.NativeAuthenticationMethod({
|
|
96
|
+
identifier: "test@test.com",
|
|
97
|
+
verificationToken: "MjAxOC0xMS0xM1QxNToxNToxNC42ODda_US2U6UK1WZC7NDAX",
|
|
98
|
+
}),
|
|
99
|
+
],
|
|
100
|
+
identifier: "test@test.com",
|
|
101
|
+
}));
|
|
102
|
+
exports.mockPasswordResetEvent = new core_1.PasswordResetEvent({}, new core_1.User({
|
|
103
|
+
identifier: "test@test.com",
|
|
104
|
+
authenticationMethods: [
|
|
105
|
+
new core_1.NativeAuthenticationMethod({
|
|
106
|
+
passwordResetToken: "MjAxOS0wNC0xNVQxMzozMDozOC43MjFa_MA2FR6HRZBW7JWD6",
|
|
107
|
+
}),
|
|
108
|
+
],
|
|
109
|
+
}));
|
|
110
|
+
exports.mockEmailAddressChangeEvent = new core_1.IdentifierChangeRequestEvent({}, new core_1.User({
|
|
111
|
+
identifier: "old-address@test.com",
|
|
112
|
+
authenticationMethods: [
|
|
113
|
+
new core_1.NativeAuthenticationMethod({
|
|
114
|
+
pendingIdentifier: "new-address@test.com",
|
|
115
|
+
identifierChangeToken: "MjAxOS0wNC0xNVQxMzozMDozOC43MjFa_MA2FR6HRZBW7JWD6",
|
|
116
|
+
}),
|
|
117
|
+
],
|
|
118
|
+
}));
|
|
119
|
+
//# sourceMappingURL=mock-events.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mock-events.js","sourceRoot":"","sources":["../../../src/handler/mock-events.ts"],"names":[],"mappings":";;;AAAA,mFAA0E;AAC1E,wCAauB;AAEV,QAAA,6BAA6B,GAAG,IAAI,gCAAyB,CACxE,kBAAkB,EAClB,gBAAgB,EAChB,EAAS,EACT,IAAI,YAAK,CAAC;IACR,EAAE,EAAE,GAAG;IACP,YAAY,EAAE,mBAAY,CAAC,GAAG;IAC9B,SAAS,EAAE,0BAA0B;IACrC,SAAS,EAAE,0BAA0B;IACrC,aAAa,EAAE,0BAA0B;IACzC,IAAI,EAAE,kBAAkB;IACxB,KAAK,EAAE,kBAAkB;IACzB,MAAM,EAAE,IAAI;IACZ,QAAQ,EAAE,IAAI,eAAQ,CAAC;Q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sumer, NestModule, OnApplicationBootstrap, OnApplicationShutdown } from "@nestjs/common";
|
|
2
|
+
import { ModuleRef } from "@nestjs/core";
|
|
3
|
+
import { EventBus, JobQueueService, ProcessContext, Type } from "@deenruv/core";
|
|
4
|
+
import { EmailProcessor } from "./email-processor";
|
|
5
|
+
export { ReactComponentLoader } from "./template-loader/react-email-template-loader";
|
|
6
|
+
export { ReactComponentEmailGenerator } from "./generator/react-email-generator";
|
|
7
|
+
import { EmailPluginDevModeOptions, EmailPluginOptions, InitializedEmailPluginOptions } from "./types";
|
|
8
|
+
/**
|
|
9
|
+
* @description
|
|
10
|
+
* The EmailPlugin creates and sends transactional emails based on Deenruv events. By default, it uses an [MJML](https://mjml.io/)-based
|
|
11
|
+
* email generator to generate the email body and [Nodemailer](https://nodemailer.com/about/) to send the emails.
|
|
12
|
+
*
|
|
13
|
+
* ## High-level description
|
|
14
|
+
* Deenruv has an internal events system (see {@link EventBus}) that allows plugins to subscribe to events. The EmailPlugin is configured with {@link EmailEventHandler}s
|
|
15
|
+
* that listen for a specific event and when it is published, the handler defines which template to use to generate the resulting email.
|
|
16
|
+
*
|
|
17
|
+
* The plugin comes with a set of default handler for the following events:
|
|
18
|
+
* - Order confirmation
|
|
19
|
+
* - New customer email address verification
|
|
20
|
+
* - Password reset request
|
|
21
|
+
* - Email address change request
|
|
22
|
+
*
|
|
23
|
+
* You can also create your own handler and register them with the plugin - see the {@link EmailEventHandler} docs for more details.
|
|
24
|
+
*
|
|
25
|
+
* ## Installation
|
|
26
|
+
*
|
|
27
|
+
* `yarn add \@deenruv/email-plugin`
|
|
28
|
+
*
|
|
29
|
+
* or
|
|
30
|
+
*
|
|
31
|
+
* `npm install \@deenruv/email-plugin`
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* import { defaultEmailHandlers, EmailPlugin } from '\@deenruv/email-plugin';
|
|
36
|
+
*
|
|
37
|
+
* const config: DeenruvConfig = {
|
|
38
|
+
* // Add an instance of the plugin to the plugins array
|
|
39
|
+
* plugins: [
|
|
40
|
+
* EmailPlugin.init({
|
|
41
|
+
* handler: defaultEmailHandlers,
|
|
42
|
+
* templatePath: path.join(__dirname, 'static/email/templates'),
|
|
43
|
+
* transport: {
|
|
44
|
+
* type: 'smtp',
|
|
45
|
+
* host: 'smtp.example.com',
|
|
46
|
+
* port: 587,
|
|
47
|
+
* auth: {
|
|
48
|
+
* user: 'username',
|
|
49
|
+
* pass: 'password',
|
|
50
|
+
* }
|
|
51
|
+
* },
|
|
52
|
+
* }),
|
|
53
|
+
* ],
|
|
54
|
+
* };
|
|
55
|
+
* ```
|
|
56
|
+
*
|
|
57
|
+
* ## Email templates
|
|
58
|
+
*
|
|
59
|
+
* In the example above, the plugin has been configured to look in `<app-root>/static/email/templates`
|
|
60
|
+
* for the email template files. If you used `\@deenruv/create` to create your application, the templates will have
|
|
61
|
+
* been copied to that location during setup.
|
|
62
|
+
*
|
|
63
|
+
* If you are installing the EmailPlugin separately, then you'll need to copy the templates manually from
|
|
64
|
+
* `node_modules/\@deenruv/email-plugin/templates` to a location of your choice, and then point the `templatePath` config
|
|
65
|
+
* property at that directory.
|
|
66
|
+
*
|
|
67
|
+
* * ### Dynamic Email Templates
|
|
68
|
+
* Instead of passing a static value to `templatePath`, use `templateLoader` to define a template path.
|
|
69
|
+
* ```ts
|
|
70
|
+
* EmailPlugin.init({
|
|
71
|
+
* ...,
|
|
72
|
+
* templateLoader: new FileBasedTemplateLoader(my/order-confirmation/templates)
|
|
73
|
+
* })
|
|
74
|
+
* ```
|
|
75
|
+
* ## Customizing templates
|
|
76
|
+
*
|
|
77
|
+
* Emails are generated from templates which use [MJML](https://mjml.io/) syntax. MJML is an open-source HTML-like markup
|
|
78
|
+
* language which makes the task of creating responsive email markup simple. By default, the templates are installed to
|
|
79
|
+
* `<project root>/deenruv/email/templates` and can be freely edited.
|
|
80
|
+
*
|
|
81
|
+
* Dynamic data such as the recipient's name or order items are specified using [Handlebars syntax](https://handlebarsjs.com/):
|
|
82
|
+
*
|
|
83
|
+
* ```html
|
|
84
|
+
* <p>Dear {{ order.customer.firstName }} {{ order.customer.lastName }},</p>
|
|
85
|
+
*
|
|
86
|
+
* <p>Thank you for your order!</p>
|
|
87
|
+
*
|
|
88
|
+
* <mj-table cellpadding="6px">
|
|
89
|
+
* {{#each order.lines }}
|
|
90
|
+
* <tr class="order-row">
|
|
91
|
+
* <td>{{ quantity }} x {{ productVariant.name }}</td>
|
|
92
|
+
* <td>{{ productVariant.quantity }}</td>
|
|
93
|
+
* <td>{{ formatMoney totalPrice }}</td>
|
|
94
|
+
* </tr>
|
|
95
|
+
* {{/each}}
|
|
96
|
+
* </mj-table>
|
|
97
|
+
* ```
|
|
98
|
+
*
|
|
99
|
+
* ### Setting global variables using `globalTemplateVars`
|
|
100
|
+
*
|
|
101
|
+
* `globalTemplateVars` is an object that can be passed to the configuration of the Email Plugin with static object variables.
|
|
102
|
+
* You can also pass an async function that will be called with the `RequestContext` and the `Injector` so you can access services
|
|
103
|
+
* and e.g. load channel specific theme configurations.
|
|
104
|
+
*
|
|
105
|
+
* @example
|
|
106
|
+
* ```ts
|
|
107
|
+
* EmailPlugin.init({
|
|
108
|
+
* globalTemplateVars: {
|
|
109
|
+
* primaryColor: '#FF0000',
|
|
110
|
+
* fromAddress: 'no-reply@ourstore.com'
|
|
111
|
+
* }
|
|
112
|
+
* })
|
|
113
|
+
* ```
|
|
114
|
+
* or
|
|
115
|
+
* ```ts
|
|
116
|
+
* EmailPlugin.init({
|
|
117
|
+
* globalTemplateVars: async (ctx, injector) => {
|
|
118
|
+
* const myAsyncService = injector.get(MyAsyncService);
|
|
119
|
+
* const asyncValue = await myAsyncService.get(ctx);
|
|
120
|
+
* const channel = ctx.channel;
|
|
121
|
+
* const { primaryColor } = channel.customFields.theme;
|
|
122
|
+
* const theme = {
|
|
123
|
+
* primaryColor,
|
|
124
|
+
* asyncValue,
|
|
125
|
+
* };
|
|
126
|
+
* return theme;
|
|
127
|
+
* }
|
|
128
|
+
* })
|
|
129
|
+
* ```
|
|
130
|
+
*
|
|
131
|
+
* ### Handlebars helpers
|
|
132
|
+
*
|
|
133
|
+
* The following helper functions are available for use in email templates:
|
|
134
|
+
*
|
|
135
|
+
* * `formatMoney`: Formats an amount of money (which are always stored as integers in Deenruv) as a decimal, e.g. `123` => `1.23`
|
|
136
|
+
* * `formatDate`: Formats a Date value with the [dateformat](https://www.npmjs.com/package/dateformat) package.
|
|
137
|
+
*
|
|
138
|
+
* ## Extending the default email handler
|
|
139
|
+
*
|
|
140
|
+
* The `defaultEmailHandlers` array defines the default handler such as for handling new account registration, order confirmation, password reset
|
|
141
|
+
* etc. These defaults can be extended by adding custom templates for languages other than the default, or even completely new types of emails
|
|
142
|
+
* which respond to any of the available [DeenruvEvents](/reference/typescript-api/events/).
|
|
143
|
+
*
|
|
144
|
+
* A good way to learn how to create your own email handler is to take a look at the
|
|
145
|
+
* [source code of the default handler](https://github.com/deenruv-ecommerce/deenruv/blob/master/packages/email-plugin/src/handler/default-email-handlers.ts).
|
|
146
|
+
* New handler are defined in exactly the same way.
|
|
147
|
+
*
|
|
148
|
+
* It is also possible to modify the default handler:
|
|
149
|
+
*
|
|
150
|
+
* ```ts
|
|
151
|
+
* // Rather than importing `defaultEmailHandlers`, you can
|
|
152
|
+
* // import the handler individually
|
|
153
|
+
* import {
|
|
154
|
+
* orderConfirmationHandler,
|
|
155
|
+
* emailVerificationHandler,
|
|
156
|
+
* passwordResetHandler,
|
|
157
|
+
* emailAddressChangeHandler,
|
|
158
|
+
* } from '\@deenruv/email-plugin';
|
|
159
|
+
* import { CustomerService } from '\@deenruv/core';
|
|
160
|
+
*
|
|
161
|
+
* // This allows you to then customize each handler to your needs.
|
|
162
|
+
* // For example, let's set a new subject line to the order confirmation:
|
|
163
|
+
* const myOrderConfirmationHandler = orderConfirmationHandler
|
|
164
|
+
* .setSubject(`We received your order!`);
|
|
165
|
+
*
|
|
166
|
+
* // Another example: loading additional data and setting new
|
|
167
|
+
* // template variables.
|
|
168
|
+
* const myPasswordResetHandler = passwordResetHandler
|
|
169
|
+
* .loadData(async ({ event, injector }) => {
|
|
170
|
+
* const customerService = injector.get(CustomerService);
|
|
171
|
+
* const customer = await customerService.findOneByUserId(event.ctx, event.user.id);
|
|
172
|
+
* return { customer };
|
|
173
|
+
* })
|
|
174
|
+
* .setTemplateVars(event => ({
|
|
175
|
+
* passwordResetToken: event.user.getNativeAuthenticationMethod().passwordResetToken,
|
|
176
|
+
* customer: event.data.customer,
|
|
177
|
+
* }));
|
|
178
|
+
*
|
|
179
|
+
* // Then you pass the handler to the EmailPlugin init method
|
|
180
|
+
* // individually
|
|
181
|
+
* EmailPlugin.init({
|
|
182
|
+
* handler: [
|
|
183
|
+
* myOrderConfirmationHandler,
|
|
184
|
+
* myPasswordResetHandler,
|
|
185
|
+
* emailVerificationHandler,
|
|
186
|
+
* emailAddressChangeHandler,
|
|
187
|
+
* ],
|
|
188
|
+
* // ...
|
|
189
|
+
* }),
|
|
190
|
+
* ```
|
|
191
|
+
*
|
|
192
|
+
* For all available methods of extending a handler, see the {@link EmailEventHandler} documentation.
|
|
193
|
+
*
|
|
194
|
+
* ## Dynamic SMTP settings
|
|
195
|
+
*
|
|
196
|
+
* Instead of defining static transport settings, you can also provide a function that dynamically resolves
|
|
197
|
+
* channel aware transport settings.
|
|
198
|
+
*
|
|
199
|
+
* @example
|
|
200
|
+
* ```ts
|
|
201
|
+
* import { defaultEmailHandlers, EmailPlugin } from '\@deenruv/email-plugin';
|
|
202
|
+
* import { MyTransportService } from './transport.services.ts';
|
|
203
|
+
* const config: DeenruvConfig = {
|
|
204
|
+
* plugins: [
|
|
205
|
+
* EmailPlugin.init({
|
|
206
|
+
* handler: defaultEmailHandlers,
|
|
207
|
+
* templatePath: path.join(__dirname, 'static/email/templates'),
|
|
208
|
+
* transport: (injector, ctx) => {
|
|
209
|
+
* if (ctx) {
|
|
210
|
+
* return injector.get(MyTransportService).getSettings(ctx);
|
|
211
|
+
* } else {
|
|
212
|
+
* return {
|
|
213
|
+
* type: 'smtp',
|
|
214
|
+
* host: 'smtp.example.com',
|
|
215
|
+
* // ... etc.
|
|
216
|
+
* }
|
|
217
|
+
* }
|
|
218
|
+
* }
|
|
219
|
+
* }),
|
|
220
|
+
* ],
|
|
221
|
+
* };
|
|
222
|
+
* ```
|
|
223
|
+
*
|
|
224
|
+
* ## Dev mode
|
|
225
|
+
*
|
|
226
|
+
* For development, the `transport` option can be replaced by `devMode: true`. Doing so configures Deenruv to use the
|
|
227
|
+
* file transport (See {@link FileTransportOptions}) and outputs emails as rendered HTML files in the directory specified by the
|
|
228
|
+
* `outputPath` property.
|
|
229
|
+
*
|
|
230
|
+
* ```ts
|
|
231
|
+
* EmailPlugin.init({
|
|
232
|
+
* devMode: true,
|
|
233
|
+
* route: 'mailbox',
|
|
234
|
+
* handler: defaultEmailHandlers,
|
|
235
|
+
* templatePath: path.join(__dirname, 'deenruv/email/templates'),
|
|
236
|
+
* outputPath: path.join(__dirname, 'test-emails'),
|
|
237
|
+
* })
|
|
238
|
+
* ```
|
|
239
|
+
*
|
|
240
|
+
* ### Dev mailbox
|
|
241
|
+
*
|
|
242
|
+
* In dev mode, a webmail-like interface available at the `/mailbox` path, e.g.
|
|
243
|
+
* http://localhost:3000/mailbox. This is a simple way to view the output of all emails generated by the EmailPlugin while in dev mode.
|
|
244
|
+
*
|
|
245
|
+
* ## Troubleshooting SMTP Connections
|
|
246
|
+
*
|
|
247
|
+
* If you are having trouble sending email over and SMTP connection, set the `logging` and `debug` options to `true`. This will
|
|
248
|
+
* send detailed information from the SMTP transporter to the configured logger (defaults to console). For maximum detail combine
|
|
249
|
+
* this with a detail log level in the configured DeenruvLogger:
|
|
250
|
+
*
|
|
251
|
+
* ```ts
|
|
252
|
+
* const config: DeenruvConfig = {
|
|
253
|
+
* logger: new DefaultLogger({ level: LogLevel.Debug })
|
|
254
|
+
* // ...
|
|
255
|
+
* plugins: [
|
|
256
|
+
* EmailPlugin.init({
|
|
257
|
+
* // ...
|
|
258
|
+
* transport: {
|
|
259
|
+
* type: 'smtp',
|
|
260
|
+
* host: 'smtp.example.com',
|
|
261
|
+
* port: 587,
|
|
262
|
+
* auth: {
|
|
263
|
+
* user: 'username',
|
|
264
|
+
* pass: 'password',
|
|
265
|
+
* },
|
|
266
|
+
* logging: true,
|
|
267
|
+
* debug: true,
|
|
268
|
+
* },
|
|
269
|
+
* }),
|
|
270
|
+
* ],
|
|
271
|
+
* };
|
|
272
|
+
* ```
|
|
273
|
+
*
|
|
274
|
+
* @docsCategory core plugins/EmailPlugin
|
|
275
|
+
*/
|
|
276
|
+
export declare class EmailPlugin implements OnApplicationBootstrap, OnApplicationShutdown, NestModule {
|
|
277
|
+
private eventBus;
|
|
278
|
+
private moduleRef;
|
|
279
|
+
private emailProcessor;
|
|
280
|
+
private jobQueueService;
|
|
281
|
+
private processContext;
|
|
282
|
+
private options;
|
|
283
|
+
private static options;
|
|
284
|
+
private devMailbox;
|
|
285
|
+
private jobQueue;
|
|
286
|
+
private testingProcessor;
|
|
287
|
+
/** @internal */
|
|
288
|
+
constructor(eventBus: EventBus, moduleRef: ModuleRef, emailProcessor: EmailProcessor, jobQueueService: JobQueueService, processContext: ProcessContext, options: InitializedEmailPluginOptions);
|
|
289
|
+
/**
|
|
290
|
+
* Set the plugin options.
|
|
291
|
+
*/
|
|
292
|
+
static init(options: EmailPluginOptions | EmailPluginDevModeOptions): Type<EmailPlugin>;
|
|
293
|
+
/** @internal */
|
|
294
|
+
onApplicationBootstrap(): Promise<void>;
|
|
295
|
+
onApplicationShutdown(): Promise<void>;
|
|
296
|
+
configure(consumer: MiddlewareConsumer): void;
|
|
297
|
+
private initInjectableStrategies;
|
|
298
|
+
private destroyInjectableStrategies;
|
|
299
|
+
private setupEventSubscribers;
|
|
300
|
+
private handleEvent;
|
|
301
|
+
}
|