From 83a176543d3a887fccbe538db2838b6963b64071 Mon Sep 17 00:00:00 2001 From: Sergey Yarkov Date: Thu, 16 Jan 2020 21:36:12 +0300 Subject: [PATCH] nodemailer --- README.md | 2 ++ config/nodemailer.config.js | 13 +++++++++++++ config/routes/success.js | 23 ++++++++++++++++++----- package-lock.json | 5 +++++ package.json | 1 + 5 files changed, 39 insertions(+), 5 deletions(-) create mode 100644 config/nodemailer.config.js diff --git a/README.md b/README.md index 2ece210..5b9b6dd 100644 --- a/README.md +++ b/README.md @@ -11,4 +11,6 @@ SESSION_SECRET=secret DATABASE_URI=подключение к БД MONGODB DATABASE_NAME=имя БД QIWI_SECRET_KEY=секретный ключ QIWI API +EMAIL_LOGIN=логин от почты +EMAIL_PASSWORD=пароль от почты ``` \ No newline at end of file diff --git a/config/nodemailer.config.js b/config/nodemailer.config.js new file mode 100644 index 0000000..c0e80b6 --- /dev/null +++ b/config/nodemailer.config.js @@ -0,0 +1,13 @@ +const nodemailer = require('nodemailer'); + +module.exports = { + transporter: nodemailer.createTransport({ + host: "smtp.mail.ru", + port: 465, + secure: true, // true for 465, false for other ports + auth: { + user: process.env.EMAIL_LOGIN, // email login + pass: process.env.EMAIL_PASSWORD // email password + } + }) +} \ No newline at end of file diff --git a/config/routes/success.js b/config/routes/success.js index 422a153..83a8b07 100644 --- a/config/routes/success.js +++ b/config/routes/success.js @@ -1,4 +1,5 @@ const ObjectID = require('mongodb').ObjectID; +const nodemailer = require('../nodemailer.config'); module.exports = function(server, db, qiwiApi) { server.get('/success', (req, res) => { @@ -15,13 +16,13 @@ module.exports = function(server, db, qiwiApi) { amount = parseFloat(data.amount.value); if (status === 'PAID') { - const ids = query.item_id.split('and'); - const findBill = await db.collection('buyers').findOne({bill_id: query.bill_id}); + const ids = query.item_id.split('and'), + findBill = await db.collection('buyers').findOne({bill_id: query.bill_id}); if(findBill == null) { const result = []; - //выдаем данные и создаем покупателя + // создаем покупателя и выдаем данные for (let i = 0; i < ids.length; i++) { const item = await db.collection('info').findOne({"_id": ObjectID(ids[i])}), data = item.data; @@ -29,8 +30,19 @@ module.exports = function(server, db, qiwiApi) { result.push(data.splice(-1, 1).join('')); db.collection('info').updateOne({"_id": ObjectID(ids[i])}, {$set: {data: data}}); } - db.collection('buyers').insertOne({bill_id, email, method, date, amount, data: result}); + + await nodemailer.transporter.sendMail({ + from: `"Оплата аккаунта." <${process.env.EMAIL_LOGIN}>`, // sender address + to: email, // list of receivers + subject: "Оплата успешно проведена.", // subject + text: "Данные купленного товара находятся в этом письме.", // plain text body + html: `Номер заказа: ${bill_id}
+ Данные товара:
+

${result.join('
')}

+ ` // html body + }); + res.render('main/success', {result, email}); } else { res.send('Этот заказ уже был оплачен. Данные были высланы покупателю.'); @@ -41,7 +53,8 @@ module.exports = function(server, db, qiwiApi) { res.send('Время жизни счета истекло. Счет не оплачен'); } }) - .catch(() => { + .catch((error) => { + console.log(error); res.render('main/404'); }); } else { diff --git a/package-lock.json b/package-lock.json index a38b513..f04bcf1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5474,6 +5474,11 @@ } } }, + "nodemailer": { + "version": "6.4.2", + "resolved": "https://registry.npmjs.org/nodemailer/-/nodemailer-6.4.2.tgz", + "integrity": "sha512-g0n4nH1ONGvqYo1v72uSWvF/MRNnnq1LzmSzXb/6EPF3LFb51akOhgG3K2+aETAsJx90/Q5eFNTntu4vBCwyQQ==" + }, "nodemon": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/nodemon/-/nodemon-2.0.2.tgz", diff --git a/package.json b/package.json index b24ea87..85ac1e4 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,7 @@ "method-override": "^3.0.0", "mongodb": "^3.3.4", "multer": "^1.4.2", + "nodemailer": "^6.4.2", "passport": "^0.4.0", "passport-local": "^1.0.0", "pug": "^2.0.4",