Skip to content

Commit 03fec6c

Browse files
authored
fix: use secure: true instead of smtps:// in mailer (#1952)
* fix: use secure for ssl instead of smtps * fix: add env var to disable ssl in self hosting compose * chore: add doc for the function * fix: make changes for PR
1 parent 63ce44f commit 03fec6c

6 files changed

Lines changed: 57 additions & 5 deletions

File tree

docker/compose.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ x-core_environment: &core_environment
2929

3030
# the Mailer config is set by the "mailer" service below
3131
MAILER_CONNECTION: smtp://docker:docker@mailer:1025
32+
# disable ssl for emails, in production the default is to enable SSL.
33+
MAILER_USE_SSL: false
3234
# Redis
3335
REDIS_CONNECTION: redis://redis:6379
3436

src/config/helpers.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { describe, expect, it } from 'vitest';
2+
3+
import { toBoolean } from './helpers';
4+
5+
describe('toBoolean', () => {
6+
it('undefined value', () => {
7+
expect(toBoolean(undefined, { default: true })).toEqual(true);
8+
expect(toBoolean(undefined, { default: false })).toEqual(false);
9+
expect(toBoolean(undefined)).toEqual(false);
10+
});
11+
it('string boolean value', () => {
12+
expect(toBoolean('true')).toEqual(true);
13+
expect(toBoolean('false')).toEqual(false);
14+
expect(toBoolean('toto')).toEqual(false);
15+
});
16+
it('number value', () => {
17+
expect(toBoolean('1')).toEqual(true);
18+
expect(toBoolean('0')).toEqual(false);
19+
expect(toBoolean('4')).toEqual(false);
20+
});
21+
});

src/config/helpers.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,23 @@ export function requiredEnvVar(name: string) {
1818
}
1919
return varValue;
2020
}
21+
22+
/**
23+
* Convert a string value to a boolean.
24+
* Accepts `true` and `1` as true values.
25+
*
26+
* A default can be provided in the options. If not provided the value will be `false`.
27+
* @param value the value to convert to a boolean, can be a string or undefined
28+
* @param options an object containing a `default` key to be used when the value is not defined,
29+
* default to `false`
30+
* @returns the boolean value of the input
31+
*/
32+
export function toBoolean(value: string | undefined, options?: { default: boolean }) {
33+
if (value == undefined) {
34+
return options?.default ?? false;
35+
}
36+
if (value === 'true' || value === '1') {
37+
return true;
38+
}
39+
return false;
40+
}

src/config/mailer.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1-
import { getEnv } from './env';
2-
import { requiredEnvVar } from './helpers';
1+
import { DEV, getEnv } from './env';
2+
import { requiredEnvVar, toBoolean } from './helpers';
33

44
getEnv();
55

66
export const MAILER_CONNECTION = requiredEnvVar('MAILER_CONNECTION');
7+
/**
8+
* In Development we disable Mailer SSL
9+
* In production we enable it by default, it can be disbaled by setting the MAILER_USE_SSL=false
10+
*/
11+
export const MAILER_USE_SSL = DEV
12+
? false
13+
: toBoolean(process.env.MAILER_USE_SSL, { default: true });
714
export const MAILER_CONFIG_FROM_EMAIL =
815
process.env.MAILER_CONFIG_FROM_EMAIL ?? 'no-reply@graasp.org';

src/di/container.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type { FastifyBaseLogger } from 'fastify';
55

66
import Etherpad from '@graasp/etherpad-api';
77

8-
import { MAILER_CONFIG_FROM_EMAIL, MAILER_CONNECTION } from '../config/mailer';
8+
import { MAILER_CONFIG_FROM_EMAIL, MAILER_CONNECTION, MAILER_USE_SSL } from '../config/mailer';
99
import { REDIS_CONNECTION } from '../config/redis';
1010
import { BaseLogger } from '../logger';
1111
import { MailerService } from '../plugins/mailer/mailer.service';
@@ -103,6 +103,7 @@ export const registerDependencies = (log: FastifyBaseLogger) => {
103103
MailerService,
104104
new MailerService({
105105
connection: MAILER_CONNECTION,
106+
useSSL: MAILER_USE_SSL,
106107
fromEmail: MAILER_CONFIG_FROM_EMAIL,
107108
}),
108109
);

src/plugins/mailer/mailer.service.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export interface Mail {
1212

1313
export interface MailerOptions {
1414
connection: string;
15+
useSSL: boolean;
1516
fromEmail: string;
1617
}
1718

@@ -20,9 +21,9 @@ export class MailerService {
2021
private readonly fromEmail: string;
2122
private readonly transporter: Transporter;
2223

23-
constructor({ connection, fromEmail }: MailerOptions) {
24+
constructor({ connection, useSSL, fromEmail }: MailerOptions) {
2425
this.fromEmail = fromEmail;
25-
this.transporter = createTransport(connection);
26+
this.transporter = createTransport(connection, { secure: useSSL });
2627
}
2728

2829
/**

0 commit comments

Comments
 (0)