7 Commits

Author SHA1 Message Date
4ab9901cfb 优化 2024-01-18 16:00:55 +08:00
f251b56675 可支持多个收件人 2023-02-14 16:25:20 +08:00
d2f9e5c1c1 增加SSL模式登录 2022-12-02 16:05:01 +08:00
074b4bc24c 支持网易邮箱 2022-12-01 14:35:55 +08:00
8d5a2cdf20 更新 'build.sh' 2022-10-26 12:12:50 +08:00
0fba9464f9 优化安装脚本 2022-10-22 17:14:25 +08:00
a75f11da62 增加脚本安装 2022-10-21 21:15:47 +08:00
7 changed files with 148 additions and 96 deletions

BIN
bin/amd64 Normal file

Binary file not shown.

25
build.sh Normal file
View 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
View File

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

2
go.mod
View File

@@ -1,4 +1,4 @@
module main
module gomail
go 1.19

View File

@@ -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
View File

@@ -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
View 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 ... ")
}