Documentation
¶
Index ¶
- func Part(contentType string, f Copier, encoding Encoding, settings []PartSetting) *part
- type Client
- type ConnectionMonitor
- type Copier
- type Encoding
- type Extension
- type FileSetting
- type Header
- type Message
- func (m *Message) AddAlternative(contentType, body string, settings ...PartSetting)
- func (m *Message) AddAlternativeWriter(contentType string, f func(io.Writer) error, settings ...PartSetting)
- func (m *Message) Attach(filename string, settings ...FileSetting) error
- func (m *Message) Embed(filename string, settings ...FileSetting) error
- func (m *Message) FormatAddress(address, name string) string
- func (m *Message) FormatDate(t time.Time) string
- func (m *Message) GetHeader(field string) []string
- func (m *Message) Reset()
- func (m *Message) SetBody(contentType, body string, settings ...PartSetting)
- func (m *Message) SetDateHeader(field string, date time.Time)
- func (m *Message) SetFrom(from string, name string)
- func (m *Message) SetHeader(key string, value ...string)
- func (m *Message) SetSubject(subject string)
- func (m *Message) SetTo(to []string)
- func (m *Message) WriteTo(w io.Writer) (n int64, err error)
- type MessageSetting
- type MessageWriter
- type PartSetting
- type SMTP
- type SMTPAuthType
- type SMTPConfig
- type SMTPSender
- func (s *SMTPSender) Client() *smtp.Client
- func (s *SMTPSender) Dial() error
- func (s *SMTPSender) IsConnected() bool
- func (s SMTPSender) Noop() error
- func (s *SMTPSender) Quit() error
- func (s *SMTPSender) Send(whereFrom bool, msgs ...*Message) error
- func (s *SMTPSender) Send1(to []string, msg []byte) error
- func (s *SMTPSender) Send2(from string, to []string, msg []byte) error
- func (s *SMTPSender) SendEmail(from string, to []string, msg io.WriterTo) error
- type SMTPSenderPool
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
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 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 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 ¶ added in v0.1.3
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 ¶
FormatAddress formats an address and a name as a valid RFC 5322 address.
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 ¶
SetDateHeader sets a date to the given header field.
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
}
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 ¶
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 ¶
DialAndSend 发送邮件,可以一次发送多封邮件。
Args ¶
- whereFrom 是否从消息中获取发件人, true 使用配置中的发件人, false 从消息中获取发件人
- msgs 邮件内容,*Message 列表
type SMTPAuthType ¶
type SMTPAuthType = string
const ( SMTPAuthCramMD5 SMTPAuthType = "CRAM-MD5" SMTPAuthXOAuth2 SMTPAuthType = "XOAUTH2" SMTPAuthLogin SMTPAuthType = "LOGIN" SMTPAuthPlain SMTPAuthType = "PLAIN" )
type SMTPConfig ¶
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) 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}: 邮件内容
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) Get ¶
func (p *SMTPSenderPool) Get() (*SMTPSender, error)
func (*SMTPSenderPool) Put ¶
func (p *SMTPSenderPool) Put(sender *SMTPSender)