smtp

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2025 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Part

func Part(contentType string, f Copier, encoding Encoding, settings []PartSetting) *part

Types

type Client added in v0.1.3

type Client = smtp.Client

type ConnectionMonitor

type ConnectionMonitor struct {
	Sender *SMTPSender
	// contains filtered or unexported fields
}

ConnectionMonitor 用于监控 *smtp.Client 连接状态,保持连接活跃.

func (*ConnectionMonitor) IsMonitoring

func (cm *ConnectionMonitor) IsMonitoring() bool

func (*ConnectionMonitor) MonitorConnection

func (cm *ConnectionMonitor) MonitorConnection(ctx context.Context, d time.Duration)

定期检查连接状态、重连. 可用于长时间处理邮件时保持连接.

type Copier

type Copier func(io.Writer) error

func NewCopier added in v0.1.3

func NewCopier(s string) Copier

type Encoding

type Encoding string
const (
	// QuotedPrintable represents the quoted-printable encoding as defined in
	// RFC 2045.
	QuotedPrintable Encoding = "quoted-printable"
	// Base64 represents the base64 encoding as defined in RFC 2045.
	Base64 Encoding = "base64"
	// Unencoded can be used to avoid encoding the body of an email. The headers
	// will still be encoded using quoted-printable encoding.
	Unencoded Encoding = "8bit"
)

type Extension

type Extension = string
const (
	SMTPExtAuth     Extension = "AUTH"
	SMTPExtStartTLS Extension = "STARTTLS"
)

type FileSetting

type FileSetting func(*file)

A FileSetting can be used as an argument in Message.Attach or Message.Embed.

func Rename

func Rename(name string) FileSetting

Rename is a file setting to set the name of the attachment if the name is different than the filename on disk.

func SetCopyFunc

func SetCopyFunc(f func(io.Writer) error) FileSetting

SetCopyFunc is a file setting to replace the function that runs when the message is sent. It should copy the content of the file to the io.Writer.

The default copy function opens the file with the given filename, and copy its content to the io.Writer.

func SetHeader

func SetHeader(h Header) FileSetting

SetHeader is a file setting to set the MIME header of the message part that contains the file content.

Mandatory headers are automatically added if they are not set when sending the email.

type Header = textproto.MIMEHeader // map[string][]string

type Message

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

func NewMessage

func NewMessage(settings ...MessageSetting) *Message

func (*Message) AddAlternative

func (m *Message) AddAlternative(contentType, body string, settings ...PartSetting)

AddAlternative adds an alternative part to the message.

It is commonly used to send HTML emails that default to the plain text version for backward compatibility. AddAlternative appends the new part to the end of the message. So the plain text part should be added before the HTML part. See http://en.wikipedia.org/wiki/MIME#Alternative

func (*Message) AddAlternativeWriter

func (m *Message) AddAlternativeWriter(contentType string, f func(io.Writer) error, settings ...PartSetting)

AddAlternativeWriter adds an alternative part to the message. It can be useful with the text/template or html/template packages.

func (*Message) Attach

func (m *Message) Attach(filename string, settings ...FileSetting) error

Attach attaches the files to the email.

func (*Message) Embed

func (m *Message) Embed(filename string, settings ...FileSetting) error

Embed embeds the images to the email.

func (*Message) FormatAddress

func (m *Message) FormatAddress(address, name string) string

FormatAddress formats an address and a name as a valid RFC 5322 address.

func (*Message) FormatDate

func (m *Message) FormatDate(t time.Time) string

func (*Message) GetHeader

func (m *Message) GetHeader(field string) []string

GetHeader gets a header field.

func (*Message) Reset

func (m *Message) Reset()

Reset resets the message so it can be reused. The message keeps its previous settings so it is in the same state that after a call to NewMessage.

func (*Message) SetBody

func (m *Message) SetBody(contentType, body string, settings ...PartSetting)

SetBody 设置邮件正文

func (*Message) SetDateHeader

func (m *Message) SetDateHeader(field string, date time.Time)

