写点什么

通过腾讯云 SES 服务发送邮件

作者:排骨虾
  • 2023-06-29
    日本
  • 本文字数:978 字

    阅读完需:约 3 分钟

首先,我们需要配置好 SES 服务发送相关的域名、发送地址和模板等,请参考官方邮件配置指南


官方文档给出的 Go 调用示例已经过时,请参考下面的代码:

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}
复制代码

看到下面的信息说明发送成功了

{"level":"info","ts":1688022179.4352791,"caller":"email/email.go:119","msg":"email code sent out to smtp","rsp":"{\"Response\":{\"MessageId\":\"qcloudses-30-1257785076-date-20230629150259-bRCGAWXdjFYt1\",\"RequestId\":\"3eeb899b-e61f-47a4-a1d6-b147f6733eca\"}}"}


腾讯云访问 Key 是从环境变量中读取,比如我们可以在命令行 export


发布于: 刚刚阅读数: 3
用户头像

排骨虾

关注

还未添加个人签名 2007-04-01 加入

还未添加个人简介

评论

发布
暂无评论
通过腾讯云SES服务发送邮件_腾讯云_排骨虾_InfoQ写作社区