send-mail/main.js

74 lines
2.3 KiB
JavaScript
Raw Normal View History

2020-03-01 05:01:03 +07:00
const nodemailer = require("nodemailer")
const core = require("@actions/core")
const fs = require("fs")
const showdown = require('showdown')
2020-03-01 05:01:03 +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) {
2020-03-25 22:18:35 +07:00
if (from.match(/.+<.+@.+>/)) {
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 {
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 body = core.getInput("body", { required: true })
const to = core.getInput("to", { required: true })
const from = core.getInput("from", { required: true })
const contentType = core.getInput("content_type", { required: true })
const attachments = core.getInput("attachments", { required: false })
const convertMarkdown = core.getInput("convert_markdown", { required: false })
2020-03-01 05:01:03 +07:00
2020-03-24 20:03:35 +07:00
const transport = nodemailer.createTransport({
host: serverAddress,
port: serverPort,
secure: serverPort == "465",
2020-03-24 20:03:35 +07:00
auth: {
user: username,
pass: password,
2020-03-01 05:13:30 +07:00
}
2020-03-24 20:03:35 +07:00
})
2020-03-01 05:01:03 +07:00
2020-09-24 15:27:30 +07:00
console.log(transport.options)
2020-09-24 15:24:29 +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,
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
2020-03-01 05:13:30 +07:00
})
2020-03-01 05:01:03 +07:00
console.log(info)
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()