add subject-prefix, fixes #7

This commit is contained in:
joe128 2024-02-19 18:58:02 +01:00
parent ecfe351e17
commit f160038984
5 changed files with 19 additions and 2 deletions

View file

@ -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.
<input type="email" name="from" placeholder="Sender's email address"/>
<input type="text" name="firstName" placeholder="First name" />
<input type="text" name="lastName" placeholder="Last name" />
<input type="hidden" name="subjectPrefix" value="[App-Question] " />
<input type="text" name="subject" placeholder="Subject" />
<div class="g-recaptcha" data-sitekey="your_site_key"></div>
<textarea name="body" placeholder="Your message"></textarea>

View file

@ -3,6 +3,7 @@ export interface Target {
origin: string;
recipients: string[];
from?: string;
subjectPrefix?: string;
redirect?: Redirects;
key?: string;
rateLimit?: TargetRateLimit;

View file

@ -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 },

View file

@ -19,6 +19,10 @@ export const targetModel = {
type: "string",
presence: false
},
subjectPrefix: {
type: "string",
presence: false
},
redirect: {
type: "object",
presence: false

View file

@ -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);