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}, nil
}
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.
package smtp
import (
	"crypto/tls"
	"encoding/base64"
	"errors"
	"fmt"
	"io"
	"net"
	"net/textproto"
	"strconv"
	"strings"
	"time"
	"github.com/emersion/go-sasl"
)
type Client struct {
	Text *textproto.Conn
	conn net.Conn
	tls        bool
	serverName string
	lmtp       bool
	ext map[string]string
	auth       []string
	localName  string    
	didHello   bool      
	helloError error     
	rcpts      []string  
	CommandTimeout time.Duration
	SubmissionTimeout time.Duration
	DebugWriter io.Writer
}
var defaultTimeout = 30 * time.Second
func Dial(addr string) (*Client, error) {
	conn, err := net.DialTimeout("tcp", addr, defaultTimeout)
	if err != nil {
		return nil, err
	}
	host, _, _ := net.SplitHostPort(addr)
	return NewClient(conn, host)
}
func DialTLS(addr string, tlsConfig *tls.Config) (*Client, error) {
	tlsDialer := tls.Dialer{
		NetDialer: &net.Dialer{
			Timeout: defaultTimeout,
		},
		Config: tlsConfig,
	}
	conn, err := tlsDialer.Dial("tcp", addr)
	if err != nil {
		return nil, err
	}
	host, _, _ := net.SplitHostPort(addr)
	return NewClient(conn, host)
}
func NewClient(conn net.Conn, host string) (*Client, error) {
	c := &Client{
		serverName: host,
		localName:  "localhost",
		CommandTimeout: 5 * time.Minute,
		SubmissionTimeout: 12 * time.Minute,
	}
	c.setConn(conn)
	c.conn.SetDeadline(time.Now().Add(5 * time.Minute))
	defer c.conn.SetDeadline(time.Time{})
	_, _, err := c.Text.ReadResponse(220)
	if err != nil {
		c.Text.Close()
		if protoErr, ok := err.(*textproto.Error); ok {
			return nil, toSMTPErr(protoErr)
		}
		return nil, err
	}
	return c, nil
}
func NewClientLMTP(conn net.Conn, host string) (*Client, error) {
	c, err := NewClient(conn, host)
	if err != nil {
		return nil, err
	}
	c.lmtp = true
	return c, nil
}
func (c *Client) setConn(conn net.Conn) {
	c.conn = conn
	var r io.Reader = conn
	var w io.Writer = conn
	r = &lineLimitReader{
		R: conn,
		LineLimit: 2000,
	}
	r = io.TeeReader(r, clientDebugWriter{c})
	w = io.MultiWriter(w, clientDebugWriter{c})
	rwc := struct {
		io.Reader
		io.Writer
		io.Closer
	}{
		Reader: r,
		Writer: w,
		Closer: conn,
	}
	c.Text = textproto.NewConn(rwc)
	_, isTLS := conn.(*tls.Conn)
	c.tls = isTLS
}
func (c *Client) Close() error {
	return c.Text.Close()
}
func (c *Client) hello() error {
	if !c.didHello {
		c.didHello = true
		err := c.ehlo()
		if err != nil {
			c.helloError = c.helo()
		}
	}
	return c.helloError
}
func (c *Client) Hello(localName string) error {
	if err := validateLine(localName); err != nil {
		return err
	}
	if c.didHello {
		return errors.New("smtp: Hello called after other methods")
	}
	c.localName = localName
	return c.hello()
}
func (c *Client) cmd(expectCode int, format string, args ...interface{}) (int, string, error) {
	c.conn.SetDeadline(time.Now().Add(c.CommandTimeout))
	defer c.conn.SetDeadline(time.Time{})
	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)
	if err != nil {
		if protoErr, ok := err.(*textproto.Error); ok {
			smtpErr := toSMTPErr(protoErr)
			return code, smtpErr.Message, smtpErr
		}
		return code, msg, err
	}
	return code, msg, nil
}
func (c *Client) helo() error {
	c.ext = nil
	_, _, err := c.cmd(250, "HELO %s", c.localName)
	return err
}
func (c *Client) ehlo() error {
	cmd := "EHLO"
	if c.lmtp {
		cmd = "LHLO"
	}
	_, msg, err := c.cmd(250, "%s %s", cmd, c.localName)
	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
}
func (c *Client) StartTLS(config *tls.Config) error {
	if err := c.hello(); err != nil {
		return err
	}
	_, _, err := c.cmd(220, "STARTTLS")
	if err != nil {
		return err
	}
	if config == nil {
		config = &tls.Config{}
	}
	if config.ServerName == "" {
		config = config.Clone()
		config.ServerName = c.serverName
	}
	if testHookStartTLS != nil {
		testHookStartTLS(config)
	}
	c.setConn(tls.Client(c.conn, config))
	return c.ehlo()
}
func (c *Client) TLSConnectionState() (state tls.ConnectionState, ok bool) {
	tc, ok := c.conn.(*tls.Conn)
	if !ok {
		return
	}
	return tc.ConnectionState(), true
}
func (c *Client) Verify(addr string) error {
	if err := validateLine(addr); err != nil {
		return err
	}
	if err := c.hello(); err != nil {
		return err
	}
	_, _, err := c.cmd(250, "VRFY %s", addr)
	return err
}
func (c *Client) Auth(a sasl.Client) error {
	if err := c.hello(); err != nil {
		return err
	}
	encoding := base64.StdEncoding
	mech, resp, err := a.Start()
	if err != nil {
		return err
	}
	resp64 := make([]byte, encoding.EncodedLen(len(resp)))
	encoding.Encode(resp64, resp)
	code, msg64, err := c.cmd(0, strings.TrimSpace(fmt.Sprintf("AUTH %s %s", mech, resp64)))
	for err == nil {
		var msg []byte
		switch code {
		case 334:
			msg, err = encoding.DecodeString(msg64)
		case 235:
			msg = []byte(msg64)
		default:
			err = toSMTPErr(&textproto.Error{Code: code, Msg: msg64})
		}
		if err == nil {
			if code == 334 {
				resp, err = a.Next(msg)
			} else {
				resp = nil
			}
		}
		if err != nil {
			c.cmd(501, "*")
			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
}
func (c *Client) Mail(from string, opts *MailOptions) error {
	if err := validateLine(from); err != nil {
		return err
	}
	if err := c.hello(); err != nil {
		return err
	}
	cmdStr := "MAIL FROM:<%s>"
	if _, ok := c.ext["8BITMIME"]; ok {
		cmdStr += " BODY=8BITMIME"
	}
	if _, ok := c.ext["SIZE"]; ok && opts != nil && opts.Size != 0 {
		cmdStr += " SIZE=" + strconv.Itoa(opts.Size)
	}
	if opts != nil && opts.RequireTLS {
		if _, ok := c.ext["REQUIRETLS"]; ok {
			cmdStr += " REQUIRETLS"
		} else {
			return errors.New("smtp: server does not support REQUIRETLS")
		}
	}
	if opts != nil && opts.UTF8 {
		if _, ok := c.ext["SMTPUTF8"]; ok {
			cmdStr += " SMTPUTF8"
		} else {
			return errors.New("smtp: server does not support SMTPUTF8")
		}
	}
	if opts != nil && opts.Auth != nil {
		if _, ok := c.ext["AUTH"]; ok {
			cmdStr += " AUTH=" + encodeXtext(*opts.Auth)
		}
	}
	_, _, err := c.cmd(250, cmdStr, from)
	return err
}
func (c *Client) Rcpt(to string) error {
	if err := validateLine(to); err != nil {
		return err
	}
	if _, _, err := c.cmd(25, "RCPT TO:<%s>", to); err != nil {
		return err
	}
	c.rcpts = append(c.rcpts, to)
	return nil
}
type dataCloser struct {
	c *Client
	io.WriteCloser
	statusCb func(rcpt string, status *SMTPError)
}
func (d *dataCloser) Close() error {
	d.WriteCloser.Close()
	d.c.conn.SetDeadline(time.Now().Add(d.c.SubmissionTimeout))
	defer d.c.conn.SetDeadline(time.Time{})
	expectedResponses := len(d.c.rcpts)
	if d.c.lmtp {
		for expectedResponses > 0 {
			rcpt := d.c.rcpts[len(d.c.rcpts)-expectedResponses]
			if _, _, err := d.c.Text.ReadResponse(250); err != nil {
				if protoErr, ok := err.(*textproto.Error); ok {
					if d.statusCb != nil {
						d.statusCb(rcpt, toSMTPErr(protoErr))
					}
				} else {
					return err
				}
			} else if d.statusCb != nil {
				d.statusCb(rcpt, nil)
			}
			expectedResponses--
		}
		return nil
	} else {
		_, _, err := d.c.Text.ReadResponse(250)
		if err != nil {
			if protoErr, ok := err.(*textproto.Error); ok {
				return toSMTPErr(protoErr)
			}
			return err
		}
		return nil
	}
}