beego傳送郵件和上傳檔案介面
阿新 • • 發佈:2018-12-19
本文主要介紹採用beego框架的郵件傳送和檔案上傳介面
傳送郵件用到"gopkg.in/gomail.v2"包
在models中定義好請求引數模型和返回結果模型
type SmtpRecv struct { From string `json:"from"` To []string `json:"to"` Cc []string `json:"cc"` Pwd string `json:"pwd"` Attachments []string `json:"attachments"` Subject string `json:"subject"` Body string `json:"body"` }
返回結果模型如下,其中code 含義 1 成功 0 失敗
type GeneralResp struct {
Code int `json:"code"`
Data interface{} `json:"data"`
Error string `json:"error"`
}
beego框架API介面
type SendController struct { beego.Controller } func (this *SendController) SendMessage() { var smtpRecv models.SmtpRecv err := json.Unmarshal(this.Ctx.Input.RequestBody, &smtpRecv) if err != nil { log4go.Error(err.Error(), "smtpRecv RequestBody error") this.Data["json"] = models.GeneralResp{Code: 0, Error: err.Error()} this.ServeJSON() return } // send email err = smtpPush(smtpRecv) if err != nil { this.Data["json"] = models.GeneralResp{Code: 0, Error: err.Error()} } else { if len(smtpRecv.Attachments) != 0 { tools.RemoveFile(smtpRecv.Attachments) } this.Data["json"] = models.GeneralResp{Code: 1, Error: "send email success!"} } this.ServeJSON() } func smtpPush(smtpRecv models.SmtpRecv) error { m := gomail.NewMessage() m.SetHeaders(map[string][]string{ "From": {smtpRecv.From}, "To": smtpRecv.To, "Cc": smtpRecv.Cc, "Subject": {smtpRecv.Subject}, }) //m.Attach() m.SetBody("text/html", smtpRecv.Body) if len(smtpRecv.Attachments) != 0 { for k, _ := range smtpRecv.Attachments { m.Attach(smtpRecv.Attachments[k]) } } port, err := beego.AppConfig.Int("sendmsgport") if err != nil { log4go.Error(err.Error()) return err } d := gomail.NewDialer(Host: "smtp.qq.com", Port: 465, Username: "
[email protected]", Password: "***********",SSL:true) // Send the email if err := d.DialAndSend(m); err != nil { log4go.Error(err.Error()) return err } return nil } type Sizer interface { Size() int64 } func (this *SendController) UploadFiles() { f, h, err := this.GetFile("file") //獲取上傳的檔案this.GetFile("file") if err != nil { this.Data["json"] = models.GeneralResp{Code: 0, Error: "get file fail!"} this.ServeJSON() return } defer f.Close() //關閉上傳的檔案,不然的話會出現臨時檔案不能清除的情況 ext := path.Ext(h.Filename) //驗證字尾名是否符合要求 var AllowExtMap map[string]bool = map[string]bool{ ".jpg": true, ".jpeg": true, ".png": true, ".gif": true, ".csv": true, ".docx": true, ".xlsx": true, ".xls": true, ".doc": true, ".pdf": true, ".txt": true, ".html": true, ".ppt": true, ".pptx": true, } var Filebytes = 1 << 24 //檔案小於16兆 if _, ok := AllowExtMap[ext]; !ok { this.Data["json"] = models.GeneralResp{Code: 0, Error: "not allowed file format!"} this.ServeJSON() return } if fileSizer, ok := f.(Sizer); ok { fileSize := fileSizer.Size() if fileSize > int64(Filebytes) { this.Data["json"] = models.GeneralResp{Code: 0, Error: "upload file error: file size exceeds 16M!"} this.ServeJSON() } else { uploadDir := "./upload/" + time.Now().Format("2006/01/02/") err = os.MkdirAll(uploadDir, os.ModePerm) if err != nil { this.Data["json"] = models.GeneralResp{Code: 0, Error: "create upload dir fail:" + err.Error()} this.ServeJSON() return } fpath := uploadDir + h.Filename err = this.SaveToFile("file", fpath) if err != nil { this.Data["json"] = models.GeneralResp{Code: 0, Error: err.Error()} this.ServeJSON() //this.Ctx.WriteString(fmt.Sprintf("%v", err)) } this.Data["json"] = models.GeneralResp{Code: 1, Data: fpath} this.ServeJSON() } } else { this.Data["json"] = models.GeneralResp{Code: 0, Error: "unable to read file size!"} this.ServeJSON() } }
如有不對歡迎指正,相互學習,共同進步。