m0603-bubble 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/06-02.js +73 -0
  2. package/index.html +0 -0
  3. package/package.json +17 -0
package/06-02.js ADDED
@@ -0,0 +1,73 @@
1
+ const http = require("http");
2
+ const qs = require("querystring");
3
+ const nodemailer = require("nodemailer");
4
+
5
+ http.createServer((req, res) => {
6
+
7
+ if (req.method === "GET") {
8
+ res.writeHead(200, {"Content-Type": "text/html; charset=utf-8"});
9
+ res.end(`
10
+ <html>
11
+ <body>
12
+ <h2>Send Email (OAuth2)</h2>
13
+ <form method="POST">
14
+ Auth Email (Gmail): <input name="auth_email"><br><br>
15
+ Access Token: <input name="access_token"><br><br>
16
+
17
+ From: <input name="from"><br><br>
18
+ To: <input name="to"><br><br>
19
+
20
+ Message:<br>
21
+ <textarea name="msg"></textarea><br><br>
22
+
23
+ <button type="submit">Send</button>
24
+ </form>
25
+ </body>
26
+ </html>
27
+ `);
28
+ }
29
+
30
+ if (req.method === "POST") {
31
+
32
+ let body = "";
33
+
34
+ req.on("data", chunk => {
35
+ body += chunk.toString();
36
+ });
37
+
38
+ req.on("end", async () => {
39
+
40
+ const data = qs.parse(body);
41
+
42
+ // создаем transporter на основе введенных данных
43
+ const transporter = nodemailer.createTransport({
44
+ service: "gmail",
45
+ auth: {
46
+ type: "OAuth2",
47
+ user: data.auth_email,
48
+ accessToken: data.access_token
49
+ }
50
+ });
51
+
52
+ try {
53
+ await transporter.sendMail({
54
+ from: data.from || data.auth_email,
55
+ to: data.to,
56
+ subject: "Message from Node.js",
57
+ html: data.msg
58
+ });
59
+
60
+ res.writeHead(200, {"Content-Type": "text/html"});
61
+ res.end("Mail sent!");
62
+
63
+ } catch (err) {
64
+ res.writeHead(500, {"Content-Type": "text/html"});
65
+ res.end("Error sending mail: " + err);
66
+ }
67
+
68
+ });
69
+ }
70
+
71
+ }).listen(3000);
72
+
73
+ console.log("Server running http://localhost:3000");
package/index.html ADDED
File without changes
package/package.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "name": "m0603-bubble",
3
+ "version": "1.0.0",
4
+ "description": "",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "keywords": [],
10
+ "author": "",
11
+ "license": "ISC",
12
+ "type": "commonjs",
13
+ "dependencies": {
14
+ "nodemailer": "^8.0.3",
15
+ "sendmail": "^1.6.1"
16
+ }
17
+ }