SetDateHeader sets a date to the given header field.

func (*Message) SetFrom

func (m *Message) SetFrom(from string, name string)

SetFrom 设置发件人

func (*Message) SetHeader

func (m *Message) SetHeader(key string, value ...string)

func (*Message) SetSubject

func (m *Message) SetSubject(subject string)

SetSubject 设置邮件主题

func (*Message) SetTo

func (m *Message) SetTo(to []string)

SetTo 设置收件人

func (*Message) WriteTo

func (m *Message) WriteTo(w io.Writer) (n int64, err error)

WriteTo 写入邮件到 io.Writer @return n int64 写入的字节数 @return err error 写入错误, nil 表示写入成功

type MessageSetting

type MessageSetting func(m *Message)

A MessageSetting can be used as an argument in NewMessage to configure an email.

func SetCharset

func SetCharset(charset string) MessageSetting

SetCharset is a message setting to set the charset of the email.

func SetEncoding

func SetEncoding(enc Encoding) MessageSetting

SetEncoding is a message setting to set the encoding of the email.

type MessageWriter

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

func (*MessageWriter) Write

func (w *MessageWriter) Write(p []byte) (n int, err error)

io.Writer 接口实现 写入数据到 io.Writer

type PartSetting

type PartSetting func(*part)

A PartSetting can be used as an argument in Message.SetBody, Message.AddAlternative or Message.AddAlternativeWriter to configure the part added to a message.

func SetPartEncoding

func SetPartEncoding(e Encoding) PartSetting

SetPartEncoding sets the encoding of the part added to the message. By default, parts use the same encoding than the message.

type SMTP

type SMTP struct {

	// ssl defines whether an SSL connection is used. It should be false in
	// most cases since the authentication mechanism should use the STARTTLS
	// extension instead.
	//
	// SSL 设置是否使用 SSL 连接,SSL 或 TLS 协议 (实际只使用TLS协议)
	//
	// 465 端口使用隐式TLS加密,意味着连接一开始就建立在安全通道上。
	// 与587端口(使用STARTTLS)不同,587是先建立普通连接再升级到TLS加密连接,
	// 但有些 SMTP 服务器 587端口 也是隐式TLS加密,
	// 这时候需要设置 SSL 为 true, 否则会连接失败。
	//
	// SSL true 使用 SSL/TLS 连接, false 尝试使用 STARTTLS extension(扩展).
	SSL bool
	// tlsConfig represents the TLS configuration used for the TLS (when the
	// STARTTLS extension is used) or SSL connection.
	TLSConfig *tls.Config
	// LocalName is the hostname sent to the SMTP server with the HELO command.
	// By default, "localhost" is sent.
	LocalName string // 本地主机名
	// contains filtered or unexported fields
}

func NewSMTP

func NewSMTP(host string, port int, username string, password string, from string) *SMTP

NewSMTP 创建一个新的 SMTP 客户端

Args

  • host: SMTP 服务器主机名
  • port: SMTP 服务器端口号
  • username: 登录 SMTP 服务器的用户名, 通常是邮箱地址
  • password: 登录 SMTP 服务器的密码
  • from: 发件人 email, 通常与 username 相同

Returns

  • {*SMTP} SMTP对象指针

func (*SMTP) Dial

func (s *SMTP) Dial() (*SMTPSender, error)

Dial 连接SMTP服务器

Returns

{*SMTPSender} 发送邮件的结构体指针
{error} nil 表示成功

func (*SMTP) DialAndSend

func (s *SMTP) DialAndSend(whereFrom bool, msgs ...*Message) error

DialAndSend 发送邮件,可以一次发送多封邮件。

Args

  • whereFrom 是否从消息中获取发件人, true 使用配置中的发件人, false 从消息中获取发件人
  • msgs 邮件内容,*Message 列表

func (*SMTP) DialAndSend1

func (s *SMTP) DialAndSend1(to []string, msg []byte) error

DialAndSend1 发送邮件,可以群发

Args

to {[]string} 收件人列表
msg {[]byte} 邮件内容

