From f1600389843e5c09e4f766c3a9f603ff0aecc049 Mon Sep 17 00:00:00 2001 From: joe128 Date: Mon, 19 Feb 2024 18:58:02 +0100 Subject: [PATCH] add subject-prefix, fixes #7 --- README.md | 3 +++ src/@types/target.ts | 1 + src/models/post.ts | 8 ++++++++ src/models/target.ts | 4 ++++ src/router.ts | 5 +++-- 5 files changed, 19 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 434b626..bd7ed8b 100644 --- a/README.md +++ b/README.md @@ -83,6 +83,7 @@ They are JSON files placed in the `/targets` directory. - `origin` *optional* | A HTTP origin that is used for CORS and to restrict access. Default is * if not set. - `recipients` *required* | An array of email addresses which should receive the email. - `from` *optional* | The "from" field of an email. This is used as fallback if no "from" is provided in the request. +- `subjectPrefix` *optional* | A target-wide prefix for the email subject. - `key` *optional* | A string used as API key if you want to restrict access to this target. - `redirect` *optional*: - `success` *optional*: A valid URL to redirect the user if the mail was sent successful. @@ -103,6 +104,7 @@ Whether as formular data or json, the fields are the same. - `from` *optional* | The email address of the sender. If this filed is not set, the "from" field of your target will be used. - `firstName` *optional* | A classic first name filed which will be attached to the "from" field of the email. - `lastName` *optional* | A classic last name filed which will be attached to the "from" field of the email. +- `subjectPrefix` *optional* | A Prefix for the email subject. - `subject` *required* | The email subject. - `body` *required* | The email body (supports HTML). @@ -131,6 +133,7 @@ If you use an API request, you have to fill it manually. +
diff --git a/src/@types/target.ts b/src/@types/target.ts index 33a2c23..d5cab9d 100644 --- a/src/@types/target.ts +++ b/src/@types/target.ts @@ -3,6 +3,7 @@ export interface Target { origin: string; recipients: string[]; from?: string; + subjectPrefix?: string; redirect?: Redirects; key?: string; rateLimit?: TargetRateLimit; diff --git a/src/models/post.ts b/src/models/post.ts index 21a7923..c15ff81 100644 --- a/src/models/post.ts +++ b/src/models/post.ts @@ -20,6 +20,14 @@ export const postBody = { maximum: 100 } }, + subjectPrefix: { + type: "string", + presence: false, + length: { + minimum: 1, + maximum: 255 + } + }, subject: { type: "string", presence: { allowEmpty: false }, diff --git a/src/models/target.ts b/src/models/target.ts index d3a74e4..2611eea 100644 --- a/src/models/target.ts +++ b/src/models/target.ts @@ -19,6 +19,10 @@ export const targetModel = { type: "string", presence: false }, + subjectPrefix: { + type: "string", + presence: false + }, redirect: { type: "object", presence: false diff --git a/src/router.ts b/src/router.ts index 6ab00cf..b4ece03 100644 --- a/src/router.ts +++ b/src/router.ts @@ -90,12 +90,13 @@ router.post("/:target", async (req: Request, res: Response) => { const fieldFrom = fields["from"] instanceof Array ? fields["from"][0] : fields["from"] const fieldFirstName = fields["firstName"] instanceof Array ? fields["firstName"][0] : fields["firstName"] const fieldLastName = fields["lastName"] instanceof Array ? fields["lastName"][0] : fields["lastName"] - const fieldSubject = fields["subject"] instanceof Array ? fields["subject"][0] : fields["subject"] + const fieldSubjectPrefix = fields["subjectPrefix"] instanceof Array ? fields["subjectPrefix"][0] : fields["subjectPrefix"] ?? "" + const subject = (target.subjectPrefix ?? "") + fieldSubjectPrefix + (fields["subject"] instanceof Array ? fields["subject"][0] : fields["subject"]) const fieldBody = fields["body"] instanceof Array ? fields["body"][0] : fields["body"] // send email let from = EmailService.formatFromField(fieldFrom ?? target.from, fieldFirstName, fieldLastName); - let sent = await EmailService.sendMail(req.params.target, from, fieldSubject, fieldBody, files); + let sent = await EmailService.sendMail(req.params.target, from, subject, fieldBody, files); if(sent instanceof Error || !sent) { if(target.redirect?.error) return res.redirect(target.redirect.error);