smtp

Imports

Imports #

"crypto/hmac"
"crypto/md5"
"errors"
"fmt"
"crypto/tls"
"encoding/base64"
"errors"
"fmt"
"io"
"net"
"net/textproto"
"strings"

Constants & Variables

testHookStartTLS var #

var testHookStartTLS func(*tls.Config)

Interfaces

Auth interface #

Auth is implemented by an SMTP authentication mechanism.

type Auth interface {
Start(server *ServerInfo) (proto string, toServer []byte, err error)
Next(fromServer []byte, more bool) (toServer []byte, err error)
}

Structs

Client struct #

A Client represents a client connection to an SMTP server.

type Client struct {
Text *textproto.Conn
conn net.Conn
tls bool
serverName string
ext map[string]string
auth []string
localName string
didHello bool
helloError error
}

ServerInfo struct #

ServerInfo records information about an SMTP server.

type ServerInfo struct {
Name string
TLS bool
Auth []string
}

cramMD5Auth struct #

type cramMD5Auth struct {
username string
secret string
}

dataCloser struct #

type dataCloser struct {
c *Client
io.WriteCloser
}

plainAuth struct #

type plainAuth struct {
identity string
username string
password string
host string
}

Functions

Auth method #

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 (c *Client) Auth(a Auth) error

CRAMMD5Auth function #

CRAMMD5Auth returns an [Auth] that implements the CRAM-MD5 authentication mechanism as defined in RFC 2195. The returned Auth uses the given username and secret to authenticate to the server using the challenge-response mechanism.

func CRAMMD5Auth(username string, secret string) Auth

Close method #

Close closes the connection.

func (c *Client) Close() error

Close method #

func (d *dataCloser) Close() error

Data method #

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 [Client.Rcpt].

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

Dial function #

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 Dial(addr string) (*Client, error)

Extension method #

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 (c *Client) Extension(ext string) (bool, string)

Hello method #

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 (c *Client) Hello(localName string) error

Mail method #

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. If the server supports the SMTPUTF8 extension, Mail adds the SMTPUTF8 parameter. This initiates a mail transaction and is followed by one or more [Client.Rcpt] calls.

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

NewClient function #

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

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

Next method #

func (a *plainAuth) Next(fromServer []byte, more bool) ([]byte, error)

Next method #

func (a *cramMD5Auth) Next(fromServer []byte, more bool) ([]byte, error)

Noop method #

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

func (c *Client) Noop() error

PlainAuth function #

PlainAuth returns an [Auth] that implements the PLAIN authentication mechanism as defined in RFC 4616. The returned Auth uses the given username and password to authenticate to host and act as identity. Usually identity should be the empty string, to act as username. PlainAuth will only send the credentials if the connection is using TLS or is connected to localhost. Otherwise authentication will fail with an error, without sending the credentials.

func PlainAuth(identity string, username string, password string, host string) Auth

Quit method #

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

func (c *Client) Quit() error

Rcpt method #

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

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

Reset method #

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

func (c *Client) Reset() error

SendMail function #

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". The addresses in the to parameter are the SMTP RCPT addresses. The msg parameter should be an RFC 822-style email with headers first, a blank line, and then the message body. The lines of msg should be CRLF terminated. The msg 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 msg headers. The SendMail function and the net/smtp package are low-level mechanisms and provide no support for DKIM signing, MIME attachments (see the mime/multipart package), or other mail functionality. Higher-level packages exist outside of the standard library.

func SendMail(addr string, a Auth, from string, to []string, msg []byte) error

Start method #

func (a *plainAuth) Start(server *ServerInfo) (string, []byte, error)

Start method #

func (a *cramMD5Auth) Start(server *ServerInfo) (string, []byte, error)

StartTLS method #

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

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

TLSConnectionState method #

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

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

Verify method #

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.

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

cmd method #

cmd is a convenience function that sends a command and returns the response

func (c *Client) cmd(expectCode int, format string, args ...any) (int, string, error)

ehlo method #

ehlo sends the EHLO (extended hello) greeting to the server. It should be the preferred greeting for servers that support it.

func (c *Client) ehlo() error

hello method #

hello runs a hello exchange if needed.

func (c *Client) hello() error

helo method #

helo sends the HELO greeting to the server. It should be used only when the server does not support ehlo.

func (c *Client) helo() error

isLocalhost function #

func isLocalhost(name string) bool

validateLine function #

validateLine checks to see if a line has CR or LF as per RFC 5321.

func validateLine(line string) error

Generated with Arrow