import (
    "fmt"
    "net/smtp"
)

const (
    SMTP_MAIL_HOST  = "smtp.163.com"
    SMTP_MAIL_PORT  = "25"
    SMTP_MAIL_USER  = "me@163.com"
    SMTP_MAIL_PWD   = "123456"
    SMTP_MAIL_NICKNAME  = "testmail"
)

func SendMail(address []string, subject string, body string) (err error) {
    auth := smtp.PlainAuth("", SMTP_MAIL_USER, SMTP_MAIL_PWD, SMTP_MAIL_HOST)
    contentType := "Content-Type: text/html; charset=UTF-8"
    for _, v := range address {
        s := fmt.Sprintf("To:%s\r\nFrom:%s<%s>\r\nSubject:%s\r\n%s\r\n\r\n%s",
            v, SMTP_MAIL_NICKNAME, SMTP_MAIL_USER, subject, contentType, body)
        msg := []byte(s)
        addr := fmt.Sprintf("%s:%s", SMTP_MAIL_HOST, SMTP_MAIL_PORT)
        err = smtp.SendMail(addr, auth, SMTP_MAIL_USER, []string{v}, msg)
        if err != nil {
            return err
        }
    }
    return
}

发表回复

您的电子邮箱地址不会被公开。

Captcha Code