version: 1.10

package smtp

import "net/smtp"

Overview

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

  1. It also implements the following extensions:

    8BITMIME RFC 1652
    AUTH RFC 2554
    STARTTLS RFC 3207

Additional extensions may be handled by clients.

The smtp package is frozen and is not accepting new features. Some external
packages provide more functionality. See:

  1. https://godoc.org/?q=smtp


Example:

  1. // Connect to the remote SMTP server.
  2. c, err := smtp.Dial("mail.example.com:25")
  3. if err != nil {
  4. log.Fatal(err)
  5. }
  6. // Set the sender and recipient first
  7. if err := c.Mail("sender@example.org"); err != nil {
  8. log.Fatal(err)
  9. }
  10. if err := c.Rcpt("recipient@example.net"); err != nil {
  11. log.Fatal(err)
  12. }
  13. // Send the email body.
  14. wc, err := c.Data()
  15. if err != nil {
  16. log.Fatal(err)
  17. }
  18. _, err = fmt.Fprintf(wc, "This is the email body")
  19. if err != nil {
  20. log.Fatal(err)
  21. }
  22. err = wc.Close()
  23. if err != nil {
  24. log.Fatal(err)
  25. }
  26. // Send the QUIT command and close the connection.
  27. err = c.Quit()
  28. if err != nil {
  29. log.Fatal(err)
  30. }

Index

Examples

Package files

auth.go smtp.go

func SendMail

  1. func SendMail(addr string, a Auth, from string, to []string, msg []byte) 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”.

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.


Example:

  1. // Set up authentication information.
  2. auth := smtp.PlainAuth("", "user@example.com", "password", "mail.example.com")
  3. // Connect to the server, authenticate, set the sender and recipient,
  4. // and send the email all in one step.
  5. to := []string{"recipient@example.net"}
  6. msg := []byte("To: recipient@example.net\r\n" +
  7. "Subject: discount Gophers!\r\n" +
  8. "\r\n" +
  9. "This is the email body.\r\n")
  10. err := smtp.SendMail("mail.example.com:25", auth, "sender@example.org", to, msg)
  11. if err != nil {
  12. log.Fatal(err)
  13. }

type Auth

  1. type Auth interface {
  2. // Start begins an authentication with a server.
  3. // It returns the name of the authentication protocol
  4. // and optionally data to include in the initial AUTH message
  5. // sent to the server. It can return proto == "" to indicate
  6. // that the authentication should be skipped.
  7. // If it returns a non-nil error, the SMTP client aborts
  8. // the authentication attempt and closes the connection.
  9. Start(server *ServerInfo) (proto string, toServer []byte, err error)
  10.  
  11. // Next continues the authentication. The server has just sent
  12. // the fromServer data. If more is true, the server expects a
  13. // response, which Next should return as toServer; otherwise
  14. // Next should return toServer == nil.
  15. // If Next returns a non-nil error, the SMTP client aborts
  16. // the authentication attempt and closes the connection.
  17. Next(fromServer []byte, more bool) (toServer []byte, err error)
  18. }

Auth is implemented by an SMTP authentication mechanism.

func CRAMMD5Auth

  1. func CRAMMD5Auth(username, secret string) Auth

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 PlainAuth

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

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.


Example:

  1. // hostname is used by PlainAuth to validate the TLS certificate.
  2. hostname := "mail.example.com"
  3. auth := smtp.PlainAuth("", "user@example.com", "password", hostname)
  4. err := smtp.SendMail(hostname+":25", auth, from, recipients, msg)
  5. if err != nil {
  6. log.Fatal(err)
  7. }

type Client

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

A Client represents a client connection to an SMTP server.

func Dial

  1. 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 NewClient

  1. 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 (*Client) Auth

  1. func (c *Client) Auth(a 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 (*Client) Close

  1. func (c *Client) Close() error

Close closes the connection.

func (*Client) Data

  1. 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.

func (*Client) Extension

  1. 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

  1. 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.

func (*Client) Mail

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

  1. 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

  1. func (c *Client) Quit() error

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

func (*Client) Rcpt

  1. 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.

func (*Client) Reset

  1. func (c *Client) Reset() error

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

func (*Client) StartTLS

  1. 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.

func (*Client) TLSConnectionState

  1. 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

  1. 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.

type ServerInfo

  1. type ServerInfo struct {
  2. Name string // SMTP server name
  3. TLS bool // using TLS, with valid certificate for Name
  4. Auth []string // advertised authentication mechanisms
  5. }

ServerInfo records information about an SMTP server.