m06_paa 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
package/06-02.html ADDED
@@ -0,0 +1,30 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>lw5</title>
7
+ <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
8
+ </head>
9
+ <body>
10
+ <div class="container">
11
+ <h1>Lab 6</h1>
12
+ <div class="form">
13
+ <form action="http://localhost:5000/" method="POST">
14
+ <hr />
15
+ <label>Sender:</label>
16
+ <input type="email" name="sender" required /><br /><br />
17
+ <label>Password:</label>
18
+ <input type="password" name="password" required /><br /><br />
19
+ <label>Receiver:</label>
20
+ <input type="email" name="receiver" required /><br /><br />
21
+ <label>Message:</label>
22
+ <input type="text" name="message" required /><br /><br />
23
+ <hr />
24
+ <button type="submit">Send</button>
25
+ </form>
26
+ </div>
27
+ </div>
28
+ <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL" crossorigin="anonymous"></script>
29
+ </body>
30
+ </html>
package/06-02.js ADDED
@@ -0,0 +1,27 @@
1
+ const nodemailer = require('nodemailer');
2
+
3
+ // Создаем транспорт для отправки писем
4
+ let transporter = nodemailer.createTransport({
5
+ service: 'mail',
6
+ auth: {
7
+ user: 'example@gmail.com', // почта отправителя
8
+ pass: 'password' // пароль отправителя
9
+ }
10
+ });
11
+
12
+ // Настройки отправляемого письма
13
+ let mailOptions = {
14
+ from: 'example@gmail.com', // почта отправителя
15
+ to: 'recipient@example.com', // почта получателя
16
+ subject: 'Тестовое письмо',
17
+ text: 'Привет, это тестовое письмо!'
18
+ };
19
+
20
+ // Отправляем письмо
21
+ transporter.sendMail(mailOptions, function(error, info){
22
+ if (error) {
23
+ console.log(error);
24
+ } else {
25
+ console.log('Письмо успешно отправлено: ' + info.response);
26
+ }
27
+ });
package/06-03.js ADDED
@@ -0,0 +1,48 @@
1
+ const http = require("http");
2
+ const url = require("url");
3
+ const fs = require("fs");
4
+ const { parse } = require("querystring");
5
+ const nodemailer = require("nodemailer");
6
+ const send = require("./m06_PAA");
7
+
8
+ const server = http.createServer().listen(5000);
9
+ console.log("http://localhost:5000/");
10
+
11
+ server.on("request", (req, res) => {
12
+ const path = url.parse(req.url).pathname;
13
+
14
+ if (path === "/" && req.method === "GET") {
15
+ fs.readFile("06-02.html", (err, data) => {
16
+ if (err) {
17
+ console.error(err);
18
+ return;
19
+ }
20
+ res.writeHead(200, { "Content-Type": "text/html" });
21
+ res.end(data);
22
+ });
23
+ } else if (path === "/" && req.method === "POST") {
24
+ let body = "";
25
+
26
+ req.on("data", (chunk) => {
27
+ body += chunk.toString();
28
+ });
29
+
30
+ req.on("end", () => {
31
+ let params = parse(body);
32
+
33
+ send.send(params.sender, params.password, params.receiver, params.message)
34
+ .then(response => console.log(`Письмо успешно отправлено: ${response}`))
35
+ .catch(error => console.log(`Ошибка при отправке письма: ${error}`));
36
+
37
+ res.writeHead(200, { "Content-Type": "text/html" });
38
+ res.end(`<h1>${params.message}</h1>`);
39
+ });
40
+ }
41
+ });
42
+
43
+
44
+ // const message = 'Привет, это сообщение отправлено из модуля m06_XXX';
45
+
46
+ // send.send("paaworker@gmail.com", message)
47
+ // .then(response => console.log(`Письмо успешно отправлено: ${response}`))
48
+ // .catch(error => console.log(`Ошибка при отправке письма: ${error}`));
package/m06_PAA.js ADDED
@@ -0,0 +1,32 @@
1
+ const nodemailer = require('nodemailer');
2
+
3
+ function send(user, pass, toMail, message) {
4
+ let transporter = nodemailer.createTransport({
5
+ host: 'smtp.mail.ru',
6
+ port: 587,
7
+ secure: false,
8
+ auth: {
9
+ user: user,
10
+ pass: pass
11
+ }
12
+ });
13
+
14
+ let mailOptions = {
15
+ from: user,
16
+ to: toMail,
17
+ subject: 'Сообщение от модуля m06_PAA',
18
+ text: message
19
+ };
20
+
21
+ return new Promise((resolve, reject) => {
22
+ transporter.sendMail(mailOptions, (error, info) => {
23
+ if (error) {
24
+ reject(error);
25
+ } else {
26
+ resolve(info.response);
27
+ }
28
+ });
29
+ });
30
+ }
31
+
32
+ module.exports = { send };
package/package.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "dependencies": {
3
+ "nodemailer": "^6.9.5"
4
+ },
5
+ "name": "m06_paa",
6
+ "version": "1.0.0",
7
+ "description": "-",
8
+ "main": "m06_PAA.js",
9
+ "devDependencies": {},
10
+ "scripts": {
11
+ "test": "echo \"Error: no test specified\" && exit 1"
12
+ },
13
+ "author": "",
14
+ "license": "ISC"
15
+ }