Skip to content

Commit

Permalink
fix sending emails
Browse files Browse the repository at this point in the history
  • Loading branch information
arturmolenda committed Mar 11, 2021
1 parent c446e5f commit 9d15ff4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 25 deletions.
18 changes: 3 additions & 15 deletions backend/controllers/userController.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import EmailSecret from '../models/emailSecret.js';
import generateToken from '../utils/generateToken.js';
import asyncHandler from 'express-async-handler';
import mongoose from 'mongoose';
import transporter from '../utils/sendEmail.js';
import sendEmail from '../utils/sendEmail.js';

// @desc Auth user & get token
// @route POST /api/users/login
Expand Down Expand Up @@ -77,13 +77,7 @@ const registerUser = asyncHandler(async (req, res) => {
text: 'Welcome to the Project Manager!',
html: `<h1>Thank you for registering!</h1></br><p>To finish registration just click this link</p><span><a href='${process.env.URL}/confirm/${emailSecretCode}'> ${process.env.URL}/confirm/${emailSecretCode}</a></span>`,
};
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
await sendEmail(mailOptions);
res.status(200).json({
message:
'Confirmation email has been send to you. You can now sign in with your newly created account',
Expand Down Expand Up @@ -149,13 +143,7 @@ const resendEmail = asyncHandler(async (req, res) => {
text: 'Welcome to the Project Manager!',
html: `<h1>Thank you for registering!</h1></br><p>To finish registration just click this link</p><span><a href='${process.env.URL}/confirm/${newSecretCode}'> ${process.env.URL}/confirm/${newSecretCode}</a></span>`,
};
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
await sendEmail(mailOptions);
} catch (err) {
console.log(err);
throw new Error('Sending email failed');
Expand Down
34 changes: 24 additions & 10 deletions backend/utils/sendEmail.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,27 @@ import dotenv from 'dotenv';
import smtpTransport from 'nodemailer-smtp-transport';
dotenv.config();

const transporter = nodemailer.createTransport(
smtpTransport({
service: 'gmail',
auth: {
user: process.env.EMAIL,
pass: process.env.PASSWORD,
},
})
);
export default transporter;
const sendEmail = async (mailOptions) => {
const transporter = nodemailer.createTransport(
smtpTransport({
host: process.env.HOST,
service: process.env.EMAIL_PROVIDER,
secure: false,
port: process.env.SMTP_PORT,
auth: {
user: process.env.EMAIL,
pass: process.env.PASSWORD,
},
tls: { rejectUnauthorized: false },
})
);
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
};

export default sendEmail;

0 comments on commit 9d15ff4

Please sign in to comment.