This repository has been archived by the owner on Jan 10, 2024. It is now read-only.
forked from juanformoso/action-send-mail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
65 lines (57 loc) · 2.31 KB
/
main.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
const nodemailer = require("nodemailer")
const core = require("@actions/core")
const fs = require("fs")
const showdown = require('showdown')
function getBody(bodyOrFile, convertMarkdown) {
let body = bodyOrFile
// Read body from file
if (bodyOrFile.startsWith("file://")) {
const file = bodyOrFile.replace("file://", "")
body = fs.readFileSync(file, "utf8")
}
// Convert Markdown to HTML
if (convertMarkdown) {
const converter = new showdown.Converter()
body = converter.makeHtml(body)
}
return body
}
async function main() {
try {
const serverAddress = core.getInput("server_address", { required: true })
const serverPort = core.getInput("server_port", { required: true })
const username = core.getInput("username", { required: true })
const password = core.getInput("password", { required: true })
const subject = core.getInput("subject", { required: true })
const body = core.getInput("body", { required: true })
const from = core.getInput("from", { required: true })
const to = core.getInput("to", { required: true })
const cc = core.getInput("cc", { required: false })
const bcc = core.getInput("bcc", { required: false })
const contentType = core.getInput("content_type", { required: true })
const attachments = core.getInput("attachments", { required: false })
const convertMarkdown = core.getInput("convert_markdown", { required: false })
const transport = nodemailer.createTransport({
host: serverAddress,
port: serverPort,
secure: serverPort == "465",
auth: {
user: username,
pass: password,
}
})
const info = await transport.sendMail({
from: from,
to: to,
cc: cc ? cc : undefined,
bcc: bcc ? bcc : undefined,
subject: subject,
text: contentType != "text/html" ? getBody(body, convertMarkdown) : undefined,
html: contentType == "text/html" ? getBody(body, convertMarkdown) : undefined,
attachments: attachments ? attachments.split(',').map(f => ({ path: f.trim() })) : undefined
})
} catch (error) {
core.setFailed(error.message)
}
}
main()