File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 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+ } ) ;
Original file line number Diff line number Diff 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+ }
Original file line number Diff line number Diff line change 1- import { getEnv } from './env' ;
2- import { requiredEnvVar } from './helpers' ;
1+ import { DEV , getEnv } from './env' ;
2+ import { requiredEnvVar , toBoolean } from './helpers' ;
33
44getEnv ( ) ;
55
66export 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 } ) ;
714export const MAILER_CONFIG_FROM_EMAIL =
815 process . env . MAILER_CONFIG_FROM_EMAIL ?? 'no-reply@graasp.org' ;
Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ import type { FastifyBaseLogger } from 'fastify';
55
66import 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' ;
99import { REDIS_CONNECTION } from '../config/redis' ;
1010import { BaseLogger } from '../logger' ;
1111import { 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 ) ;
Original file line number Diff line number Diff line change @@ -12,6 +12,7 @@ export interface Mail {
1212
1313export 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 /**
You can’t perform that action at this time.
0 commit comments