[Go] golang调用第三方smtp发送邮件

2020-05-29 17:26:02 浏览数 (2)

发邮件功能可以调用比如: qq 163 新浪的邮箱进行发送 , 使用go-smtp这个类库来发送比较简单 , 例如下面的例子:

代码语言:javascript复制
package tools

import (
    "github.com/emersion/go-sasl"
    "github.com/emersion/go-smtp"
    "strings"
)

func Send(server string,from string,password string,to []string,subject string,body string)error{
    auth := sasl.NewPlainClient("", from, password)
    msg := strings.NewReader(
        "From: " from "rn" 
            "To: " strings.Join(to,",") "rn" 
        "Subject: " subject "rn"  
        "rn"  
        body "rn")
    err := smtp.SendMail(server, auth, from, to, msg)
    if err != nil {
        return err
    }
    return nil
}

0 人点赞