smtp

package
v0.0.0-...-10f170f Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2023 License: Apache-2.0, MIT Imports: 11 Imported by: 0

Documentation

Overview

Package smtp implements the Simple Mail Transfer Protocol as defined in RFC 5321.

It also implements the following extensions:

8BITMIME: RFC 1652
AUTH: RFC 2554
STARTTLS: RFC 3207
ENHANCEDSTATUSCODES: RFC 2034
SMTPUTF8: RFC 6531
REQUIRETLS: RFC 8689
CHUNKING: RFC 3030
BINARYMIME: RFC 3030

LMTP (RFC 2033) is also supported.

Additional extensions may be handled by other packages.

Index

Constants

This section is empty.

Variables

View Source
var EnhancedCodeNotSet = EnhancedCode{0, 0, 0}

EnhancedCodeNotSet is a nil value of EnhancedCode field in SMTPError, used to indicate that backend failed to provide enhanced status code. X.0.0 will be used (X is derived from error code).

View Source
var ErrTooLongLine = errors.New("smtp: too long a line in input stream")
View Source
var NoEnhancedCode = EnhancedCode{-1, -1, -1}

NoEnhancedCode is used to indicate that enhanced error code should not be included in response.

Note that RFC 2034 requires an enhanced code to be included in all 2xx, 4xx and 5xx responses. This constant is exported for use by extensions, you should probably use EnhancedCodeNotSet instead.

Functions

func SendMail

func SendMail(addr string, a sasl.Client, from string, to []string, r io.Reader) error

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

The addresses in the to parameter are the SMTP RCPT addresses.

The r parameter should be an RFC 822-style email with headers first, a blank line, and then the message body. The lines of r should be CRLF terminated. The r headers should usually include fields such as "From", "To", "Subject", and "Cc". Sending "Bcc" messages is accomplished by including an email address in the to parameter but not including it in the r headers.

SendMail is intended to be used for very simple use-cases. If you want to customize SendMail's behavior, use a Client instead.

The SendMail function and the go-smtp package are low-level mechanisms and provide no support for DKIM signing (see go-msgauth), MIME attachments (see the mime/multipart package or the go-message package), or other mail functionality.

Types

type BodyType

type BodyType string
const (
	Body7Bit       BodyType = "7BIT"
	Body8BitMIME   BodyType = "8BITMIME"
	BodyBinaryMIME BodyType = "BINARYMIME"
)

type Client

type Client struct {
	// Text is the textproto.Conn used by the Client. It is exported to allow for
	// clients to add extensions.
	Text *textproto.Conn

	// Time to wait for command responses (this includes 3xx reply to DATA).
	CommandTimeout time.Duration
	// Time to wait for responses after final dot.
	SubmissionTimeout time.Duration

	// Logger for all network activity.
	DebugWriter io.Writer
	// contains filtered or unexported fields
}

A Client represents a client connection to an SMTP server.

func Dial

func Dial(addr string) (*Client, error)

Dial returns a new Client connected to an SMTP server at addr. The addr must include a port, as in "mail.example.com:smtp".

func DialTLS

func DialTLS(addr string, tlsConfig *tls.Config) (*Client, error)

DialTLS returns a new Client connected to an SMTP server via TLS at addr. The addr must include a port, as in "mail.example.com:smtps".

A nil tlsConfig is equivalent to a zero tls.Config.

func NewClient

func NewClient(conn net.Conn, host string) (*Client, error)

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

func NewClientLMTP

func NewClientLMTP(conn net.Conn, host string) (*Client, error)

NewClientLMTP returns a new LMTP Client (as defined in RFC 2033) using an existing connection and host as a server name to be used when authenticating.

func (*Client) Auth

func (c *Client) Auth(a sasl.Client) error

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

If server returns an error, it will be of type *SMTPError.

func (*Client) Close

func (c *Client) Close() error

Close closes the connection.

func (*Client) Data

func (c *Client) 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.

If server returns an error, it will be of type *SMTPError.

func (*Client) Extension

func (c *Client) 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 (*Client) Hello

func (c *Client) 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.

If server returns an error, it will be of type *SMTPError.

func (*Client) LMTPData

func (c *Client) LMTPData(statusCb func(rcpt string, status *SMTPError)) (io.WriteCloser, error)

LMTPData is the LMTP-specific version of the Data method. It accepts a callback that will be called for each status response received from the server.

Status callback will receive a SMTPError argument for each negative server reply and nil for each positive reply. I/O errors will not be reported using callback and instead will be returned by the Close method of io.WriteCloser. Callback will be called for each successfull Rcpt call done before in the same order.

func (*Client) Mail

func (c *Client) Mail(from string, opts *MailOptions) 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.

If opts is not nil, MAIL arguments provided in the structure will be added to the command. Handling of unsupported options depends on the extension.

If server returns an error, it will be of type *SMTPError.

func (*Client) Noop

func (c *Client) Noop() error

Noop sends the NOOP command to the server. It does nothing but check that the connection to the server is okay.

func (*Client) Quit

func (c *Client) Quit() error

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

If Quit fails the connection is not closed, Close should be used in this case.

func (*Client) Rcpt

func (c *Client) 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.

If server returns an error, it will be of type *SMTPError.

func (*Client) Reset

func (c *Client) Reset() error

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

func (*Client) SendMail

func (c *Client) SendMail(from string, to []string, r io.Reader) error

SendMail will use an existing connection to send an email from address from, to addresses to, with message r.

This function does not start TLS, nor does it perform authentication. Use StartTLS and Auth before-hand if desirable.

The addresses in the to parameter are the SMTP RCPT addresses.

The r parameter should be an RFC 822-style email with headers first, a blank line, and then the message body. The lines of r should be CRLF terminated. The r headers should usually include fields such as "From", "To", "Subject", and "Cc". Sending "Bcc" messages is accomplished by including an email address in the to parameter but not including it in the r headers.

func (*Client) StartTLS

func (c *Client) 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.

A nil config is equivalent to a zero tls.Config.

If server returns an error, it will be of type *SMTPError.

func (*Client) TLSConnectionState

func (c *Client) 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 (*Client) Verify

func (c *Client) 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.

If server returns an error, it will be of type *SMTPError.

type EnhancedCode

type EnhancedCode [3]int

type MailOptions

type MailOptions struct {
	// Value of BODY= argument, 7BIT, 8BITMIME or BINARYMIME.
	Body BodyType

	// Size of the body. Can be 0 if not specified by client.
	Size int

	// TLS is required for the message transmission.
	//
	// The message should be rejected if it can't be transmitted
	// with TLS.
	RequireTLS bool

	// The message envelope or message header contains UTF-8-encoded strings.
	// This flag is set by SMTPUTF8-aware (RFC 6531) client.
	UTF8 bool

	// The authorization identity asserted by the message sender in decoded
	// form with angle brackets stripped.
	//
	// nil value indicates missing AUTH, non-nil empty string indicates
	// AUTH=<>.
	//
	// Defined in RFC 4954.
	Auth *string
}

MailOptions contains custom arguments that were passed as an argument to the MAIL command.

type SMTPError

type SMTPError struct {
	Code         int
	EnhancedCode EnhancedCode
	Message      string
}

SMTPError specifies the error code, enhanced error code (if any) and message returned by the server.

func (*SMTPError) Error

func (err *SMTPError) Error() string

func (*SMTPError) Temporary

func (err *SMTPError) Temporary() bool

Jump to

Keyboard shortcuts

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