email

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 9, 2024 License: Apache-2.0 Imports: 8 Imported by: 0

README

前言

使用Go发送邮件,目前官网GO 1.17的版本的文档中,包 "net/smtp" 仅支持支持两种SMTP的认证方式。CRAM-MD5和PLAIN 认证方式。但是目前smtp的认证方式有非常多包括:GSSAPI、NTLM、LOGIN、Kerberos、XOAUTH、XOAUTH2。以下为一些认证方式介绍:

  • CRAM-MD5: 是基于Keyed-MD5的认证方式
  • PLAIN: 是一种明文的验证方式
  • LOGIN: 邮箱登录的方式,使用邮箱的账号和密码

公司使用的邮件服务器SMTP支持三种认证方式:GSSAPI、NTLM、LOGIN。

认证方式

  1. 在原来的CRAM-MD5和PLAIN的基础上增加了LOGIN方式
  2. 通过helo接口自动化认证方式
if ok, authStr := c.Extension("AUTH"); ok {
    var a smtp.Auth
    if strings.Contains(authStr, "PLAIN") && authType == PlainAuthType {
        a = smtp.PlainAuth("", from, password, host)
    } else if strings.Contains(authStr, "LOGIN") && authType == LoginAuthType {
        a = NewLoginAuth(from, password)
    } else if strings.Contains(authStr, "") && authType == CramMd5AuthType {
        a = smtp.CRAMMD5Auth(from, password)
    } else {
        log.Errorf("dont support this authType: %s", authType)
        return fmt.Errorf("dont support this authType: %s", authType)
    }
    if err = c.Auth(a); err != nil {
        log.Error("check auth with err:", err)
        return err
    }
}

参数介绍

type Email struct {
    From      string   // 发件人邮箱,如:songjiayu02@baidu.com
    FromAlias string   // 发件人别名(选填),如:宋家宇
    Password  string   // 发件人邮箱密码
    AuthType  string   // smtp认证方式,默认PLAIN
    Addr      string   // 邮箱地址(一般端口为25),如:email.baidu.com:25
    To        []string // 收件人邮箱
    Subject   string   // 主题
    Body      string   // 内容
}

FromAlias

区别在于发件人的别名:如果设置如图1,不设置如图2

  • 图1
    图1

  • 图2
    图2

AuthType

仅支持constant.go文件里面的认证方式

示例

示例参考

Documentation

Index

Constants

View Source
const (
	PlainAuthType   = "PLAIN"
	LoginAuthType   = "LOGIN"
	CramMd5AuthType = "CRAM-MD5"
)

Variables

This section is empty.

Functions

func NewLoginAuth

func NewLoginAuth(username, password string) smtp.Auth

func Send

func Send(e *Email) error

func SendMail

func SendMail(addr, from, fromAlias, password, authType string, to []string, subject string, msg []byte) error

Types

type Email

type Email struct {
	From      string   // 发件人邮箱,如:songjiayu02@baidu.com
	FromAlias string   // 发件人别名(选填),如:宋家宇
	Password  string   // 发件人邮箱密码
	AuthType  string   // smtp认证方式,默认PLAIN
	Addr      string   // 邮箱地址(一般端口为25),如:email.baidu.com:25
	To        []string // 收件人邮箱
	Subject   string   // 主题
	Body      string   // 内容
}

type LoginAuth

type LoginAuth struct {
	// contains filtered or unexported fields
}

func (*LoginAuth) Next

func (a *LoginAuth) Next(fromServer []byte, more bool) ([]byte, error)

func (*LoginAuth) Start

func (a *LoginAuth) Start(server *smtp.ServerInfo) (string, []byte, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL