fix send mail fail

这个提交包含在:
betterfor
2022-10-10 10:35:23 +08:00
父节点 717f278383
当前提交 8d13d90fc9
共有 2 个文件被更改,包括 28 次插入 和 29 次删除

查看文件

@ -60,7 +60,7 @@ jobs:
server_port: ${{secrets.ADDRESS_PORT}} server_port: ${{secrets.ADDRESS_PORT}}
username: ${{secrets.USERNAME}} username: ${{secrets.USERNAME}}
password: ${{secrets.PASSWORD}} password: ${{secrets.PASSWORD}}
subject: ${{matrix.body}} subject: ${{matrix.subject}}
body: ${{matrix.body}} body: ${{matrix.body}}
html_body: ${{matrix.html_body}} html_body: ${{matrix.html_body}}
to: ${{github.event.pusher.email}} to: ${{github.event.pusher.email}}

47
main.go
查看文件

@ -4,6 +4,7 @@ import (
"crypto/tls" "crypto/tls"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log"
"os" "os"
"regexp" "regexp"
"strconv" "strconv"
@ -15,49 +16,50 @@ import (
) )
func main() { func main() {
envs := os.Environ()
for _, e := range envs {
if strings.HasPrefix(e, "INPUT_") {
fmt.Println(e, "=", os.Getenv(e))
}
}
mailHost := os.Getenv("INPUT_SERVER_ADDRESS") mailHost := os.Getenv("INPUT_SERVER_ADDRESS")
mailPort := convertInt(os.Getenv("INPUT_SERVER_PORT")) mailPort := convertInt(os.Getenv("INPUT_SERVER_PORT"))
secure := convertBool(os.Getenv("INPUT_SECURE")) secure := convertBool(os.Getenv("INPUT_SECURE"))
username := os.Getenv("INPUT_USERNAME") username := os.Getenv("INPUT_USERNAME")
password := os.Getenv("INPUT_PASSWORD") password := os.Getenv("INPUT_PASSWORD")
subject := os.Getenv("INPUT_SUBJECT") subject := os.Getenv("INPUT_SUBJECT")
to := strings.Split(os.Getenv("INPUT_TO"), ",") to := os.Getenv("INPUT_TO")
from := os.Getenv("INPUT_FROM") from := os.Getenv("INPUT_FROM")
body := os.Getenv("INPUT_BODY") body := os.Getenv("INPUT_BODY")
htmlBody := os.Getenv("INPUT_HTML_BODY") htmlBody := os.Getenv("INPUT_HTML_BODY")
cc := strings.Split(os.Getenv("INPUT_CC"), ",") cc := os.Getenv("INPUT_CC")
bcc := strings.Split(os.Getenv("INPUT_BCC"), ",") bcc := os.Getenv("INPUT_BCC")
replyTo := os.Getenv("INPUT_REPLY_TO") replyTo := os.Getenv("INPUT_REPLY_TO")
inReplyTo := os.Getenv("INPUT_IN_REPLY_TO") inReplyTo := os.Getenv("INPUT_IN_REPLY_TO")
//ignoreCert := viper.GetBool("ignore_cert") //ignoreCert := viper.GetBool("ignore_cert")
convertMarkdown := convertBool(os.Getenv("INPUT_CONVERT_MARKDOWN")) convertMarkdown := convertBool(os.Getenv("INPUT_CONVERT_MARKDOWN"))
attachments := strings.Split(os.Getenv("INPUT_ATTACHMENTS"), ",") attachments := os.Getenv("INPUT_ATTACHMENTS")
priority := os.Getenv("INPUT_PRIORITY") priority := os.Getenv("INPUT_PRIORITY")
m := gomail.NewMessage() m := gomail.NewMessage()
m.SetHeader("Subject", subject) m.SetHeader("Subject", subject)
m.SetHeader("From", m.FormatAddress(username, from)) m.SetHeader("From", m.FormatAddress(username, from))
m.SetHeader("To", to...) m.SetHeader("To", strings.Split(to, ",")...)
m.SetHeader("Cc", cc...) if len(cc) != 0 {
m.SetHeader("Bcc", bcc...) m.SetHeader("Cc", strings.Split(cc, ",")...)
}
if len(bcc) != 0 {
m.SetHeader("Bcc", strings.Split(bcc, ",")...)
}
if len(replyTo) != 0 {
m.SetHeader("Reply-To", replyTo) m.SetHeader("Reply-To", replyTo)
}
if len(inReplyTo) != 0 {
m.SetHeader("In-Reply-To", inReplyTo) m.SetHeader("In-Reply-To", inReplyTo)
}
if len(priority) != 0 {
m.SetHeader("X-Priority", priority) m.SetHeader("X-Priority", priority)
}
fmt.Println("Subject: ", subject) if len(attachments) != 0 {
fmt.Println("From: ", m.FormatAddress(username, from)) for _, a := range strings.Split(attachments, ",") {
fmt.Println("To: ", to)
for _, a := range attachments {
m.Attach(a) m.Attach(a)
} }
}
if !secure { if !secure {
secure = mailPort == 465 secure = mailPort == 465
@ -75,11 +77,9 @@ func main() {
dialer.TLSConfig = &tls.Config{InsecureSkipVerify: true} dialer.TLSConfig = &tls.Config{InsecureSkipVerify: true}
} }
fmt.Println()
err := dialer.DialAndSend(m) err := dialer.DialAndSend(m)
if err != nil { if err != nil {
fmt.Println(time.Now(), "send mail error: ", err) log.Fatal(time.Now(), "send mail error: ", err)
} }
} }
@ -89,8 +89,7 @@ func getBody(bodyOrFile string, convertMarkdown bool) string {
file := strings.TrimPrefix(bodyOrFile, "file://") file := strings.TrimPrefix(bodyOrFile, "file://")
bodyBts, err := ioutil.ReadFile(file) bodyBts, err := ioutil.ReadFile(file)
if err != nil { if err != nil {
fmt.Println("attach file error: ", err) log.Fatal(err)
return err.Error()
} }
body = string(bodyBts) body = string(bodyBts)
} }