rss/mail/mail.go

47 lines
1.0 KiB
Go
Raw Normal View History

2023-07-09 14:57:47 +00:00
package mail
import (
2023-07-11 04:12:03 +00:00
"errors"
2023-07-09 14:57:47 +00:00
"log"
"os"
"os/exec"
)
2023-07-11 04:12:03 +00:00
func SendMail(subject string, content string, files ...string) error {
2023-07-09 14:57:47 +00:00
cm := os.Getenv("mail_cmd_path")
if cm == "" {
2023-07-11 04:12:03 +00:00
return errors.New("cmd can't empty")
2023-07-09 14:57:47 +00:00
}
subjectParam := os.Getenv("mail_cmd_subject_param_name")
if subjectParam == "" {
2023-07-11 04:12:03 +00:00
return errors.New("title param name empty")
2023-07-09 14:57:47 +00:00
}
contentParam := os.Getenv("mail_cmd_content_param_name")
if contentParam == "" {
2023-07-11 04:12:03 +00:00
return errors.New("content param name emtpy")
2023-07-09 14:57:47 +00:00
}
fileParam := os.Getenv("mail_cmd_file_param_name")
if fileParam == "" {
2023-07-11 04:12:03 +00:00
return errors.New("file empty")
2023-07-09 14:57:47 +00:00
}
v := []string{subjectParam, subject, contentParam, content}
for _, f := range files {
if f != "" {
v = append(v, fileParam, f)
}
}
cmd := exec.Command(cm, v...)
2023-07-11 04:12:03 +00:00
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
}
2023-07-09 14:57:47 +00:00
output, err := cmd.CombinedOutput()
2023-07-11 04:12:03 +00:00
log.Println(string(output))
return err
2023-07-09 14:57:47 +00:00
}