1 | ;
|
2 |
|
3 | /**
|
4 | * An email sender sends an email
|
5 | */
|
6 | class EmailSender
|
7 | {
|
8 | constructor()
|
9 | {
|
10 |
|
11 | /**
|
12 | * Filters to validate address with before sending emails to.
|
13 | * @type {Array<EmailFilter>}
|
14 | */
|
15 | this.filters = [];
|
16 | }
|
17 |
|
18 | /**
|
19 | * Send an email. This performs various checks and calls sendImplementation
|
20 | *
|
21 | * @param {string} to where to send email
|
22 | * @param {string} from reply address
|
23 | * @param {string} subject subject of email
|
24 | * @param {string} body content of email
|
25 | */
|
26 | async send(to, from, subject, body)
|
27 | {
|
28 | for (let filter of this.filters)
|
29 | {
|
30 | if (!await filter.doesAllow(to))
|
31 | {
|
32 | throw new Error(`${filter.name} does not allow specified email address: '${to}'.`);
|
33 | }
|
34 | }
|
35 | await this.sendImplementation(to, from, subject, body);
|
36 | }
|
37 |
|
38 | /**
|
39 | * @abstract
|
40 | *
|
41 | * @param {string} to where to send email
|
42 | * @param {string} from reply address
|
43 | * @param {string} subject subject of email
|
44 | * @param {string} body content of email
|
45 | */
|
46 | async sendImplementation(to_, from_, subject_, body_)
|
47 | {
|
48 | throw new Error('TODO: not implemented');
|
49 | }
|
50 | }
|
51 |
|
52 | module.exports = EmailSender;
|