-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
81 lines (71 loc) · 2.26 KB
/
Copy pathtest.js
File metadata and controls
81 lines (71 loc) · 2.26 KB
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
const { createReport } = require('docx-templates')
const fs = require('fs')
const path = require("path");
require('dotenv').config()
const { MailtrapClient } = require("mailtrap");
const TOKEN = process.env.MAILER_TOKEN;
const ENDPOINT = process.env.MAILER_SERVER;
const client = new MailtrapClient({ endpoint: ENDPOINT, token: TOKEN });
const sender = {
email: process.env.MAILER_SENDER,
name: process.env.MAILER_NAME,
};
const sendEmail = async(receivers, subject, content, category) => {
try {
var recipients = []
for (var x in receivers) {
recipients.push({ email: receivers[x] })
}
const saftfile = fs.readFileSync(path.join(__dirname, "output.docx"));
var ret = await client.send({
from: sender,
to: recipients,
subject: subject,
html: content,
category: category,
attachments: [
{
filename: "saft.docx",
content_id: "saft.docx",
disposition: "inline",
content: saftfile
}
]
})
return ret
} catch (error) {
console.log("sendEmail", error)
return {
success: false, error: [ 'connection failed 2' ]
}
}
}
module.exports.sendEmail = sendEmail
const run = async() => {
const template = fs.readFileSync('input.docx');
const buffer = await createReport({
template,
data: {
preparedfor: "John2",
date: "4th August 2023",
},
additionalJsContext: {
injectSvg: () => {
const svg_data = Buffer.from(`<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<rect x="10" y="5" height="20" width="100" style="stroke:#ff0000; fill: #0000ff"/>
</svg>`, 'utf-8');
// Providing a thumbnail is technically optional, as newer versions of Word will just ignore it.
const thumbnail = {
data: fs.readFileSync('image.png'),
extension: '.png',
};
return { width: 6, height: 3, data: svg_data, extension: '.svg', thumbnail };
}
},
cmdDelimiter: ['{', '}'],
});
fs.writeFileSync('output.docx', buffer)
var ret = await sendEmail(['sceptre520@gmail.com'], "TestSaft", "TestSaft Content", "Service");
console.log(ret)
}
run()