Skip to content

Commit

Permalink
Add mail service
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaniz-ifte committed Feb 27, 2022
1 parent 8afe107 commit a445fe7
Show file tree
Hide file tree
Showing 5 changed files with 1,594 additions and 1,607 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ PORT=
CLOUDINARY_CLOUD_NAME=
CLOUDINARY_API_KEY=
CLOUDINARY_API_SECRET=
HOST_EMAIL=
HOST_EMAIL_PASSWORD=
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"multer-storage-cloudinary": "^4.0.0",
"mysql2": "^2.3.3",
"node-cache": "^5.1.2",
"nodemailer": "^6.7.2",
"passport": "^0.5.2",
"passport-jwt": "^4.0.0",
"sequelize": "^6.12.0-beta.3",
Expand Down
39 changes: 39 additions & 0 deletions src/modules/core/email/email.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const nodemailer = require('nodemailer');

const transporter = nodemailer.createTransport({
port: 465, // true for 465, false for other ports
host: "smtp.gmail.com",
auth: {
user: process.env.HOST_EMAIL,
pass: process.env.HOST_EMAIL_PASSWORD,
},
secure: true,
});

async function send (options) {
try {
const mailData = {
from: process.env.HOST_EMAIL,
to: options.email,
subject: options.subject,
//text,
html: options.template,
};

transporter.sendMail(mailData, (err, info) => {
if (err) {
console.log(err)
//res.status(200).send({ message: "Mail send", message_id: info.messageID });
console.log({ message: "Mail send", message_id: info.messageID });
}
else
console.log(info);
});

} catch (err) {
console.log(err);
//res.status(500).send("Internal server error!");
}
};

module.exports.send = send;
6 changes: 6 additions & 0 deletions src/modules/customer/customer.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const Cart = require(path.join(process.cwd(), 'src/modules/cart/cart.model'));
const Customer = require(path.join(process.cwd(), 'src/modules/customer/customer.model'));
const { generateAccessToken } = require(path.join(process.cwd(), 'src/modules/customer/services/customer.service'));
const cloudinary = require(path.join(process.cwd(), 'src/config/lib/cloudinary'));
const emailService = require(path.join(process.cwd(), 'src/modules/core/email/email.service'));
const Shop = require(path.join(process.cwd(), 'src/modules/shop/shop.model'));
const Product = require(path.join(process.cwd(), 'src/modules/product/product.model'));
const Order = require(path.join(process.cwd(), 'src/modules/order/order.model'));
Expand Down Expand Up @@ -30,6 +31,7 @@ async function logout(req, res) {
res.send('Logged out.');
}


const registerCustomer = async (req, res) => {
try {
const { username, email, password } = req.body;
Expand All @@ -45,6 +47,10 @@ const registerCustomer = async (req, res) => {

if (!created) return res.status(400).send("Already registered with this email address.");

const subject = "Verify your email";

emailService.send({subject, email});

res.status(201).send(customer);
} catch (err) {
console.log(err);
Expand Down
Loading

0 comments on commit a445fe7

Please sign in to comment.