2012-03-27 23:13:14 +00:00
|
|
|
// Copyright 2010 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// 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
|
|
|
|
// Additional extensions may be handled by clients.
|
2017-10-07 00:16:47 +00:00
|
|
|
//
|
|
|
|
// The smtp package is frozen and is not accepting new features.
|
|
|
|
// Some external packages provide more functionality. See:
|
|
|
|
//
|
|
|
|
// https://godoc.org/?q=smtp
|
2012-03-27 23:13:14 +00:00
|
|
|
package smtp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"encoding/base64"
|
2014-09-21 17:33:12 +00:00
|
|
|
"errors"
|
2017-10-07 00:16:47 +00:00
|
|
|
"fmt"
|
2012-03-27 23:13:14 +00:00
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"net/textproto"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// A Client represents a client connection to an SMTP server.
|
|
|
|
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
|
|
|
|
// keep a reference to the connection so it can be used to create a TLS
|
|
|
|
// connection later
|
|
|
|
conn net.Conn
|
|
|
|
// whether the Client is using TLS
|
|
|
|
tls bool
|
|
|
|
serverName string
|
|
|
|
// map of supported extensions
|
|
|
|
ext map[string]string
|
|
|
|
// supported auth mechanisms
|
2014-09-21 17:33:12 +00:00
|
|
|
auth []string
|
|
|
|
localName string // the name to use in HELO/EHLO
|
|
|
|
didHello bool // whether we've said HELO/EHLO
|
|
|
|
helloError error // the error from the hello
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Dial returns a new Client connected to an SMTP server at addr.
|
2017-04-10 11:32:00 +00:00
|
|
|
// The addr must include a port, as in "mail.example.com:smtp".
|
2012-03-27 23:13:14 +00:00
|
|
|
func Dial(addr string) (*Client, error) {
|
|
|
|
conn, err := net.Dial("tcp", addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-09-21 17:33:12 +00:00
|
|
|
host, _, _ := net.SplitHostPort(addr)
|
2012-03-27 23:13:14 +00:00
|
|
|
return NewClient(conn, host)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
text := textproto.NewConn(conn)
|
|
|
|
_, _, err := text.ReadResponse(220)
|
|
|
|
if err != nil {
|
|
|
|
text.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-09-21 17:33:12 +00:00
|
|
|
c := &Client{Text: text, conn: conn, serverName: host, localName: "localhost"}
|
2018-12-28 15:30:48 +00:00
|
|
|
_, c.tls = conn.(*tls.Conn)
|
2014-09-21 17:33:12 +00:00
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the connection.
|
|
|
|
func (c *Client) Close() error {
|
|
|
|
return c.Text.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// hello runs a hello exchange if needed.
|
|
|
|
func (c *Client) hello() error {
|
|
|
|
if !c.didHello {
|
|
|
|
c.didHello = true
|
|
|
|
err := c.ehlo()
|
|
|
|
if err != nil {
|
|
|
|
c.helloError = c.helo()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return c.helloError
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2017-10-07 00:16:47 +00:00
|
|
|
// over the host name used. The client will introduce itself as "localhost"
|
|
|
|
// automatically otherwise. If Hello is called, it must be called before
|
2014-09-21 17:33:12 +00:00
|
|
|
// any of the other methods.
|
|
|
|
func (c *Client) Hello(localName string) error {
|
2018-12-28 15:30:48 +00:00
|
|
|
if err := validateLine(localName); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-09-21 17:33:12 +00:00
|
|
|
if c.didHello {
|
|
|
|
return errors.New("smtp: Hello called after other methods")
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
2014-09-21 17:33:12 +00:00
|
|
|
c.localName = localName
|
|
|
|
return c.hello()
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// cmd is a convenience function that sends a command and returns the response
|
|
|
|
func (c *Client) cmd(expectCode int, format string, args ...interface{}) (int, string, error) {
|
|
|
|
id, err := c.Text.Cmd(format, args...)
|
|
|
|
if err != nil {
|
|
|
|
return 0, "", err
|
|
|
|
}
|
|
|
|
c.Text.StartResponse(id)
|
|
|
|
defer c.Text.EndResponse(id)
|
|
|
|
code, msg, err := c.Text.ReadResponse(expectCode)
|
|
|
|
return code, msg, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
c.ext = nil
|
2014-09-21 17:33:12 +00:00
|
|
|
_, _, err := c.cmd(250, "HELO %s", c.localName)
|
2012-03-27 23:13:14 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2014-09-21 17:33:12 +00:00
|
|
|
_, msg, err := c.cmd(250, "EHLO %s", c.localName)
|
2012-03-27 23:13:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ext := make(map[string]string)
|
|
|
|
extList := strings.Split(msg, "\n")
|
|
|
|
if len(extList) > 1 {
|
|
|
|
extList = extList[1:]
|
|
|
|
for _, line := range extList {
|
|
|
|
args := strings.SplitN(line, " ", 2)
|
|
|
|
if len(args) > 1 {
|
|
|
|
ext[args[0]] = args[1]
|
|
|
|
} else {
|
|
|
|
ext[args[0]] = ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if mechs, ok := ext["AUTH"]; ok {
|
|
|
|
c.auth = strings.Split(mechs, " ")
|
|
|
|
}
|
|
|
|
c.ext = ext
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2014-09-21 17:33:12 +00:00
|
|
|
if err := c.hello(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2012-03-27 23:13:14 +00:00
|
|
|
_, _, err := c.cmd(220, "STARTTLS")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.conn = tls.Client(c.conn, config)
|
|
|
|
c.Text = textproto.NewConn(c.conn)
|
|
|
|
c.tls = true
|
|
|
|
return c.ehlo()
|
|
|
|
}
|
|
|
|
|
2017-04-10 11:32:00 +00:00
|
|
|
// TLSConnectionState returns the client's TLS connection state.
|
|
|
|
// The return values are their zero values if StartTLS did
|
|
|
|
// not succeed.
|
|
|
|
func (c *Client) TLSConnectionState() (state tls.ConnectionState, ok bool) {
|
|
|
|
tc, ok := c.conn.(*tls.Conn)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return tc.ConnectionState(), true
|
|
|
|
}
|
|
|
|
|
2012-03-27 23:13:14 +00:00
|
|
|
// 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 {
|
2018-12-28 15:30:48 +00:00
|
|
|
if err := validateLine(addr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-09-21 17:33:12 +00:00
|
|
|
if err := c.hello(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2012-03-27 23:13:14 +00:00
|
|
|
_, _, err := c.cmd(250, "VRFY %s", addr)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2014-09-21 17:33:12 +00:00
|
|
|
if err := c.hello(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2012-03-27 23:13:14 +00:00
|
|
|
encoding := base64.StdEncoding
|
|
|
|
mech, resp, err := a.Start(&ServerInfo{c.serverName, c.tls, c.auth})
|
|
|
|
if err != nil {
|
|
|
|
c.Quit()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
resp64 := make([]byte, encoding.EncodedLen(len(resp)))
|
|
|
|
encoding.Encode(resp64, resp)
|
2017-10-07 00:16:47 +00:00
|
|
|
code, msg64, err := c.cmd(0, strings.TrimSpace(fmt.Sprintf("AUTH %s %s", mech, resp64)))
|
2012-03-27 23:13:14 +00:00
|
|
|
for err == nil {
|
|
|
|
var msg []byte
|
|
|
|
switch code {
|
|
|
|
case 334:
|
|
|
|
msg, err = encoding.DecodeString(msg64)
|
|
|
|
case 235:
|
|
|
|
// the last message isn't base64 because it isn't a challenge
|
|
|
|
msg = []byte(msg64)
|
|
|
|
default:
|
|
|
|
err = &textproto.Error{Code: code, Msg: msg64}
|
|
|
|
}
|
2014-09-21 17:33:12 +00:00
|
|
|
if err == nil {
|
|
|
|
resp, err = a.Next(msg, code == 334)
|
|
|
|
}
|
2012-03-27 23:13:14 +00:00
|
|
|
if err != nil {
|
|
|
|
// abort the AUTH
|
|
|
|
c.cmd(501, "*")
|
|
|
|
c.Quit()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if resp == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
resp64 = make([]byte, encoding.EncodedLen(len(resp)))
|
|
|
|
encoding.Encode(resp64, resp)
|
|
|
|
code, msg64, err = c.cmd(0, string(resp64))
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 (c *Client) Mail(from string) error {
|
2018-12-28 15:30:48 +00:00
|
|
|
if err := validateLine(from); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-09-21 17:33:12 +00:00
|
|
|
if err := c.hello(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2012-03-27 23:13:14 +00:00
|
|
|
cmdStr := "MAIL FROM:<%s>"
|
|
|
|
if c.ext != nil {
|
|
|
|
if _, ok := c.ext["8BITMIME"]; ok {
|
|
|
|
cmdStr += " BODY=8BITMIME"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_, _, err := c.cmd(250, cmdStr, from)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 (c *Client) Rcpt(to string) error {
|
2018-12-28 15:30:48 +00:00
|
|
|
if err := validateLine(to); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2012-03-27 23:13:14 +00:00
|
|
|
_, _, err := c.cmd(25, "RCPT TO:<%s>", to)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
type dataCloser struct {
|
|
|
|
c *Client
|
|
|
|
io.WriteCloser
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *dataCloser) Close() error {
|
|
|
|
d.WriteCloser.Close()
|
|
|
|
_, _, err := d.c.Text.ReadResponse(250)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Data issues a DATA command to the server and returns a writer that
|
2017-04-10 11:32:00 +00:00
|
|
|
// can be used to write the mail headers and body. The caller should
|
2017-10-07 00:16:47 +00:00
|
|
|
// close the writer before calling any more methods on c. A call to
|
2017-04-10 11:32:00 +00:00
|
|
|
// Data must be preceded by one or more calls to Rcpt.
|
2012-03-27 23:13:14 +00:00
|
|
|
func (c *Client) Data() (io.WriteCloser, error) {
|
|
|
|
_, _, err := c.cmd(354, "DATA")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &dataCloser{c, c.Text.DotWriter()}, nil
|
|
|
|
}
|
|
|
|
|
2015-08-28 15:33:40 +00:00
|
|
|
var testHookStartTLS func(*tls.Config) // nil, except for tests
|
|
|
|
|
2014-09-21 17:33:12 +00:00
|
|
|
// 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.
|
2017-04-10 11:32:00 +00:00
|
|
|
// 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
|
2017-10-07 00:16:47 +00:00
|
|
|
// should be CRLF terminated. The msg headers should usually include
|
2017-04-10 11:32:00 +00:00
|
|
|
// 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.
|
|
|
|
//
|
2018-12-28 15:30:48 +00:00
|
|
|
// The SendMail function and the net/smtp package are low-level
|
2017-04-10 11:32:00 +00:00
|
|
|
// 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.
|
2012-03-27 23:13:14 +00:00
|
|
|
func SendMail(addr string, a Auth, from string, to []string, msg []byte) error {
|
2018-12-28 15:30:48 +00:00
|
|
|
if err := validateLine(from); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, recp := range to {
|
|
|
|
if err := validateLine(recp); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2012-03-27 23:13:14 +00:00
|
|
|
c, err := Dial(addr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-09-21 17:33:12 +00:00
|
|
|
defer c.Close()
|
|
|
|
if err = c.hello(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2012-03-27 23:13:14 +00:00
|
|
|
if ok, _ := c.Extension("STARTTLS"); ok {
|
2015-08-28 15:33:40 +00:00
|
|
|
config := &tls.Config{ServerName: c.serverName}
|
|
|
|
if testHookStartTLS != nil {
|
|
|
|
testHookStartTLS(config)
|
|
|
|
}
|
|
|
|
if err = c.StartTLS(config); err != nil {
|
2012-03-27 23:13:14 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if a != nil && c.ext != nil {
|
2019-06-02 15:48:37 +00:00
|
|
|
if _, ok := c.ext["AUTH"]; !ok {
|
|
|
|
return errors.New("smtp: server doesn't support AUTH")
|
|
|
|
}
|
|
|
|
if err = c.Auth(a); err != nil {
|
|
|
|
return err
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if err = c.Mail(from); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, addr := range to {
|
|
|
|
if err = c.Rcpt(addr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
w, err := c.Data()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = w.Write(msg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = w.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return c.Quit()
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2014-09-21 17:33:12 +00:00
|
|
|
if err := c.hello(); err != nil {
|
|
|
|
return false, ""
|
|
|
|
}
|
2012-03-27 23:13:14 +00:00
|
|
|
if c.ext == nil {
|
|
|
|
return false, ""
|
|
|
|
}
|
|
|
|
ext = strings.ToUpper(ext)
|
|
|
|
param, ok := c.ext[ext]
|
|
|
|
return ok, param
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset sends the RSET command to the server, aborting the current mail
|
|
|
|
// transaction.
|
|
|
|
func (c *Client) Reset() error {
|
2014-09-21 17:33:12 +00:00
|
|
|
if err := c.hello(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2012-03-27 23:13:14 +00:00
|
|
|
_, _, err := c.cmd(250, "RSET")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-12-28 15:30:48 +00:00
|
|
|
// 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 {
|
|
|
|
if err := c.hello(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, _, err := c.cmd(250, "NOOP")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2012-03-27 23:13:14 +00:00
|
|
|
// Quit sends the QUIT command and closes the connection to the server.
|
|
|
|
func (c *Client) Quit() error {
|
2014-09-21 17:33:12 +00:00
|
|
|
if err := c.hello(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2012-03-27 23:13:14 +00:00
|
|
|
_, _, err := c.cmd(221, "QUIT")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return c.Text.Close()
|
|
|
|
}
|
2018-12-28 15:30:48 +00:00
|
|
|
|
|
|
|
// validateLine checks to see if a line has CR or LF as per RFC 5321
|
|
|
|
func validateLine(line string) error {
|
|
|
|
if strings.ContainsAny(line, "\n\r") {
|
|
|
|
return errors.New("smtp: A line must not contain CR or LF")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|