package mail // Installation // go get github.com/jordan-wright/email // Note: Version > 1 of this library requires Go v1.5 or above. // If you need compatibility with previous Go versions, you can use the previous package at gopkg.in/jordan-wright/email.v1 // Sending email using Gmail // // e := email.NewEmail() // e.From = "Jordan Wright " // e.To = []string{"test@example.com"} // e.Bcc = []string{"test_bcc@example.com"} // e.Cc = []string{"test_cc@example.com"} // e.Subject = "Awesome Subject" // e.Text = []byte("Text Body is, of course, supported!") // e.HTML = []byte("

Fancy HTML is supported, too!

") // e.Send("smtp.gmail.com:587", smtp.PlainAuth("", "test@gmail.com", "password123", "smtp.gmail.com")) import ( "fmt" "net/smtp" emailPKG "github.com/jordan-wright/email" "github.com/runningwater/gohub/pkg/logger" ) // SMTP 实现 email.Driver 接口 // 使用 github.com/jordan-wright/email 库实现 SMTP 协议邮件发送 type SMTP struct{} // Send 发送邮件 // 参数 config 需包含以下配置项: // // host - SMTP 服务器地址 // port - SMTP 端口 // username - 认证用户名 // password - 认证密码 // // 返回 bool 表示邮件是否成功投递到服务器 func (s *SMTP) Send(email Email, config map[string]string) bool { // 实现发送邮件的逻辑 e := emailPKG.NewEmail() e.From = fmt.Sprintf("%s <%s>", email.From.Name, email.From.Address) e.To = email.To e.Bcc = email.Bcc e.Cc = email.Cc e.Subject = email.Subject e.Text = email.Text e.HTML = email.HTML logger.DebugJSON("发送邮件", "发送详情", e) if err := e.Send( fmt.Sprintf("%v:%v", config["host"], config["port"]), smtp.PlainAuth( "", config["username"], config["password"], config["host"], ), ); err != nil { logger.ErrorString("发送邮件", "发送邮件失败", err.Error()) return false } // 成功时记录简略日志,避免泄露敏感内容 logger.DebugString("发送邮件", "投递状态", "服务器已接收邮件") return true }