rss/mail/mail.go
2023-07-11 14:54:17 +08:00

53 lines
1.1 KiB
Go

package mail
import (
"errors"
"log"
"os"
"os/exec"
)
var cm, subjectParam, contentParam, fileParam string
func CheckEnv() error {
cm = os.Getenv("mail_cmd_path")
if cm == "" {
return errors.New("cmd can't empty")
}
subjectParam = os.Getenv("mail_cmd_subject_param_name")
if subjectParam == "" {
return errors.New("title param name empty")
}
contentParam = os.Getenv("mail_cmd_content_param_name")
if contentParam == "" {
return errors.New("content param name emtpy")
}
fileParam = os.Getenv("mail_cmd_file_param_name")
if fileParam == "" {
return errors.New("file empty")
}
return nil
}
func SendMail(subject string, content string, files ...string) error {
v := []string{subjectParam, subject, contentParam, content}
for _, f := range files {
if f != "" {
v = append(v, fileParam, f)
}
}
cmd := exec.Command(cm, v...)
file, err := os.OpenFile("run.sh", os.O_CREATE|os.O_APPEND, 0755)
if err != nil {
return err
}
_, err = file.WriteString(cmd.String())
if err != nil {
return err
}
output, err := cmd.CombinedOutput()
log.Println(string(output))
return err
}