1 // Copyright 2010 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Package smtp implements the Simple Mail Transfer Protocol as defined in RFC 5321.
6 // It also implements the following extensions:
10 // Additional extensions may be handled by clients.
22 // A Client represents a client connection to an SMTP server.
24 // Text is the textproto.Conn used by the Client. It is exported to allow for
25 // clients to add extensions.
27 // keep a reference to the connection so it can be used to create a TLS
30 // whether the Client is using TLS
33 // map of supported extensions
35 // supported auth mechanisms
39 // Dial returns a new Client connected to an SMTP server at addr.
40 func Dial(addr string) (*Client, error) {
41 conn, err := net.Dial("tcp", addr)
45 host := addr[:strings.Index(addr, ":")]
46 return NewClient(conn, host)
49 // NewClient returns a new Client using an existing connection and host as a
50 // server name to be used when authenticating.
51 func NewClient(conn net.Conn, host string) (*Client, error) {
52 text := textproto.NewConn(conn)
53 _, msg, err := text.ReadResponse(220)
58 c := &Client{Text: text, conn: conn, serverName: host}
59 if strings.Contains(msg, "ESMTP") {
67 // cmd is a convenience function that sends a command and returns the response
68 func (c *Client) cmd(expectCode int, format string, args ...interface{}) (int, string, error) {
69 id, err := c.Text.Cmd(format, args...)
73 c.Text.StartResponse(id)
74 defer c.Text.EndResponse(id)
75 code, msg, err := c.Text.ReadResponse(expectCode)
79 // helo sends the HELO greeting to the server. It should be used only when the
80 // server does not support ehlo.
81 func (c *Client) helo() error {
83 _, _, err := c.cmd(250, "HELO localhost")
87 // ehlo sends the EHLO (extended hello) greeting to the server. It
88 // should be the preferred greeting for servers that support it.
89 func (c *Client) ehlo() error {
90 _, msg, err := c.cmd(250, "EHLO localhost")
94 ext := make(map[string]string)
95 extList := strings.Split(msg, "\n")
98 for _, line := range extList {
99 args := strings.SplitN(line, " ", 2)
101 ext[args[0]] = args[1]
107 if mechs, ok := ext["AUTH"]; ok {
108 c.auth = strings.Split(mechs, " ")
114 // StartTLS sends the STARTTLS command and encrypts all further communication.
115 // Only servers that advertise the STARTTLS extension support this function.
116 func (c *Client) StartTLS(config *tls.Config) error {
117 _, _, err := c.cmd(220, "STARTTLS")
121 c.conn = tls.Client(c.conn, config)
122 c.Text = textproto.NewConn(c.conn)
127 // Verify checks the validity of an email address on the server.
128 // If Verify returns nil, the address is valid. A non-nil return
129 // does not necessarily indicate an invalid address. Many servers
130 // will not verify addresses for security reasons.
131 func (c *Client) Verify(addr string) error {
132 _, _, err := c.cmd(250, "VRFY %s", addr)
136 // Auth authenticates a client using the provided authentication mechanism.
137 // A failed authentication closes the connection.
138 // Only servers that advertise the AUTH extension support this function.
139 func (c *Client) Auth(a Auth) error {
140 encoding := base64.StdEncoding
141 mech, resp, err := a.Start(&ServerInfo{c.serverName, c.tls, c.auth})
146 resp64 := make([]byte, encoding.EncodedLen(len(resp)))
147 encoding.Encode(resp64, resp)
148 code, msg64, err := c.cmd(0, "AUTH %s %s", mech, resp64)
153 msg, err = encoding.DecodeString(msg64)
155 // the last message isn't base64 because it isn't a challenge
158 err = &textproto.Error{code, msg64}
160 resp, err = a.Next(msg, code == 334)
170 resp64 = make([]byte, encoding.EncodedLen(len(resp)))
171 encoding.Encode(resp64, resp)
172 code, msg64, err = c.cmd(0, string(resp64))
177 // Mail issues a MAIL command to the server using the provided email address.
178 // If the server supports the 8BITMIME extension, Mail adds the BODY=8BITMIME
180 // This initiates a mail transaction and is followed by one or more Rcpt calls.
181 func (c *Client) Mail(from string) error {
182 cmdStr := "MAIL FROM:<%s>"
184 if _, ok := c.ext["8BITMIME"]; ok {
185 cmdStr += " BODY=8BITMIME"
188 _, _, err := c.cmd(250, cmdStr, from)
192 // Rcpt issues a RCPT command to the server using the provided email address.
193 // A call to Rcpt must be preceded by a call to Mail and may be followed by
194 // a Data call or another Rcpt call.
195 func (c *Client) Rcpt(to string) error {
196 _, _, err := c.cmd(25, "RCPT TO:<%s>", to)
200 type dataCloser struct {
205 func (d *dataCloser) Close() error {
206 d.WriteCloser.Close()
207 _, _, err := d.c.Text.ReadResponse(250)
211 // Data issues a DATA command to the server and returns a writer that
212 // can be used to write the data. The caller should close the writer
213 // before calling any more methods on c.
214 // A call to Data must be preceded by one or more calls to Rcpt.
215 func (c *Client) Data() (io.WriteCloser, error) {
216 _, _, err := c.cmd(354, "DATA")
220 return &dataCloser{c, c.Text.DotWriter()}, nil
223 // SendMail connects to the server at addr, switches to TLS if possible,
224 // authenticates with mechanism a if possible, and then sends an email from
225 // address from, to addresses to, with message msg.
226 func SendMail(addr string, a Auth, from string, to []string, msg []byte) error {
231 if ok, _ := c.Extension("STARTTLS"); ok {
232 if err = c.StartTLS(nil); err != nil {
236 if a != nil && c.ext != nil {
237 if _, ok := c.ext["AUTH"]; ok {
238 if err = c.Auth(a); err != nil {
243 if err = c.Mail(from); err != nil {
246 for _, addr := range to {
247 if err = c.Rcpt(addr); err != nil {
255 _, err = w.Write(msg)
266 // Extension reports whether an extension is support by the server.
267 // The extension name is case-insensitive. If the extension is supported,
268 // Extension also returns a string that contains any parameters the
269 // server specifies for the extension.
270 func (c *Client) Extension(ext string) (bool, string) {
274 ext = strings.ToUpper(ext)
275 param, ok := c.ext[ext]
279 // Reset sends the RSET command to the server, aborting the current mail
281 func (c *Client) Reset() error {
282 _, _, err := c.cmd(250, "RSET")
286 // Quit sends the QUIT command and closes the connection to the server.
287 func (c *Client) Quit() error {
288 _, _, err := c.cmd(221, "QUIT")
292 return c.Text.Close()