gomail

package
v1.1.7 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2022 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Send

func Send(s Sender, msg ...*Message) error

Send 使用发送器发送对象

Types

type AttachmentFile added in v1.0.6

type AttachmentFile struct {
	Name     string                  // 名称
	Header   map[string][]string     // 请求头
	CopyFunc func(w io.Writer) error // 复制函数
}

文件对象

type Dialer

type Dialer struct {
	Host      string      // SMTP服务地址
	Port      int         // SMTP端口号
	Username  string      // 用户名
	Password  string      // 密码
	Auth      smtp.Auth   // 权限
	SSL       bool        // 是否开启SSL
	TLSConfig *tls.Config // SSL配置
	LocalName string      // 本地服务名称,默认“localhost”
}

Dialer SMTP的邮件拨号器

func NewDialer

func NewDialer(host string, port int, username, password string) *Dialer

NewDialer 创建拨号器

func (*Dialer) Dial

func (d *Dialer) Dial() (sender SendCloser, err error)

Dial 使用拨号器进行拨号

func (*Dialer) DialAndSend

func (d *Dialer) DialAndSend(m ...*Message) error

DialAndSend opens a connection to the SMTP server, sends the given emails and closes the connection.

type Encoding

type Encoding string

Encoding represents a MIME encoding scheme like quoted-printable or base64.

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(*AttachmentFile)

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 map[string][]string) 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 Message

type Message struct {
	Fs      *embed.FS           // 嵌入文件系统
	Readers map[string]*os.File // 读取器列表
	// contains filtered or unexported fields
}

Message 准备邮件内容

func NewMessage

func NewMessage(settings ...MessageSetting) *Message

NewMessage 创建一条消息,默认使用UTF-8编码

func NewMessageWithFs added in v1.0.3

func NewMessageWithFs(fs *embed.FS, settings ...MessageSetting) *Message

NewMessageWithFs 使用嵌入文件系统创建消息

func NewMessageWithReaders added in v1.0.6

func NewMessageWithReaders(readers map[string]*os.File, settings ...MessageSetting) *Message

NewMessageWithReaders 使用读取器列表创建消息

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) AddAttachmentFile added in v1.0.6

func (m *Message) AddAttachmentFile(fileName string, settings []FileSetting)

AddAttachmentFile 添加附件

func (*Message) AddAttachmentFileObj added in v1.0.6

func (m *Message) AddAttachmentFileObj(fileName string, fileObj *os.File)

AddAttachmentFileObj 添加文件对象作为附件

func (*Message) Attach

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

Attach 添加邮件附件

func (*Message) AttachWithFiles added in v1.0.6

func (m *Message) AttachWithFiles(files map[string]*os.File, filename string, settings ...FileSetting)

AttachWithFiles 使用文件列表追加附件

func (*Message) AttachWithFs added in v1.0.3

func (m *Message) AttachWithFs(fs *embed.FS, filename string, settings ...FileSetting)

AttachWithFs 使用嵌入文件系统作为附件

func (*Message) AttachWithReaders added in v1.0.6

func (m *Message) AttachWithReaders(readers map[string]*os.File, filename string, settings ...FileSetting)

AttachWithReaders 使用读取器列表作为附件

func (*Message) Embed

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

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(date time.Time) string

FormatDate formats a date as a valid RFC 5322 date.

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) SetAddressHeader

func (m *Message) SetAddressHeader(field, address, name string)

SetAddressHeader sets an address to the given header field.

func (*Message) SetBody

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

SetBody sets the body of the message. It replaces any content previously set by SetBody, AddAlternative or AddAlternativeWriter.

func (*Message) SetDateHeader

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

SetDateHeader sets a date to the given header field.

func (*Message) SetHeader

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

SetHeader sets a value to the given header field.

func (*Message) SetHeaders

func (m *Message) SetHeaders(h map[string][]string)

SetHeaders sets the message headers.

func (*Message) WriteTo

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

WriteTo implements io.WriterTo. It dumps the whole message into w.

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 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 SendCloser

type SendCloser interface {
	Sender
	Close() error
}

SendCloser is the interface that groups the Send and Close methods.

type SendFunc

type SendFunc func(from string, to []string, msg io.WriterTo) error

A SendFunc is a function that sends emails to the given addresses.

The SendFunc type is an adapter to allow the use of ordinary functions as email senders. If f is a function with the appropriate signature, SendFunc(f) is a Sender object that calls f.

func (SendFunc) Send

func (f SendFunc) Send(from string, to []string, msg io.WriterTo) error

Send calls f(from, to, msg).

type Sender

type Sender interface {
	Send(from string, to []string, msg io.WriterTo) error
}

Sender is the interface that wraps the Send method.

Send sends an email to the given addresses.

Jump to

Keyboard shortcuts

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