golang web Send Email

//注意邮件格式,以免被当垃圾邮件

package email


import (
    "net/smtp"
    "strings"
)

const (
    HOST        = "smtp.163.com"
    SERVER_ADDR = "smtp.163.com:25"
    USER        = "xxx@163.com" //发送邮件的邮箱
    PASSWORD    = "xxx"         //发送邮件邮箱的客户端授权密码
)

type Email struct {
    to      string "to"
    subject string "subject"
    msg     string "msg"
}

func NewEmail(to, subject, msg string) *Email {
    return &Email{to: to, subject: subject, msg: msg}
}

func SendEmail(email *Email) error {
    auth := smtp.PlainAuth("", USER, PASSWORD, HOST)
    sendTo := strings.Split(email.to, ";")
    done := make(chan error, 1024)

    go func() {
        defer close(done)
        for _, v := range sendTo {

            str := strings.Replace("From: "+USER+"~To: "+v+"~Subject: "+email.subject+"~~", "~", "\r\n", -1) + email.msg

            err := smtp.SendMail(
                SERVER_ADDR,
                auth,
                USER,
                []string{v},
                []byte(str),
            )
            done <- err
        }
    }()

    for i := 0; i < len(sendTo); i++ {
        <-done
    }

    return nil
}

testCode

func sendMail()  {
    fmt.Println("发送邮件")
    mycontent := "请查收验证码:12322"
    mail := email.NewEmail("xxx@163.com","案发现场-注册邮件验证",mycontent)
    err := email.SendEmail(mail)
    fmt.Println(err)
}