优化
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
[global]
|
[global]
|
||||||
SEND_MAIL = "yuling.niu@hohong.cn"; // 发送者邮箱
|
SEND_MAIL = "yuling.niu@hohong.cn"; // 发送者邮箱
|
||||||
MAIL_KEY = "1234567"; // 发送者邮箱密钥
|
MAIL_KEY = "8MWeAUdg3ttvMjKv"; // 发送者邮箱密钥
|
||||||
SMTP_SERVER = "smtphz.qiye.163.com:994"; // smtp服务器地址
|
SMTP_SERVER = "smtphz.qiye.163.com:994"; // smtp服务器地址
|
||||||
|
|||||||
139
src/main.go
139
src/main.go
@@ -10,16 +10,70 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/jordan-wright/email"
|
gomail "github.com/jordan-wright/email"
|
||||||
"gopkg.in/ini.v1"
|
"gopkg.in/ini.v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetCurrentDirectory() string {
|
type Config struct {
|
||||||
dir, err := filepath.Abs(filepath.Dir(os.Args[0])) //返回绝对路径 filepath.Dir(os.Args[0])去除最后一个元素的路径
|
SenderEmail string
|
||||||
|
EmailPassword string
|
||||||
|
SMTPServer string
|
||||||
|
}
|
||||||
|
|
||||||
|
func getConfigFilePath() string {
|
||||||
|
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
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) {
|
func PathExists(path string) (bool, error) {
|
||||||
@@ -36,75 +90,28 @@ func PathExists(path string) (bool, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// 判断配置文件是否存在
|
configFile := getConfigFilePath()
|
||||||
INIFILE := GetCurrentDirectory() + "/" + "gomail.ini"
|
inifile, _ := PathExists(configFile)
|
||||||
b, _ := PathExists(INIFILE)
|
if !inifile {
|
||||||
|
configFile = "/etc/gomail.ini"
|
||||||
if !b {
|
|
||||||
INIFILE = "/etc/gomail.ini"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 读取配置文件
|
config := readConfig(configFile)
|
||||||
cfg, inierr := ini.Load(INIFILE)
|
|
||||||
if inierr != nil {
|
|
||||||
fmt.Printf("Fail to read file: %v", inierr)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
SEND_MAIL := cfg.Section("global").Key("SEND_MAIL").String()
|
text := flag.String("t", "", "文本")
|
||||||
MAIL_KEY := cfg.Section("global").Key("MAIL_KEY").String()
|
subject := flag.String("s", "", "主题")
|
||||||
SMTP_SERVER := cfg.Section("global").Key("SMTP_SERVER").String()
|
attachment := flag.String("a", "", "添加附件")
|
||||||
|
recipients := flag.String("r", "", "接收者邮箱")
|
||||||
Text := flag.String("t", "", "文本")
|
|
||||||
Subject := flag.String("s", "", "主题")
|
|
||||||
is_Attac := flag.String("a", "", "添加附件")
|
|
||||||
To := flag.String("r", "", "接收者邮箱")
|
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
// 依据空格分割字符串取得接收者
|
recipientsList := strings.Fields(*recipients)
|
||||||
strHaiCoder := *To
|
fmt.Println("TO =", recipientsList)
|
||||||
TO := strings.Fields(strHaiCoder)
|
|
||||||
fmt.Println("TO =", TO)
|
|
||||||
|
|
||||||
// 简单设置 log 参数
|
email := createEmail(config.SenderEmail, recipientsList, *subject, *text, *attachment)
|
||||||
log.SetFlags(log.Lshortfile | log.LstdFlags)
|
|
||||||
|
|
||||||
e := email.NewEmail()
|
err := sendEmail(email, config.SMTPServer, config.SenderEmail, config.EmailPassword)
|
||||||
// 设置 sender 发送方 的邮箱 , 此处可以填写自己的邮箱
|
if err != nil {
|
||||||
e.From = SEND_MAIL
|
log.Fatalf("Failed to send email: %v", err)
|
||||||
|
|
||||||
// 设置 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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Println("send successfully ... ")
|
log.Println("send successfully ... ")
|
||||||
|
|||||||
Reference in New Issue
Block a user