forked from lyy289065406/action-send-mail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
68 lines (60 loc) · 2.14 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
66
67
68
const nodemailer = require("nodemailer")
const core = require("@actions/core")
const fs = require("fs")
function load(value) {
if (value.startsWith("file://")) {
const filepath = value.replace("file://", "")
if(fs.existsSync(filepath)) {
value = fs.readFileSync(filepath, "utf8")
} else {
value = ""
}
}
return value
}
function get_from(from, username) {
if (from.match(/.+<.+@.+>/)) {
return from
}
return `"${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 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 attachments = core.getInput("attachments", { required: false })
// if the email content is empty, don't send it
content = load(body)
if (content == "") {
return
}
const transport = nodemailer.createTransport({
host: server_address,
port: server_port,
secure: server_port == "465",
auth: {
user: username,
pass: password,
}
})
const info = await transport.sendMail({
from: get_from(from, username),
to: load(to),
subject: subject,
text: content_type != "text/html" ? content : undefined,
html: content_type == "text/html" ? content : undefined,
attachments: attachments ? attachments.split(',').map(f => ({ path: f.trim() })) : undefined
})
console.log(info)
} catch (error) {
core.setFailed(error.message)
}
}
main()