go QQ mail client

This commit is contained in:
2022-10-12 17:01:15 +08:00
commit 68196e0ff8
4 changed files with 102 additions and 0 deletions

7
go.mod Normal file
View File

@@ -0,0 +1,7 @@
module main
go 1.19
require github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible
require gopkg.in/ini.v1 v1.67.0 // indirect

4
go.sum Normal file
View File

@@ -0,0 +1,4 @@
github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible h1:jdpOPRN1zP63Td1hDQbZW73xKmzDvZHzVdNYxhnTMDA=
github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible/go.mod h1:1c7szIrayyPPB/987hsnvNzLushdWf4o/79s3P08L8A=
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=

4
gomail.ini Normal file
View File

@@ -0,0 +1,4 @@
[global]
SEND_QQ = "1605227279@qq.com"; // 发送者QQ
MAIL_KEY = "yojgexjxmnktiife"; // 发送者QQ密钥
SMTP_SERVER = "smtp.qq.com:25"; // smtp服务器地址

87
main.go Normal file
View File

@@ -0,0 +1,87 @@
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)
}
//设置服务器相关的配置
err := e.Send(SMTP_SERVER, smtp.PlainAuth("", SEND_QQ, MAIL_KEY, "smtp.qq.com"))
if err != nil {
log.Fatal(err)
}
log.Println("send successfully ... ")
}