You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
star/smtpclient/smtp.go

149 lines
4.1 KiB
Go

package smtpclient
import (
"b612.me/apps/b612/smtpclient/email"
"b612.me/stario"
"b612.me/starlog"
"crypto/tls"
"github.com/spf13/cobra"
"mime"
"net/smtp"
"os"
"path/filepath"
"strings"
)
var Cmd = &cobra.Command{
Use: "smtpc",
Short: "smtp client",
Long: "smtp client",
Run: func(cmd *cobra.Command, args []string) {
run()
},
}
var from, subject, text string
var to, cc, bcc, attachments, replyTo []string
var useHTML bool
var user, pwd, server string
var skipInsecure, usingFile bool
var useTLS int
var hostname string
var autoHostname bool
func init() {
Cmd.Flags().BoolVarP(&usingFile, "file", "F", false, "using file")
Cmd.Flags().StringSliceVarP(&replyTo, "reply-to", "r", nil, "reply to")
Cmd.Flags().StringVarP(&from, "from", "f", "", "from")
Cmd.Flags().StringVarP(&subject, "subject", "s", "", "subject")
Cmd.Flags().StringVarP(&text, "text", "t", "", "text")
Cmd.Flags().StringSliceVarP(&to, "to", "T", nil, "to")
Cmd.Flags().StringSliceVarP(&cc, "cc", "C", nil, "cc")
Cmd.Flags().StringSliceVarP(&bcc, "bcc", "B", nil, "bcc")
Cmd.Flags().StringSliceVarP(&attachments, "attachments", "a", nil, "attachments")
Cmd.Flags().BoolVarP(&useHTML, "html", "H", false, "use html")
Cmd.Flags().StringVarP(&user, "user", "u", "", "user")
Cmd.Flags().StringVarP(&pwd, "pwd", "p", "", "password")
Cmd.Flags().StringVarP(&server, "server", "S", "127.0.0.1:25", "server")
Cmd.Flags().IntVarP(&useTLS, "tls", "l", 0, "use tls,1 means use tls,2 means use starttls,other means not use tls")
Cmd.Flags().BoolVarP(&skipInsecure, "skip-insecure", "i", false, "skip insecure")
Cmd.Flags().StringVarP(&hostname, "hostname", "n", "", "hostname")
Cmd.Flags().BoolVarP(&autoHostname, "auto-hostname", "N", false, "auto hostname")
}
func run() {
{
for from == "" {
from = stario.MessageBox("Please input mail from:", "").MustString()
}
for len(to) == 0 {
to = stario.MessageBox("Please input mail to,split by ,:", "").MustSliceString(",")
}
for subject == "" {
subject = stario.MessageBox("Please input mail subject:", "").MustString()
}
for text == "" {
text = stario.MessageBox("Please input mail text:", "").MustString()
}
for server == "" {
server = stario.MessageBox("Please input mail server:", "").MustString()
}
}
var mail = email.Email{
ReplyTo: replyTo,
From: from,
To: to,
Bcc: bcc,
Cc: cc,
Subject: subject,
}
var txt []byte
var err error
if usingFile {
txt, err = os.ReadFile(text)
if err != nil {
starlog.Errorf("read file %s error:%s\n", text, err)
os.Exit(1)
}
} else {
txt = []byte(text)
}
if useHTML {
mail.HTML = txt
} else {
mail.Text = txt
}
if len(attachments) > 0 {
for _, v := range attachments {
if strings.TrimSpace(v) == "" {
continue
}
f, err := os.Open(v)
if err != nil {
starlog.Errorf("open attach file %s error:%s\n", v, err)
os.Exit(1)
}
stat, err := f.Stat()
if err != nil {
starlog.Errorf("stat attach file %s error:%s\n", v, err)
os.Exit(1)
}
_, err = mail.Attach(f, stat.Name(), mime.TypeByExtension(filepath.Ext(stat.Name())))
if err != nil {
starlog.Errorf("attach file %s error:%s\n", v, err)
os.Exit(1)
}
}
}
if autoHostname && hostname == "" {
hostname = strings.Split(server, ":")[0]
}
var auth smtp.Auth
if user != "" && pwd != "" {
auth = smtp.PlainAuth("", user, pwd, hostname)
}
switch useTLS {
case 1:
starlog.Noticef("Mail send method:TLS InsecureSkipVerify:%v ServerName:%v\n", skipInsecure, hostname)
err = mail.SendWithTLS(server, auth, &tls.Config{
InsecureSkipVerify: skipInsecure,
ServerName: hostname,
})
case 2:
starlog.Noticef("Mail send method:StartTLS InsecureSkipVerify:%v ServerName:%v\n", skipInsecure, hostname)
err = mail.SendWithStartTLS(server, auth, &tls.Config{InsecureSkipVerify: skipInsecure,
ServerName: hostname})
default:
starlog.Noticef("Mail send method:Normal\n")
err = mail.Send(server, auth)
}
if err != nil {
starlog.Errorf("send mail error:%s\n", err)
os.Exit(1)
}
starlog.Infof("send mail to %v success by server %s\n", to, server)
}