You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
86 lines
2.7 KiB
86 lines
2.7 KiB
|
1 year ago
|
package values
|
||
|
|
|
||
|
|
import (
|
||
|
|
"bytes"
|
||
|
|
"encoding/json"
|
||
|
|
"fmt"
|
||
|
|
"io/ioutil"
|
||
|
|
"net/http"
|
||
|
|
"server/config"
|
||
|
|
"server/util"
|
||
|
|
"strconv"
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"github.com/liangdas/mqant/log"
|
||
|
|
)
|
||
|
|
|
||
|
|
// =====================================不卡email
|
||
|
|
type BukaEmailCodeReq struct {
|
||
|
|
AppID string `json:"appId"` // appId
|
||
|
|
FromEmailAddress string `json:"fromEmailAddress"` // 发送的源邮箱
|
||
|
|
ToAddress string `json:"toAddress"` // 发送的目标邮箱
|
||
|
|
TemplateID string `json:"templateID"` // 模板id
|
||
|
|
TemplateData string `json:"templateData"` // 模板数据
|
||
|
|
Language string `json:"language"` // 语言
|
||
|
|
}
|
||
|
|
|
||
|
|
type BukaEmailCodeResp struct {
|
||
|
|
Status string `json:"status"` // 0成功
|
||
|
|
Reason string `json:"reason"` // 失败原因
|
||
|
|
Success string `json:"success"` // 成功数
|
||
|
|
Fail string `json:"fail"` // 失败数
|
||
|
|
EmailState string `json:"emailState"` // 提交状态:失败、成功
|
||
|
|
Remark string `json:"remark"` // 成功/失败描述
|
||
|
|
EmailId string `json:"emailId"` // 提交邮件到平台emailId
|
||
|
|
ToAddress string `json:"toAddress"` // 提交收件人地址(邮箱地址)
|
||
|
|
}
|
||
|
|
|
||
|
|
func BukaMailRequest(toAddr, code, lan string) error {
|
||
|
|
req := BukaEmailCodeReq{
|
||
|
|
AppID: config.GetConfig().Web.Mail.BukaEmailAppID,
|
||
|
|
FromEmailAddress: config.GetConfig().Web.Mail.BukaEmailAddr,
|
||
|
|
ToAddress: toAddr,
|
||
|
|
TemplateID: config.GetConfig().Web.Mail.BukaTemplateID,
|
||
|
|
Language: lan,
|
||
|
|
}
|
||
|
|
tmp := map[string]interface{}{"code": code}
|
||
|
|
templateData, _ := json.Marshal(tmp)
|
||
|
|
req.TemplateData = string(templateData)
|
||
|
|
|
||
|
|
url := config.GetConfig().Web.Mail.BukaEmailUrl
|
||
|
|
reqStr, _ := json.Marshal(req)
|
||
|
|
log.Debug("Post to:%v,req:%v", url, string(reqStr))
|
||
|
|
request, err := http.NewRequest("POST", url, bytes.NewBuffer(reqStr))
|
||
|
|
if err != nil {
|
||
|
|
log.Error("err:%v", err)
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
now := strconv.FormatInt(time.Now().Unix(), 10)
|
||
|
|
request.Header.Set("Content-Type", "application/json;charset=UTF-8")
|
||
|
|
request.Header.Set("Api-Key", config.GetConfig().Web.Mail.BukaAPIKey)
|
||
|
|
request.Header.Set("Timestamp", now)
|
||
|
|
request.Header.Set("Sign", util.CalculateMD5(config.GetConfig().Web.Mail.BukaAPIKey+config.GetConfig().Web.Mail.BukaAPISecret+now))
|
||
|
|
|
||
|
|
client := &http.Client{
|
||
|
|
Timeout: 5 * time.Second,
|
||
|
|
}
|
||
|
|
log.Debug("request:%+v", request)
|
||
|
|
resp, err := client.Do(request)
|
||
|
|
if err != nil {
|
||
|
|
log.Error("http post call err:%v", err)
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
defer resp.Body.Close()
|
||
|
|
body, _ := ioutil.ReadAll(resp.Body)
|
||
|
|
log.Debug("response Body%v:", string(body))
|
||
|
|
ret := BukaEmailCodeResp{}
|
||
|
|
if err := json.Unmarshal(body, &ret); err != nil {
|
||
|
|
log.Error("unmarshal fail err:%v", err)
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
if ret.Status != "0" {
|
||
|
|
return fmt.Errorf("%v", ret.Reason)
|
||
|
|
}
|
||
|
|
return nil
|
||
|
|
}
|