-
Notifications
You must be signed in to change notification settings - Fork 7
/
email.ts
64 lines (56 loc) · 2.04 KB
/
email.ts
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
import nodemailer from "npm:nodemailer";
import config from "./config.json" with { type: "json" };
export async function sendEmail() {
const transporter = nodemailer.createTransport({
host: config.emailHost,
port: config.emailPort,
secure: !config.emailAllowSTARTTLS,
auth: {
user: config.emailUser,
pass: config.emailPassword,
},
});
const mailOptions = {
from: `${config.emailFrom} <${config.emailUser}>`,
to: config.emailRecipient,
subject: config.title,
text: `Sent by Omnivore EPUB (https://github.com/agrmohit/omnivore-epub)`,
attachments: [
{
path: config.outputFileName,
},
],
};
if (await verifyEpub()) {
try {
console.log(`📧 Sending email from '${config.emailFrom} <${config.emailUser}>' to '${config.emailRecipient}'`);
const info = await transporter.sendMail(mailOptions);
console.log(`📨 Email sent: ${info.messageId}`);
} catch (error) {
console.error(`🚫 Error: ${error}`);
Deno.exit(1);
}
}
}
async function verifyEpub(): Promise<boolean> {
try {
const file = await Deno.stat(config.outputFileName);
// Check if it is indeed a file
if (!file.isFile) {
console.error(`🚫 ${config.outputFileName} is not a file`);
Deno.exit(1);
}
// Convert from bytes to MB (not MiB) rounded off to 2 digits after decimal
const ebookSize = (file.size / 1_000_000).toFixed(2);
// Show a warning if ebook is over a specified size
if (!config.emailSizeWarningSuppress && Number(ebookSize) >= config.emailSizeWarningMinSize) {
console.warn(`⚠️ ebook size is too large at ${ebookSize} MB (limit: ${config.emailSizeWarningMinSize} MB)`);
console.warn("⚠️ Many email providers and eReader emailing services may reject this email");
console.warn("⚠️ To suppress this warning, set 'emailSuppressSizeWarning' to true in 'config.json'");
}
} catch (_err) {
console.error(`🚫 ebook file '${config.outputFileName}' is missing`);
Deno.exit(1);
}
return true;
}