Allow custom email subjects for invite and pw reset mail (#5446)

* Make email template overrides work

The ternary needs to be this way for custom email templates to be picked up.

* Allow custom subject lines for emails

Allow subject line to be passed into `inviteUser()` and `requestPasswordReset()`

* Fix typo

* Revert so only one change for PR

* fix typo

* Fix ts tests
This commit is contained in:
Sebastian Kinzlinger
2021-05-04 16:32:29 +03:00
committed by GitHub
parent faa3ca7a06
commit a4552e3752

View File

@@ -217,7 +217,7 @@ export class UsersService extends ItemsService {
return keys;
}
async inviteUser(email: string | string[], role: string, url: string | null): Promise<void> {
async inviteUser(email: string | string[], role: string, url: string | null, subject?: string | null): Promise<void> {
const emails = toArray(email);
const urlWhitelist = toArray(env.USER_INVITE_URL_ALLOW_LIST);
@@ -246,10 +246,11 @@ export class UsersService extends ItemsService {
const token = jwt.sign(payload, env.SECRET as string, { expiresIn: '7d' });
const inviteURL = url ?? env.PUBLIC_URL + '/admin/accept-invite';
const acceptURL = inviteURL + '?token=' + token;
const subjectLine = subject ? subject : "You've been invited";
await mailService.send({
to: email,
subject: "You've been invited",
subject: subjectLine,
template: {
name: 'user-invitation',
data: {
@@ -286,7 +287,7 @@ export class UsersService extends ItemsService {
}
}
async requestPasswordReset(email: string, url: string | null): Promise<void> {
async requestPasswordReset(email: string, url: string | null, subject?: string | null): Promise<void> {
const user = await this.knex.select('id').from('directus_users').where({ email }).first();
if (!user) throw new ForbiddenException();
@@ -306,10 +307,11 @@ export class UsersService extends ItemsService {
}
const acceptURL = url ? `${url}?token=${token}` : `${env.PUBLIC_URL}/admin/reset-password?token=${token}`;
const subjectLine = subject ? subject : 'Password Reset Request';
await mailService.send({
to: email,
subject: 'Password Reset Request',
subject: subjectLine,
template: {
name: 'password-reset',
data: {