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
| const fs = require("fs"); const path = require("path"); const nodemailer = require("nodemailer"); const ejs = require("ejs"); const { MAIL_TRANSPORT_CONFIG, MAIL_FROM, MAIL_TO } = require("./config"); const usersService = require("./services/users");
const renderHtml = async function () { const users = await usersService.query({ ename: null, deptno: null }); const template = ejs.compile( fs.readFileSync( path.resolve(__dirname, "template", "email.ejs"), "utf8" ), { async: false } ); const html = template({ title: "EDI Group", users: users, });
return html; };
async function mail() { let transporter = nodemailer.createTransport(MAIL_TRANSPORT_CONFIG);
let info = await transporter.sendMail({ from: MAIL_FROM, to: MAIL_TO, subject: "Hello ✔", text: "Hello world?", html: await renderHtml(), attachments: [ { filename: "data.json", path: "./data.json", }, ], });
console.log("Message sent: %s", info.messageId); }
mail().catch(console.error);
|