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 06:54:17 +00:00
|
|
|
var cm, subjectParam, contentParam, fileParam string
|
|
|
|
|
|
|
|
func CheckEnv() error {
|
|
|
|
cm = os.Getenv("mail_cmd_path")
|
2023-07-09 14:57:47 +00:00
|
|
|
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
|
|
|
}
|
2023-07-11 06:54:17 +00:00
|
|
|
subjectParam = os.Getenv("mail_cmd_subject_param_name")
|
2023-07-09 14:57:47 +00:00
|
|
|
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
|
|
|
}
|
2023-07-11 06:54:17 +00:00
|
|
|
contentParam = os.Getenv("mail_cmd_content_param_name")
|
2023-07-09 14:57:47 +00:00
|
|
|
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
|
|
|
}
|
2023-07-11 06:54:17 +00:00
|
|
|
fileParam = os.Getenv("mail_cmd_file_param_name")
|
2023-07-09 14:57:47 +00:00
|
|
|
if fileParam == "" {
|
2023-07-11 04:12:03 +00:00
|
|
|
return errors.New("file empty")
|
2023-07-09 14:57:47 +00:00
|
|
|
}
|
2023-07-11 06:54:17 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func SendMail(subject string, content string, files ...string) error {
|
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
|
|
|
}
|