import (
...
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
ses "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ses/v20201002"
...
)
func Send(ctx context.Context, toEmail, code string) error {
envPro := common.NewEnvProvider("tencent_sec_id", "tencent_sec_key")
cred, err := envPro.GetCredential()
if err != nil {
logger.L.Error("init tencent credential failed", zap.Error(err))
return err
}
prof := profile.NewClientProfile()
//region list: https://www.tencentcloud.com/document/product/213/6091
client, err := ses.NewClient(cred, "ap-hongkong", prof)
if err != nil {
logger.L.Error("init tencent ses client failed", zap.Error(err))
return err
}
var (
tmpId = uint64(84376)
tmpData = fmt.Sprintf("{\"code\":\"%s\"}", code)
from = "service@domain.com"
subject = "邮箱验证码"
unsubscribe = "0"
)
req := ses.NewSendEmailRequest()
req.Template = &ses.Template{
TemplateID: &tmpId,
TemplateData: &tmpData,
}
req.Destination = append(req.Destination, &toEmail)
req.FromEmailAddress = &from
req.Subject = &subject
req.Unsubscribe = &unsubscribe
rsp, err := client.SendEmailWithContext(ctx, req)
if err != nil {
logger.L.Error("tencent ses send email failed", zap.Error(err))
return err
}
logger.L.Info("email code sent out to smtp", zap.String("rsp", rsp.ToJsonString()))
return nil
}
评论