mandala

package module
v0.0.0-...-8ba031b Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2019 License: MIT Imports: 20 Imported by: 0

README

mandala

Simple SMTP client

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsPermanent

func IsPermanent(err error) bool

IsPermanent returns true if the error is permanent, and false otherwise. If it can't tell, it returns false.

func JoinAddresses

func JoinAddresses(addrs []EmailAddress) string

JoinAddresses produces a concatenation of email addresses

func JoinFormattedAddresses

func JoinFormattedAddresses(addrs []EmailAddress, charset string) string

JoinFormattedAddresses produces a concatenation of formatted email addresses

func SendMail

func SendMail(addr string, a smtp.Auth, msg *Email) error

SendMail connects to the server at addr, switches to TLS if possible, authenticates with the optional mechanism a if possible, and then sends an email from address from, to addresses to, with message msg. The addr must include a port, as in "mail.example.com:smtp".

func Split

func Split(addr string) (string, string)

Split an user@domain address into user and domain.

func WriteEncodedBody

func WriteEncodedBody(p io.Writer, body []byte, encoding string) (err error)

WriteEncodedBody writes the body in encoded format.

Types

type Email

type Email struct {
	From        EmailAddress   `json:"from"`
	To          []EmailAddress `json:"to"`
	Cc          []EmailAddress `json:"cc"`
	Bcc         []EmailAddress `json:"bcc"`
	Subject     string         `json:"subject"`
	Headers     Headers        `json:"headers"` // Extended SMTP headers
	Text        string         `json:"text"`
	HTML        string         `json:"html"`
	AMP         string         `json:"amp"`
	Encoding    string         `json:"encoding"` // "quoted-printable", "base64", "8bit"
	CharSet     string         `json:"charset"`  // "UTF-8", "iso-8859-1", ...
	MessageID   string         `json:"message_id"`
	ReplyTo     EmailAddress   `json:"reply_to"`
	Recipient   string         `json:"recipient"`
	ReturnPath  string         `json:"returnpath"`
	Sender      string         `json:"sender"`
	Attachments []*Part        `json:"attachments"`
	Images      []*Part        `json:"images"`
	Sanitize    bool           `json:"sanitize"`
}

Email rapresents an email message.

func NewEmail

func NewEmail(from EmailAddress, to []EmailAddress, subject, html, text string) *Email

NewEmail creates a new email message using default settings.

func (*Email) AddAttachment

func (e *Email) AddAttachment(filename, contentType string, body []byte)

AddAttachment adds an attachment to the message.

func (*Email) AddEmbeddedImage

func (e *Email) AddEmbeddedImage(filename, contentType, contentID string, body []byte)

AddEmbeddedImage adds an embedded image to the message.

func (*Email) ContentType

func (e *Email) ContentType() string

ContentType detects the message content type.

func (*Email) IsMultiPart

func (e *Email) IsMultiPart() bool

IsMultiPart detects if the message is multipart.

func (*Email) LoadAttachment

func (e *Email) LoadAttachment(path string) error

LoadAttachment attachs a file to the message.

func (*Email) Write

func (e *Email) Write(w io.Writer) error

type EmailAddress

type EmailAddress struct {
	Name    string `json:"name"`
	Address string `json:"address"`
}

EmailAddress contains an email name and address.

func (*EmailAddress) FormatAddress

func (ad *EmailAddress) FormatAddress(charset string) string

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

func (*EmailAddress) String

func (ad *EmailAddress) String() string
type Header struct {
	Name    string
	Value   string
	Encoded bool
}

Header contains SMTP or MIME header.

type Headers

type Headers []*Header

Headers is a list of email headers.

func (Headers) Add

func (h Headers) Add(name string, value string, encoded bool) Headers

Add an header to the list

func (Headers) AddHeaders

func (h Headers) AddHeaders(headers Headers) Headers

AddHeaders adds a list of headers

func (Headers) GetHeader

func (h Headers) GetHeader(name string) *Header

GetHeader returns the first occurrance of the header with this name or nil if not found.

func (Headers) GetHeaders

func (h Headers) GetHeaders(name string) []*Header

GetHeaders returns all the occurrence of the header with this name.

func (Headers) Write

func (h Headers) Write(w io.Writer, charset string) error

Write the headers to the specified io.Writer following RFC 2047

type Part

type Part struct {
	Filename           string `json:"filename"`
	ContentType        string `json:"content_type"`
	ContentDisposition string `json:"content_disposition"` // "attachment"
	Encoding           string `json:"encoding"`            // "quoted-printable", "base64", "8bit"
	CharSet            string `json:"charset"`             // "utf-8", "iso-8859-1", ...
	ContentID          string `json:"content_id"`
	Body               []byte `json:"body"`
}