func (*SMTP) DialAndSend2

func (s *SMTP) DialAndSend2(from string, to []string, msg []byte) error

DialAndSend2 发送邮件,可以群发

Args

  • from 发件人email
  • to 收件人列表
  • msg 邮件内容

type SMTPAuthType

type SMTPAuthType = string
const (
	SMTPAuthCramMD5 SMTPAuthType = "CRAM-MD5"
	SMTPAuthXOAuth2 SMTPAuthType = "XOAUTH2"
	SMTPAuthLogin   SMTPAuthType = "LOGIN"
	SMTPAuthPlain   SMTPAuthType = "PLAIN"
)

type SMTPConfig

type SMTPConfig struct {
	Host     string `json:"smtp_host"`     // SMTP 服务器主机名
	Port     int    `json:"smtp_port"`     // SMTP 服务器端口号
	Username string `json:"smtp_username"` // SMTP 服务器用户名
	Password string `json:"smtp_password"` // SMTP 服务器密码
	From     string `json:"from"`          // 发件人邮箱地址
}

type SMTPSender

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

SMTPSender 一个邮件发送客户端。

func NewSMTPSender

func NewSMTPSender(smtp *SMTP) *SMTPSender

func NewSMTPSender1

func NewSMTPSender1(client *Client, smtp *SMTP) *SMTPSender

func (*SMTPSender) Client

func (s *SMTPSender) Client() *smtp.Client

func (*SMTPSender) Dial

func (s *SMTPSender) Dial() error

Dial 连接SMTP server。未连接 或 连接断开,才会重新连接SMTP服务器。

func (*SMTPSender) IsConnected

func (s *SMTPSender) IsConnected() bool

func (SMTPSender) Noop

func (s SMTPSender) Noop() error

Noop 发送 NOOP 命令(No Operation, 无操作命令),用于测试连接是否正常。

Return

  • {error} 错误信息。nil 表示 NOOP 命令成功,连接正常。

func (*SMTPSender) Quit

func (s *SMTPSender) Quit() error

Quit 关闭连接

func (*SMTPSender) Send

func (s *SMTPSender) Send(whereFrom bool, msgs ...*Message) error

Send 发送邮件

Args

  • whereFrom: 是否从消息中获取发件人, true 使用 smtp字段 中的发件人(smtp.from), false 从消息中获取发件人。
  • msgs 邮件内容,*Message 列表。

func (*SMTPSender) Send1

func (s *SMTPSender) Send1(to []string, msg []byte) error

Send1 发送邮件,使用 smtp.from 作为发件人

Args

  • to: 收件人列表/切片
  • msg {[]byte}: 邮件内容

func (*SMTPSender) Send2

func (s *SMTPSender) Send2(from string, to []string, msg []byte) error

Send2 发送邮件,指定发件人邮箱

Args

  • from: 发件人
  • to: 收件人列表
  • msg {[]byte}: 邮件内容

func (*SMTPSender) SendEmail

func (s *SMTPSender) SendEmail(from string, to []string, msg io.WriterTo) error

SendEmail 发送邮件,可以将 msg 发送给多个收件人(群发)。

Args

  • from {string} 发件人邮箱
  • to {[]string} 收件人邮箱切片
  • msg {io.WriterTo} 邮件内容,需实现 io.WriterTo 接口

type SMTPSenderPool

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

SMTPSenderPool 是一个 SMTP 发送池。 它维护一个连接池, 每个连接都是一个 Sender 实例。 每个连接都有一个 SMTP 客户端, 用于发送邮件。

func NewSMTPSenderPool

func NewSMTPSenderPool(poolSize int, config *SMTPConfig) *SMTPSenderPool

func (*SMTPSenderPool) Close

func (p *SMTPSenderPool) Close()

Close 关闭连接池中的所有连接

func (*SMTPSenderPool) Get

func (p *SMTPSenderPool) Get() (*SMTPSender, error)

func (*SMTPSenderPool) Put

func (p *SMTPSenderPool) Put(sender *SMTPSender)