镜像自地址
https://github.com/betterfor/action-send-mail.git
已同步 2026-11-20 16:37:04 +00:00
send mail
这个提交包含在:
@ -1,3 +0,0 @@
|
||||
#!/bin/sh -l
|
||||
|
||||
echo "hello $1"
|
||||
23
go.mod
23
go.mod
@ -1,3 +1,26 @@
|
||||
module github.com/betterfor/action-send-mail
|
||||
|
||||
go 1.17
|
||||
|
||||
require (
|
||||
github.com/fsnotify/fsnotify v1.5.4 // indirect
|
||||
github.com/hashicorp/hcl v1.0.0 // indirect
|
||||
github.com/magiconair/properties v1.8.6 // indirect
|
||||
github.com/mitchellh/mapstructure v1.5.0 // indirect
|
||||
github.com/pelletier/go-toml v1.9.5 // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
|
||||
github.com/russross/blackfriday v1.6.0 // indirect
|
||||
github.com/spf13/afero v1.8.2 // indirect
|
||||
github.com/spf13/cast v1.5.0 // indirect
|
||||
github.com/spf13/jwalterweatherman v1.1.0 // indirect
|
||||
github.com/spf13/pflag v1.0.5 // indirect
|
||||
github.com/spf13/viper v1.13.0 // indirect
|
||||
github.com/subosito/gotenv v1.4.1 // indirect
|
||||
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
|
||||
golang.org/x/text v0.3.7 // indirect
|
||||
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
|
||||
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df // indirect
|
||||
gopkg.in/ini.v1 v1.67.0 // indirect
|
||||
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
||||
87
main.go
87
main.go
@ -1,13 +1,92 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"os"
|
||||
"github.com/russross/blackfriday"
|
||||
"github.com/spf13/viper"
|
||||
"gopkg.in/gomail.v2"
|
||||
"io/ioutil"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func init() {
|
||||
viper.AutomaticEnv()
|
||||
viper.SetEnvPrefix("input_")
|
||||
}
|
||||
|
||||
func main() {
|
||||
envs := os.Environ()
|
||||
for _, e := range envs {
|
||||
fmt.Println(e)
|
||||
mailHost := viper.GetString("server_address")
|
||||
mailPort := viper.GetInt("server_port")
|
||||
secure := viper.GetBool("secure")
|
||||
username := viper.GetString("username")
|
||||
password := viper.GetString("password")
|
||||
subject := viper.GetString("subject")
|
||||
to := strings.Split(viper.GetString("to"), ",")
|
||||
from := viper.GetString("from")
|
||||
body := viper.GetString("body")
|
||||
htmlBody := viper.GetString("html_body")
|
||||
cc := strings.Split(viper.GetString("cc"), ",")
|
||||
bcc := strings.Split(viper.GetString("bcc"), ",")
|
||||
replyTo := viper.GetString("reply_to")
|
||||
inReplyTo := viper.GetString("in_reply_to")
|
||||
//ignoreCert := viper.GetBool("ignore_cert")
|
||||
convertMarkdown := viper.GetBool("convert_markdown")
|
||||
attachments := strings.Split(viper.GetString("attachments"), ",")
|
||||
priority := viper.GetString("priority")
|
||||
|
||||
m := gomail.NewMessage()
|
||||
m.SetHeader("Subject", subject)
|
||||
m.SetHeader("From", from)
|
||||
m.SetHeader("To", to...)
|
||||
m.SetHeader("Cc", cc...)
|
||||
m.SetHeader("Bcc", bcc...)
|
||||
m.SetHeader("Reply-To", replyTo)
|
||||
m.SetHeader("In-Reply-To", inReplyTo)
|
||||
m.SetHeader("X-Priority", priority)
|
||||
|
||||
for _, a := range attachments {
|
||||
m.Attach(a)
|
||||
}
|
||||
|
||||
if !secure {
|
||||
secure = mailPort == 465
|
||||
}
|
||||
|
||||
if body != "" {
|
||||
m.SetBody("text/plain", getBody(body, false))
|
||||
}
|
||||
if htmlBody != "" {
|
||||
m.SetBody("text/html", getBody(htmlBody, convertMarkdown))
|
||||
}
|
||||
|
||||
dialer := gomail.NewDialer(mailHost, mailPort, username, password)
|
||||
if secure {
|
||||
dialer.TLSConfig = &tls.Config{InsecureSkipVerify: true}
|
||||
}
|
||||
|
||||
err := dialer.DialAndSend(m)
|
||||
if err != nil {
|
||||
fmt.Println(time.Now(), "send mail error: ", err)
|
||||
}
|
||||
}
|
||||
|
||||
func getBody(bodyOrFile string, convertMarkdown bool) string {
|
||||
var body = bodyOrFile
|
||||
if strings.HasPrefix(bodyOrFile, "file://") {
|
||||
file := strings.TrimPrefix(bodyOrFile, "file://")
|
||||
bodyBts, err := ioutil.ReadFile(file)
|
||||
if err != nil {
|
||||
fmt.Println("attach file error: ", err)
|
||||
return err.Error()
|
||||
}
|
||||
body = string(bodyBts)
|
||||
}
|
||||
|
||||
if convertMarkdown {
|
||||
body = string(blackfriday.MarkdownBasic([]byte(body)))
|
||||
}
|
||||
|
||||
return body
|
||||
}
|
||||
|
||||
在新工单中引用
屏蔽一个用户