解决公网邮箱发送失败问题

上一步中,我没有使用Mailhog 搭建自己的本地邮件服务器,而是直接使用腾讯企业邮箱服务器来发送邮件的,但是一直发送失败,后来发现是因为tls问题,从github.com/jordan-wright/email这个包中的issue中看到了解决方法。

就是将Send方法替换成SendWithTls方法:

// Send 实现 send 方法
func (s *SMTP) Send(email Email, config map[string]string) bool {
    e := emailPKG.NewEmail()
    e.From = fmt.Sprintf("%v <%v>", email.From.Name, email.From.Address)
    e.Bcc = email.Bcc
    e.Cc = email.Cc
    e.To = email.To
    e.Subject = email.Subject
    e.Text = email.Text
    e.HTML = email.HTML

    logger.DebugJSON("send email", "发送详情", e)

    err := e.SendWithTLS(
        fmt.Sprintf("%v:%v", config["host"], config["port"]),

        smtp.PlainAuth(
            "",
            config["username"],
            config["password"],
            config["host"],
        ),
        &tls.Config{
            ServerName: config["host"],
        },
    )

    if err != nil {
        logger.ErrorString("send email", "发送失败", err.Error())
        return false
    }

    logger.DebugString("send email", "发送成功", "")
    return true
}
本帖已被设为精华帖!
本帖由系统于 1年前 自动加精
讨论数量: 1

Veify写错了,是Verify

2年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!