2020-03-01 05:01:03 +07:00
|
|
|
const nodemailer = require("nodemailer")
|
|
|
|
const core = require("@actions/core")
|
|
|
|
const fs = require("fs")
|
2021-04-25 21:14:13 +07:00
|
|
|
const showdown = require("showdown")
|
2020-03-01 05:01:03 +07:00
|
|
|
|
2020-12-01 00:51:34 +07:00
|
|
|
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)
|
2020-03-01 05:01:03 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
return body
|
|
|
|
}
|
|
|
|
|
2020-09-24 15:24:29 +07:00
|
|
|
function getFrom(from, username) {
|
2021-04-25 21:14:13 +07:00
|
|
|
if (from.match(/.+ <.+@.+>/)) {
|
2020-03-25 22:18:35 +07:00
|
|
|
return from
|
|
|
|
}
|
|
|
|
|
|
|
|
return `"${from}" <${username}>`
|
|
|
|
}
|
|
|
|
|
2020-03-01 05:01:03 +07:00
|
|
|
async function main() {
|
2020-03-01 05:13:30 +07:00
|
|
|
try {
|
2020-12-01 00:51:34 +07:00
|
|
|
const serverAddress = core.getInput("server_address", { required: true })
|
|
|
|
const serverPort = core.getInput("server_port", { required: true })
|
2020-03-01 18:37:11 +07:00
|
|
|
const username = core.getInput("username", { required: true })
|
|
|
|
const password = core.getInput("password", { required: true })
|
|
|
|
const subject = core.getInput("subject", { required: true })
|
|
|
|
const from = core.getInput("from", { required: true })
|
2020-12-16 23:16:24 +07:00
|
|
|
const to = core.getInput("to", { required: true })
|
2021-04-25 21:11:54 +07:00
|
|
|
const body = core.getInput("body", { required: false })
|
|
|
|
const htmlBody = core.getInput("html_body", { required: false })
|
2020-12-16 23:16:24 +07:00
|
|
|
const cc = core.getInput("cc", { required: false })
|
|
|
|
const bcc = core.getInput("bcc", { required: false })
|
2021-04-08 00:34:58 +07:00
|
|
|
const replyTo = core.getInput("reply_to", { required: false })
|
2020-04-07 15:30:45 +07:00
|
|
|
const attachments = core.getInput("attachments", { required: false })
|
2020-12-01 00:51:34 +07:00
|
|
|
const convertMarkdown = core.getInput("convert_markdown", { required: false })
|
2021-03-26 14:42:12 +07:00
|
|
|
const ignoreCert = core.getInput("ignore_cert", { required: false })
|
2020-03-01 05:01:03 +07:00
|
|
|
|
2020-03-24 20:03:35 +07:00
|
|
|
const transport = nodemailer.createTransport({
|
2020-12-01 00:51:34 +07:00
|
|
|
host: serverAddress,
|
|
|
|
port: serverPort,
|
|
|
|
secure: serverPort == "465",
|
2020-03-24 20:03:35 +07:00
|
|
|
auth: {
|
|
|
|
user: username,
|
|
|
|
pass: password,
|
2021-04-02 14:11:00 +07:00
|
|
|
},
|
2021-03-26 14:42:12 +07:00
|
|
|
tls: ignoreCert ? {
|
|
|
|
rejectUnauthorized: false
|
|
|
|
} : undefined
|
2020-03-24 20:03:35 +07:00
|
|
|
})
|
2021-04-25 21:14:13 +07:00
|
|
|
|
2020-03-01 18:37:11 +07:00
|
|
|
const info = await transport.sendMail({
|
2020-09-24 15:24:29 +07:00
|
|
|
from: getFrom(from, username),
|
2020-03-01 05:13:30 +07:00
|
|
|
to: to,
|
2021-04-25 21:11:54 +07:00
|
|
|
subject: subject,
|
2020-12-16 23:16:24 +07:00
|
|
|
cc: cc ? cc : undefined,
|
|
|
|
bcc: bcc ? bcc : undefined,
|
2021-04-08 00:34:58 +07:00
|
|
|
replyTo: replyTo ? replyTo : undefined,
|
2021-04-25 21:19:34 +07:00
|
|
|
text: body ? getBody(body, false) : undefined,
|
2021-04-25 21:11:54 +07:00
|
|
|
html: htmlBody ? getBody(htmlBody, convertMarkdown) : undefined,
|
2020-04-07 15:30:45 +07:00
|
|
|
attachments: attachments ? attachments.split(',').map(f => ({ path: f.trim() })) : undefined
|
2020-03-01 05:13:30 +07:00
|
|
|
})
|
|
|
|
} catch (error) {
|
|
|
|
core.setFailed(error.message)
|
|
|
|
}
|
2020-03-01 05:01:03 +07:00
|
|
|
}
|
|
|
|
|
2020-03-25 22:06:11 +07:00
|
|
|
main()
|