rss/mail/mail.go
2023-07-11 12:12:03 +08:00

47 lines
1.0 KiB
Go

package mail
import (
"errors"
"log"
"os"
"os/exec"
)
func SendMail(subject string, content string, files ...string) 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")
}
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
}