mirror of
https://github.com/dawidd6/action-send-mail.git
synced 2025-10-11 14:01:27 +07:00
Add markdown format support (#25)
Co-authored-by: Dawid Dziurla <dawidd0811@gmail.com>
This commit is contained in:
35
main.js
35
main.js
@ -1,11 +1,21 @@
|
||||
const nodemailer = require("nodemailer")
|
||||
const core = require("@actions/core")
|
||||
const fs = require("fs")
|
||||
const showdown = require('showdown')
|
||||
|
||||
function getBody(body) {
|
||||
if (body.startsWith("file://")) {
|
||||
const file = body.replace("file://", "")
|
||||
return fs.readFileSync(file, "utf8")
|
||||
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
|
||||
@ -21,21 +31,22 @@ function getFrom(from, username) {
|
||||
|
||||
async function main() {
|
||||
try {
|
||||
const server_address = core.getInput("server_address", { required: true })
|
||||
const server_port = core.getInput("server_port", { required: true })
|
||||
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 to = core.getInput("to", { required: true })
|
||||
const from = core.getInput("from", { required: true })
|
||||
const content_type = core.getInput("content_type", { 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 })
|
||||
|
||||
const transport = nodemailer.createTransport({
|
||||
host: server_address,
|
||||
port: server_port,
|
||||
secure: server_port == "465",
|
||||
host: serverAddress,
|
||||
port: serverPort,
|
||||
secure: serverPort == "465",
|
||||
auth: {
|
||||
user: username,
|
||||
pass: password,
|
||||
@ -48,8 +59,8 @@ async function main() {
|
||||
from: getFrom(from, username),
|
||||
to: to,
|
||||
subject: subject,
|
||||
text: content_type != "text/html" ? getBody(body) : undefined,
|
||||
html: content_type == "text/html" ? getBody(body) : undefined,
|
||||
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
|
||||
})
|
||||
|
||||
|
Reference in New Issue
Block a user