Part is used for including file and embedded image.

func (*Part) WriteMultipart

func (a *Part) WriteMultipart(w *multipart.Writer) error

WriteMultipart writes the attachment to the specified multipart writer.

type SendBulkReport

type SendBulkReport []SendBulkReportItem

SendBulkReport contains the list of the report items.

type SendBulkReportItem

type SendBulkReportItem struct {
	MessageID string
	Sent      bool
	Err       error
}

SendBulkReportItem represents the outcome of a single sending.

type Session

type Session struct {
	// Text is the textproto.Conn used by the Client. It is exported to allow for
	// clients to add extensions.
	Text *textproto.Conn
	// contains filtered or unexported fields
}

Session represents a client connection to an SMTP server.

func NewSession

func NewSession(host string, a smtp.Auth) (*Session, error)

NewSession returns a new client Session connected to an SMTP server at host. The host must include a port, as in "mail.example.com:smtp".

func NewSessionUsingConnection

func NewSessionUsingConnection(conn net.Conn, host string, auth smtp.Auth) (*Session, error)

NewSessionUsingConnection returns a new Session using an existing connection and host as a server name to be used when authenticating.

func (*Session) Auth

func (c *Session) Auth() error

Auth authenticates a client using the provided authentication mechanism. A failed authentication closes the connection. Only servers that advertise the AUTH extension support this function.

func (*Session) Close

func (c *Session) Close() error

Close closes the connection.

func (*Session) Data

func (c *Session) Data() (io.WriteCloser, error)

Data issues a DATA command to the server and returns a writer that can be used to write the mail headers and body. The caller should close the writer before calling any more methods on c. A call to Data must be preceded by one or more calls to Rcpt.

func (*Session) Extension

func (c *Session) Extension(ext string) (bool, string)

Extension reports whether an extension is support by the server. The extension name is case-insensitive. If the extension is supported, Extension also returns a string that contains any parameters the server specifies for the extension.

func (*Session) Hello

func (c *Session) Hello(localName string) error

Hello sends a HELO or EHLO to the server as the given host name. Calling this method is only necessary if the client needs control over the host name used. The client will introduce itself as "localhost" automatically otherwise. If Hello is called, it must be called before any of the other methods.

func (*Session) Mail

func (c *Session) Mail(from string) error

Mail issues a MAIL command to the server using the provided email address. If the server supports the 8BITMIME extension, Mail adds the BODY=8BITMIME parameter. This initiates a mail transaction and is followed by one or more Rcpt calls.

func (*Session) MailAndRcpt

func (c *Session) MailAndRcpt(msg *Email) error

MailAndRcpt issues MAIL FROM and RCPT TO commands, in sequence. It will check the addresses, decide if SMTPUTF8 is needed, and apply the necessary transformations. If the message ReturnPath is setted, it will be used as MAIL FROM address. If the message Recipient is setted, it will be used ad the only RCPT TO address.

func (*Session) Quit

func (c *Session) Quit() error

Quit sends the QUIT command and closes the connection to the server.

func (*Session) Rcpt

func (c *Session) Rcpt(to string) error

Rcpt issues a RCPT command to the server using the provided email address. A call to Rcpt must be preceded by a call to Mail and may be followed by a Data call or another Rcpt call.

func (*Session) Reset

func (c *Session) Reset() error

Reset sends the RSET command to the server, aborting the current mail transaction.

func (*Session) SendMessageBulk

func (c *Session) SendMessageBulk(messages []*Email) (SendBulkReport, error)

SendMessageBulk sends a list of messages to an SMTP server using the same connection and at the end closes the session and the connection.

func (*Session) SendSingleMessage

func (c *Session) SendSingleMessage(msg *Email) error

SendSingleMessage sends a single email to the recipient. The method requires that the session is open and leaves it open.

func (*Session) StartSession

func (c *Session) StartSession() error

StartSession opens an SMTP session.

func (*Session) StartTLS

func (c *Session) StartTLS(config *tls.Config) error

StartTLS sends the STARTTLS command and encrypts all further communication. Only servers that advertise the STARTTLS extension support this function.

func (*Session) TLSConnectionState

func (c *Session) TLSConnectionState() (state tls.ConnectionState, ok bool)

TLSConnectionState returns the client's TLS connection state. The return values are their zero values if StartTLS did not succeed.

func (*Session) Verify

func (c *Session) Verify(addr string) error

Verify checks the validity of an email address on the server. If Verify returns nil, the address is valid. A non-nil return does not necessarily indicate an invalid address. Many servers will not verify addresses for security reasons.

Jump to

Keyboard shortcuts

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