Skip to content

Commit

Permalink
feat: make email config optional
Browse files Browse the repository at this point in the history
  • Loading branch information
Geoffroy Empain committed Dec 5, 2020
1 parent 58153d9 commit a5194f2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ MELI_CADDY_MELI_API_HOST=http://host.docker.internal:3001
MELI_CADDY_MELI_UI_HOST=http://host.docker.internal:3000

# mail
MELI_MAIL_TEMPLATE_DIR=./build/emails/templates
MELI_MAIL_FROM=alert@domain.com
MELI_MAIL_HOST=localhost
MELI_MAIL_PORT=1025
Expand Down
41 changes: 31 additions & 10 deletions src/emails/emails.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,33 @@
import nodemailer, { Transporter, TransportOptions } from 'nodemailer';
import nodemailer, { SentMessageInfo, TransportOptions } from 'nodemailer';
import { env } from '../env';
import Mail from 'nodemailer/lib/mailer';
import { Logger } from '../commons/logger/logger';
import chalk from 'chalk';

// https://nodemailer.com/smtp/#general-options
export const emails: Transporter = nodemailer.createTransport(<TransportOptions>{
host: env.MELI_MAIL_HOST,
port: env.MELI_MAIL_PORT,
auth: env.MELI_MAIL_USERNAME && env.MELI_MAIL_PASSWORD ? {
user: env.MELI_MAIL_USERNAME,
pass: env.MELI_MAIL_PASSWORD,
} : undefined,
});
const logger = new Logger('meli.server:emails');

let transporter: {
sendMail(mailOptions: Mail.Options): Promise<SentMessageInfo>;
};

if (env.MELI_MAIL_HOST && env.MELI_MAIL_PORT) {
logger.debug('Emails are configured');
// https://nodemailer.com/smtp/#general-options
transporter = nodemailer.createTransport(<TransportOptions>{
host: env.MELI_MAIL_HOST,
port: env.MELI_MAIL_PORT,
auth: env.MELI_MAIL_USERNAME && env.MELI_MAIL_PASSWORD ? {
user: env.MELI_MAIL_USERNAME,
pass: env.MELI_MAIL_PASSWORD,
} : undefined,
});
} else {
logger.warn(`Emails are ${chalk.red('disabled')}, will print to console`);
transporter = {
async sendMail(mailOptions: Mail.Options): Promise<SentMessageInfo> {
logger.info('email', JSON.stringify(mailOptions, null, 2));
},
};
}

export const emails = transporter;

0 comments on commit a5194f2

Please sign in to comment.