-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmailsendattach.js
59 lines (59 loc) · 1.84 KB
/
mailsendattach.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
// save file as mailsendattch.js
var nodemailer = require('nodemailer');
var http = require('http');
var fs = require('fs');
var querystring = require('querystring');
var fs = require('fs');
const { transporter } = require('./transporter');
http
.createServer(function (req, res) {
console.log(req.url);
if (req.url === '/') {
fs.readFile('./public/mailsendattch.html', 'UTF-8', function (err, html) {
res.writeHead(200, { 'Content-type': 'text/html' });
res.end(html);
});
}
if (req.method === 'POST') {
var data = '';
req.on('data', function (chunk) {
data += chunk;
});
req.on('end', function (chunk) {
var q = querystring.parse(data);
/* var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
type: 'OAuth2',
user: process.env.USER,
clientId: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
refreshToken: process.env.REFRESH_TOKEN,
accessToken: process.env.ACCESS_TOKEN,
},
}); */
var mailOptions = {
from: '[email protected]', // sender's gmail
to: q.demail, // data coming from form
subject: q.dsub, // data coming from form
text: q.dmsg, // data coming from form
attachments: [
{
// file on disk as an attachment
filename: q.dfile, // name of the file attach
path: q.dfile,
},
],
};
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
console.log(error);
} else {
res.end('Mail sent successefully');
console.log('Email sent: ' + info.response);
}
});
});
}
})
.listen(3000);