Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 4ab9901cfb | |||
| f251b56675 | |||
| d2f9e5c1c1 | |||
| 074b4bc24c | |||
| 8d5a2cdf20 | |||
| 0fba9464f9 | |||
| a75f11da62 |
25
build.sh
Normal file
25
build.sh
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
:
|
||||||
|
|
||||||
|
_build()
|
||||||
|
{
|
||||||
|
ARCH="amd64"
|
||||||
|
|
||||||
|
for a in $ARCH
|
||||||
|
do
|
||||||
|
|
||||||
|
CGO_ENABLED=0 GOOS=linux GOARCH=$a go build -ldflags '-w -s' -o bin/$a src/main.go && upx -9 bin/$a
|
||||||
|
done
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
_install()
|
||||||
|
{
|
||||||
|
ARCH=`go env | grep GOARCH | cut -d \" -f 2`
|
||||||
|
cp bin/${ARCH} /bin/gomail
|
||||||
|
chmod +x /bin/gomail
|
||||||
|
cp -f etc/gomail.ini /etc/
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
test "$1" = "install" && _install && exit 0
|
||||||
|
_build
|
||||||
4
etc/gomail.ini
Normal file
4
etc/gomail.ini
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
[global]
|
||||||
|
SEND_MAIL = "yuling.niu@hohong.cn"; // 发送者邮箱
|
||||||
|
MAIL_KEY = "8MWeAUdg3ttvMjKv"; // 发送者邮箱密钥
|
||||||
|
SMTP_SERVER = "smtphz.qiye.163.com:994"; // smtp服务器地址
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
[global]
|
|
||||||
SEND_QQ = "1605227279@qq.com"; // 发送者QQ
|
|
||||||
MAIL_KEY = "yojgexjxmnktiife"; // 发送者QQ密钥
|
|
||||||
SMTP_SERVER = "smtp.qq.com:25"; // smtp服务器地址
|
|
||||||
91
main.go
91
main.go
@@ -1,91 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"flag"
|
|
||||||
"fmt"
|
|
||||||
"log"
|
|
||||||
"net/smtp"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"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])去除最后一个元素的路径
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
return strings.Replace(dir, "\\", "/", -1) //将\替换成/
|
|
||||||
}
|
|
||||||
|
|
||||||
func PathExists(path string) (bool, error) {
|
|
||||||
_, err := os.Stat(path)
|
|
||||||
if err == nil {
|
|
||||||
return true, nil
|
|
||||||
}
|
|
||||||
if os.IsNotExist(err) {
|
|
||||||
return false, nil
|
|
||||||
}
|
|
||||||
return false, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
// 判断配置文件是否存在
|
|
||||||
INIFILE := GetCurrentDirectory() + "/" + "gomail.ini"
|
|
||||||
b, _ := PathExists(INIFILE)
|
|
||||||
if !b {
|
|
||||||
INIFILE = "/etc/gomail.ini"
|
|
||||||
}
|
|
||||||
|
|
||||||
// 读取配置文件
|
|
||||||
cfg, inierr := ini.Load(INIFILE)
|
|
||||||
if inierr != nil {
|
|
||||||
fmt.Printf("Fail to read file: %v", inierr)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
SEND_QQ := cfg.Section("global").Key("SEND_QQ").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", "", "接收者QQ")
|
|
||||||
flag.Parse()
|
|
||||||
|
|
||||||
// 简单设置 log 参数
|
|
||||||
log.SetFlags(log.Lshortfile | log.LstdFlags)
|
|
||||||
|
|
||||||
e := email.NewEmail()
|
|
||||||
// 设置 sender 发送方 的邮箱 , 此处可以填写自己的邮箱
|
|
||||||
e.From = SEND_QQ
|
|
||||||
|
|
||||||
// 设置 receiver 接收方 的邮箱 此处也可以填写自己的邮箱, 就是自己发邮件给自己
|
|
||||||
e.To = []string{*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:]))]
|
|
||||||
|
|
||||||
//设置服务器相关的配置
|
|
||||||
err := e.Send(SMTP_SERVER, smtp.PlainAuth("", SEND_QQ, MAIL_KEY, SMTP_SERVER_Host))
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Println("send successfully ... ")
|
|
||||||
}
|
|
||||||
118
src/main.go
Normal file
118
src/main.go
Normal file
@@ -0,0 +1,118 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/tls"
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"net/smtp"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
gomail "github.com/jordan-wright/email"
|
||||||
|
"gopkg.in/ini.v1"
|
||||||
|
)
|
||||||
|
|
||||||
|
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 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) {
|
||||||
|
_, err := os.Stat(path)
|
||||||
|
if err == nil {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
configFile := getConfigFilePath()
|
||||||
|
inifile, _ := PathExists(configFile)
|
||||||
|
if !inifile {
|
||||||
|
configFile = "/etc/gomail.ini"
|
||||||
|
}
|
||||||
|
|
||||||
|
config := readConfig(configFile)
|
||||||
|
|
||||||
|
text := flag.String("t", "", "文本")
|
||||||
|
subject := flag.String("s", "", "主题")
|
||||||
|
attachment := flag.String("a", "", "添加附件")
|
||||||
|
recipients := flag.String("r", "", "接收者邮箱")
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
recipientsList := strings.Fields(*recipients)
|
||||||
|
fmt.Println("TO =", recipientsList)
|
||||||
|
|
||||||
|
email := createEmail(config.SenderEmail, recipientsList, *subject, *text, *attachment)
|
||||||
|
|
||||||
|
err := sendEmail(email, config.SMTPServer, config.SenderEmail, config.EmailPassword)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to send email: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Println("send successfully ... ")
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user