mail

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2023 License: MIT Imports: 24 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MaxLineLength      = 76                             // MaxLineLength is the maximum line length pre RFC 2045
	DefaultContentType = "text/plain; charset=us-ascii" // email.ContentType is email default Content-Type according to RFC 2045, section 5.2

	StrFileNameParam = "filename" // mime request file name param

	CONTENT_TYPE              = "Content-Type"
	CONTENT_TRANSFER_ENCODING = "Content-Transfer-Encoding"
	BASE_64                   = "base64"
	QUOTED_PRINTABLE          = "quoted-printable"
	MULTIPART                 = "multipart/"
	BOUNDARY                  = "boundary"
	StrContentDisposition     = "Content-Disposition"
	StrReplyTo                = "Reply-To"
	StrSubject                = "Subject"
	StrTo                     = "To"
	StrCc                     = "Cc"
	StrBcc                    = "Bcc"
	StrFrom                   = "From"
	StrDate                   = "Date"
	StrMessageId              = "Message-Id"
	StrMimeVersion            = "MIME-Version"
)

Variables

View Source
var (
	// ErrMissingBoundary is returned when there is no boundary given for multipart entity
	ErrMissingBoundary = errors.New("no boundary found for multipart entity")

	// ErrMissingContentType is returned when there is no "Content-Type" header for a MIME entity
	ErrMissingContentType = errors.New("no Content-Type found for MIME entity")

	// ErrMustSpecifyMessage is returned when there is email param exist empty
	ErrMustSpecifyMessage = errors.New("must specify at least one from address and one to address")
)
View Source
var (
	ErrClosed  = errors.New("pool closed")
	ErrTimeout = errors.New("timed out")
)

Functions

func TpReader

func TpReader(r io.Reader) *textproto.Reader

TpReader textproto reader io

Types

type Attachment

type Attachment struct {
	FileName    string               // fileName
	ContentType string               // content type
	Header      textproto.MIMEHeader // header
	Content     []byte               // content
	HTMLRelated bool
}

Attachment is email attachement param type Based on the mime/multipart.FileHeader struct Attachment contains the name, MIMEHeader, and content of the attachment in question

type Email

type Email struct {
	ReplyTo     []string             // reply to
	From        string               //from
	To          []string             // to
	Bcc         []string             // bcc
	Cc          []string             // cc
	Subject     string               // subject
	Text        []byte               // text plaintext message (optional)
	Html        []byte               // html message (optional)
	Sender      string               // override from as SMTP envelope sender (optional)
	Headers     textproto.MIMEHeader // headers
	Attachments []*Attachment        // attachments
	ReadReceipt []string             // read receipt

}

Email used for email message type

func New

func New() *Email

New create an Email, and returns the pointer to it.

func NewEmailFromReader

func NewEmailFromReader(r io.Reader) (*Email, error)

NewEmailFromReader reads a stream of bytes from an io.Reader, r, and returns an email struct containing the parsed data. This function expects the data in RFC 5322 format.

func (*Email) Attach

func (e *Email) Attach(r io.Reader, fileName string, contentType string) (a *Attachment, err error)

Attach is used to attach content from an io.Reader to the email. Required params include an io.Reader, the desired fileName for the attachment, and the Content-Type the func will return the create Attachment for reference, as well as nil for the error, if successful.

func (*Email) AttachFile

func (e *Email) AttachFile(fileName string) (*Attachment, error)

AttachFile is used to attach content to the email. it attempts to open the file referenced by fileName and, if successful, creates an attachment. this attachment is then appended to the slice of e.Attachments. the func will then return the Attachment for reference, as well as nil for the error, if successful.

func (*Email) AttachWithHeaders

func (e *Email) AttachWithHeaders(r io.Reader, fileName string, contentType string, headers textproto.MIMEHeader) (a *Attachment, err error)

AttachWithHeaders is used to attach content from an io.Reader to the email. Required parameters include an io.Reader, the desired filename for the attachment, the Content-Type and the original MIME headers. The function will return the created Attachment for reference, as well as nil for the error, if successful.

func (*Email) Bytes

func (e *Email) Bytes() ([]byte, error)

Bytes converts the Email object to []byte including all needed MIMEHeaders, boundaries, etc.

func (*Email) Send

func (e *Email) Send(addr string, a smtp.Auth) error

Send an email using the given host and SMTP auth (optional), returns any error thrown by smtp.SendMail This function merges the To, Cc, and Bcc fields and calls the smtp.SendMail function using the Email.Bytes() output as the message

func (*Email) SendWithStartTLS

func (e *Email) SendWithStartTLS(addr string, auth smtp.Auth, tlsConfig *tls.Config) error

SendWithStartTLS sends an email over TLS using STARTTLS with an optional TLS config.

The TLS Config is helpful if you need to connect to a host that is used an untrusted certificate.

func (*Email) SendWithTLS

func (e *Email) SendWithTLS(addr string, auth smtp.Auth, tlsConfig *tls.Config) error

SendWithTLS sends an email with an optional TLS config. This is helpful if you need to connect to a host that is used an untrusted certificate.

type Pool

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

func NewPool

func NewPool(address string, count int, auth smtp.Auth, optTlsConfig ...*tls.Config) (pool *Pool, err error)

func (*Pool) Close

func (p *Pool) Close()

Close immediately changes the pool's state so no new connections will be created, then gets and closes the existing ones as they become available.

func (*Pool) Send

func (p *Pool) Send(e *Email, timeout time.Duration) (err error)

Send sends an email via a connection pulled from the Pool. The timeout may be <0 to indicate no timeout. Otherwise reaching the timeout will produce and error building a connection that occurred while we were waiting, or otherwise ErrTimeout.

func (*Pool) SetHelloHostname

func (p *Pool) SetHelloHostname(h string)

SetHelloHostname optionally sets the hostname that the Go smtp.Client will use when doing a HELLO with the upstream SMTP server. By default, Go uses "localhost" which may not be accepted by certain SMTP servers that demand an FQDN.

Jump to

Keyboard shortcuts

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