This commit is contained in:
2024-01-18 16:00:55 +08:00
parent f251b56675
commit 4ab9901cfb
3 changed files with 75 additions and 68 deletions

BIN
bin/amd64

Binary file not shown.

View File

@@ -1,4 +1,4 @@
[global]
SEND_MAIL = "yuling.niu@hohong.cn"; // 发送者邮箱
MAIL_KEY = "1234567"; // 发送者邮箱密钥
SMTP_SERVER = "smtphz.qiye.163.com:994"; // smtp服务器地址
MAIL_KEY = "8MWeAUdg3ttvMjKv"; // 发送者邮箱密钥
SMTP_SERVER = "smtphz.qiye.163.com:994"; // smtp服务器地址

View File

@@ -10,16 +10,70 @@ import (
"path/filepath"
"strings"
"github.com/jordan-wright/email"
gomail "github.com/jordan-wright/email"
"gopkg.in/ini.v1"
)
func GetCurrentDirectory() string {
dir, err := filepath.Abs(filepath.Dir(os.Args[0])) //返回绝对路径 filepath.Dir(os.Args[0])去除最后一个元素的路径
type Config struct {
SenderEmail string
EmailPassword string
SMTPServer string
}
func getConfigFilePath() string {
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
log.Fatal(err)
}
return strings.Replace(dir, "\\", "/", -1) //将\替换成/
return filepath.Join(dir, "gomail.ini")
}
func readConfig(configFile string) *Config {
config, err := ini.Load(configFile)
if err != nil {
log.Fatalf("Fail to read config file: %v", err)
}
return &Config{
SenderEmail: config.Section("global").Key("SEND_MAIL").String(),
EmailPassword: config.Section("global").Key("MAIL_KEY").String(),
SMTPServer: config.Section("global").Key("SMTP_SERVER").String(),
}
}
func createEmail(senderEmail string, recipients []string, subject, text, attachment string) *gomail.Email {
email := gomail.NewEmail()
email.From = senderEmail
email.To = recipients
email.Subject = subject
email.Text = []byte(text)
if attachment != "" {
email.AttachFile(attachment)
}
return email
}
func sendEmail(email *gomail.Email, smtpServer, senderEmail, emailPassword string) error {
hostSign := strings.Index(smtpServer, ":")
smtpServerHost := smtpServer[0:(len(smtpServer) - len(smtpServer[hostSign:]))]
smtpServerPort := smtpServer[hostSign+1:]
auth := smtp.PlainAuth("", senderEmail, emailPassword, smtpServerHost)
var err error
if smtpServerPort != "25" {
fmt.Println("SSL mode")
err = email.SendWithTLS(smtpServer, auth, &tls.Config{ServerName: smtpServerHost})
} else {
err = email.Send(smtpServer, auth)
}
if err != nil {
log.Fatalf("Failed to send email: %v", err)
}
return nil
}
func PathExists(path string) (bool, error) {
@@ -36,75 +90,28 @@ func PathExists(path string) (bool, error) {
}
func main() {
// 判断配置文件是否存在
INIFILE := GetCurrentDirectory() + "/" + "gomail.ini"
b, _ := PathExists(INIFILE)
if !b {
INIFILE = "/etc/gomail.ini"
configFile := getConfigFilePath()
inifile, _ := PathExists(configFile)
if !inifile {
configFile = "/etc/gomail.ini"
}
// 读取配置文件
cfg, inierr := ini.Load(INIFILE)
if inierr != nil {
fmt.Printf("Fail to read file: %v", inierr)
os.Exit(1)
}
config := readConfig(configFile)
SEND_MAIL := cfg.Section("global").Key("SEND_MAIL").String()
MAIL_KEY := cfg.Section("global").Key("MAIL_KEY").String()
SMTP_SERVER := cfg.Section("global").Key("SMTP_SERVER").String()
Text := flag.String("t", "", "文本")
Subject := flag.String("s", "", "主题")
is_Attac := flag.String("a", "", "添加附件")
To := flag.String("r", "", "接收者邮箱")
text := flag.String("t", "", "文本")
subject := flag.String("s", "", "主题")
attachment := flag.String("a", "", "添加附件")
recipients := flag.String("r", "", "接收者邮箱")
flag.Parse()
// 依据空格分割字符串取得接收者
strHaiCoder := *To
TO := strings.Fields(strHaiCoder)
fmt.Println("TO =", TO)
recipientsList := strings.Fields(*recipients)
fmt.Println("TO =", recipientsList)
// 简单设置 log 参数
log.SetFlags(log.Lshortfile | log.LstdFlags)
email := createEmail(config.SenderEmail, recipientsList, *subject, *text, *attachment)
e := email.NewEmail()
// 设置 sender 发送方 的邮箱 此处可以填写自己的邮箱
e.From = SEND_MAIL
// 设置 receiver 接收方 的邮箱 此处也可以填写自己的邮箱, 就是自己发邮件给自己
e.To = TO
// 设置主题
e.Subject = *Subject
// 简单设置文件发送的内容,暂时设置成纯文本
e.Text = []byte(*Text)
// 附件
if is_Attac != nil {
e.AttachFile(*is_Attac)
}
host_sign := strings.Index(SMTP_SERVER, ":")
SMTP_SERVER_Host := SMTP_SERVER[0:(len(SMTP_SERVER) - len(SMTP_SERVER[host_sign:]))]
SMTP_SERVER_Port := SMTP_SERVER[host_sign+1:]
auth := smtp.PlainAuth("", SEND_MAIL, MAIL_KEY, SMTP_SERVER_Host)
if SMTP_SERVER_Port != "25" {
fmt.Println("SSL mode")
err := e.SendWithTLS(SMTP_SERVER, auth, &tls.Config{ServerName: SMTP_SERVER_Host})
if err != nil {
log.Fatal(err)
}
} else {
err := e.Send(SMTP_SERVER, auth)
if err != nil {
log.Fatal(err)
}
err := sendEmail(email, config.SMTPServer, config.SenderEmail, config.EmailPassword)
if err != nil {
log.Fatalf("Failed to send email: %v", err)
}
log.Println("send successfully ... ")