golang发送邮件

栏目: Go · 发布时间: 5年前

内容简介:SSL协议发送邮件的方法

SSL协议发送邮件的方法

package utils

import (
    "crypto/tls"
    "fmt"
    "log"
    "net/smtp"
    "strings"
)

const Email_User = "*******@163.com"
const Email_Password = "***********"
const Email_Host = "smtp.163.com"
const Email_Port = "465"
const Email_Name = "测试"
const Email_Subject = "测试验证码"

type Mail struct {
    senderId string
    toIds    []string
    subject  string
    body     string
}

type SmtpServer struct {
    host string
    port string
}

func EmailSendCode(nickname, to, code string) error {

    if nickname != "" {
        nickname = nickname + ","
    }

    body := `
        <html>
        <body>
        <h3>
        `+nickname+`您好:
        </h3>
        非常感谢您使用`+Email_Name+`,您的邮箱验证码为:<br/>
        <b>`+code+`</b><br/>
        此验证码有效期30分钟,请妥善保存。<br/>
        如果这不是您本人的操作,请忽略本邮件。<br/>
        </body>
        </html>
        `

    return SendToMail(to, Email_Subject, body)
}

func (s *SmtpServer) ServerName() string {
    return s.host + ":" + s.port
}

func (mail *Mail) BuildMessage() string {
    message := ""
    message += fmt.Sprintf("From: %s<%s>\r\n", Email_Name, mail.senderId)
    if len(mail.toIds) > 0 {
        message += fmt.Sprintf("To: %s\r\n", strings.Join(mail.toIds, ";"))
    }

    message += fmt.Sprintf("Subject: %s\r\n", mail.subject)

    message += "Content-Type: text/html; charset=UTF-8"

    message += "\r\n\r\n" + mail.body

    return message
}

func SendToMail(to, subject, body string) error {

    mail := Mail{}
    mail.senderId = Email_User
    mail.toIds = strings.Split(to, ";")
    mail.subject = Email_Subject
    mail.body = body

    messageBody := mail.BuildMessage()

    smtpServer := SmtpServer{host: Email_Host, port: Email_Port}

    //build an auth
    auth := smtp.PlainAuth("", mail.senderId, Email_Password, smtpServer.host)

    // Gmail will reject connection if it's not secure
    // TLS config
    tlsconfig := &tls.Config{
        InsecureSkipVerify: false,
        ServerName:         smtpServer.host,
    }

    conn, err := tls.Dial("tcp", smtpServer.ServerName(), tlsconfig)
    if err != nil {
        return err
    }

    client, err := smtp.NewClient(conn, smtpServer.host)
    if err != nil {
        return err
    }

    // step 1: Use Auth
    if err = client.Auth(auth); err != nil {
        return err
    }

    // step 2: add all from and to
    if err = client.Mail(mail.senderId); err != nil {
        return err
    }
    for _, k := range mail.toIds {
        if err = client.Rcpt(k); err != nil {
            return err
        }
    }

    // Data
    w, err := client.Data()
    if err != nil {
        return err
    }

    _, err = w.Write([]byte(messageBody))
    if err != nil {
        return err
    }

    err = w.Close()
    if err != nil {
        return err
    }

    client.Quit()

    log.Println("Mail sent successfully")

    return nil
}

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Thirty-three Miniatures

Thirty-three Miniatures

Jiří Matoušek / American Mathematical Socity / 2010-6-18 / USD 24.60

This volume contains a collection of clever mathematical applications of linear algebra, mainly in combinatorics, geometry, and algorithms. Each chapter covers a single main result with motivation and......一起来看看 《Thirty-three Miniatures》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

MD5 加密
MD5 加密

MD5 加密工具

html转js在线工具
html转js在线工具

html转js在线工具