Skip to content

Commit

Permalink
Verify if connection to email server was successful
Browse files Browse the repository at this point in the history
  • Loading branch information
hagopj13 committed Nov 22, 2019
1 parent 3856580 commit f38d86a
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 19 deletions.
13 changes: 8 additions & 5 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ MONGODB_URL=mongodb://127.0.0.1:27017/node-boilerplate
JWT_SECRET=thisisasamplesecret
JWT_ACCESS_EXPIRATION_MINUTES=30
JWT_REFRESH_EXPIRATION_DAYS=30
EMAIL_HOST=
EMAIL_PORT=
EMAIL_USER=
EMAIL_PASS=
EMAIL_FROM=

# SMTP configuration options for the email service
# For testing, you can use a fake SMTP service like Ethereal: https://ethereal.email/create
SMTP_HOST=email-server
SMTP_PORT=587
SMTP_USERNAME=email-server-username
SMTP_PASSWORD=email-server-password
EMAIL_FROM=support@yourapp.com
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Environment varibales:
```bash
cp .env.example .env

# open the .env file and fill out any empty environment vars (optional)
# open .env and modify the environment variables (if needed)
```

### Commands
Expand Down
18 changes: 9 additions & 9 deletions src/config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ const envVarsSchema = Joi.object()
JWT_REFRESH_EXPIRATION_DAYS: Joi.number()
.default(30)
.description('days after which refresh tokens expire'),
EMAIL_HOST: Joi.string().description('email service host which is passed to transporter configuration'),
EMAIL_PORT: Joi.number().description('email service port which is passed to transporter configuration'),
EMAIL_USER: Joi.string().description('email service username which is passed to transporter configuration'),
EMAIL_PASS: Joi.string().description('email service password which is passed to transporter configuration'),
SMTP_HOST: Joi.string().description('server that will send the emails'),
SMTP_PORT: Joi.number().description('port to connect to the email server'),
SMTP_USERNAME: Joi.string().description('username for email server'),
SMTP_PASSWORD: Joi.string().description('password for email server'),
EMAIL_FROM: Joi.string().description('the from field in the emails sent by the app'),
})
.unknown();
Expand All @@ -50,12 +50,12 @@ module.exports = {
resetPasswordExpirationMinutes: 10,
},
email: {
transport: {
host: envVars.EMAIL_HOST,
port: envVars.EMAIL_PORT,
smtp: {
host: envVars.SMTP_HOST,
port: envVars.SMTP_PORT,
auth: {
user: envVars.EMAIL_USER,
pass: envVars.EMAIL_PASS,
user: envVars.SMTP_USERNAME,
pass: envVars.SMTP_PASSWORD,
},
},
from: envVars.EMAIL_FROM,
Expand Down
14 changes: 11 additions & 3 deletions src/services/email.service.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
const nodemailer = require('nodemailer');
const config = require('../config/config');
const logger = require('../config/logger');

const transporter = nodemailer.createTransport(config.email.transport);
const transport = nodemailer.createTransport(config.email.smtp);
/* istanbul ignore next */
if (config.env !== 'test') {
transport
.verify()
.then(() => logger.info('Connected to email server'))
.catch(() => logger.warn('Unable to connect to email server. Make sure you have configured the SMTP options in .env'));
}

const sendEmail = async (to, subject, text) => {
const msg = { from: config.email.from, to, subject, text };
await transporter.sendMail(msg);
await transport.sendMail(msg);
};

const sendResetPasswordEmail = async (to, token) => {
Expand All @@ -19,7 +27,7 @@ const sendResetPasswordEmail = async (to, token) => {
};

module.exports = {
transporter,
transport,
sendEmail,
sendResetPasswordEmail,
};
2 changes: 1 addition & 1 deletion tests/integration/auth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ describe('Auth routes', () => {

describe('POST /v1/auth/forgot-password', () => {
beforeEach(() => {
jest.spyOn(emailService.transporter, 'sendMail').mockResolvedValue();
jest.spyOn(emailService.transport, 'sendMail').mockResolvedValue();
});

test('should return 204 and send reset password email to the user', async () => {
Expand Down

0 comments on commit f38d86a

Please sign in to comment.