-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
103 lines (77 loc) · 2.36 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
const nodemailer = require('nodemailer')
const express = require('express')
const multer = require('multer')
const CLIENT_ID = ""; // ID API
const CLIENT_SECRET = ""; //CLIENT_ID API
var bodyParser = require("body-parser")
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, 'public/uploads')
},
filename: function (req, file, cb) {
cb(null, Date.now() + path.extname(file.originalname))
}
})
var upload = multer({ storage: storage }).array('file',100);
const path = require('path')
const PORT = process.env.PORT || 3000
const app = express()
app.use(bodyParser.urlencoded({extended:true}));
app.get('/',(req,res)=>{
res.sendFile(__dirname +"/index.html" )
})
let paths = []
app.post('/sendemail' , (req,res)=>{
upload(req,res, (error)=>{
if (error) {
console.log("erreur pour charger le fichier ")
return;
}
else{
req.files.forEach(file =>{
paths.push({
filename:Date.now() + "file " + path.extname(file.originalname),
path:file.path
})
});
let user_email = req.body.email
let user_objet = req.body.objet
let user_message = req.body.message
sendEmail(paths,user_email,user_objet,user_message)
.then((result)=>{
res.send("Email envoyer voir votre boite mail ")
})
.catch((error)=>{
res.send("Email non envoyer ")
})
}
})
})
app.listen(PORT, () => {
console.log("application démarre en port 3000 ")
})
async function sendEmail(paths,user_email,user_objet,user_message) {
try {
const transport = nodemailer.createTransport({
service : 'gmail',
auth : {
type: 'OAuth2',
user: 'useremail',
clientId: CLIENT_ID,
clientSecret: CLIENT_SECRET,
accessToken :'',
},
});
const mailOptions = {
from: user_email,
to: '',
subject: user_objet ,
text:user_message,
attachments: paths
}
const result = await transport.sendMail(mailOptions);
return result;
} catch (error){
return error;
}
}