diff --git a/email.go b/email.go index f0221ab..b982241 100644 --- a/email.go +++ b/email.go @@ -8,10 +8,11 @@ import ( "io/ioutil" "mime" "net/mail" - "net/smtp" "path/filepath" "strings" "time" + + "qoobing.com/gomod/email/smtp" ) // Attachment represents an email attachment. diff --git a/example/aaa.txt b/example/aaa.txt new file mode 100644 index 0000000..fc902f4 --- /dev/null +++ b/example/aaa.txt @@ -0,0 +1 @@ +124 diff --git a/example/go.mod b/example/go.mod new file mode 100644 index 0000000..4c7ad31 --- /dev/null +++ b/example/go.mod @@ -0,0 +1,9 @@ +module example.com + +go 1.23.6 + +require qoobing.com/gomod/email v1.0.0 + +require golang.org/x/net v0.41.0 // indirect + +replace qoobing.com/gomod/email v1.0.0 => ../../email diff --git a/example/go.sum b/example/go.sum new file mode 100644 index 0000000..532fcda --- /dev/null +++ b/example/go.sum @@ -0,0 +1,2 @@ +golang.org/x/net v0.41.0 h1:vBTly1HeNPEn3wtREYfy4GZ/NECgw2Cnl+nK6Nz3uvw= +golang.org/x/net v0.41.0/go.mod h1:B/K4NNqkfmg07DQYrbwvSluqCJOOXwUjeb/5lOisjbA= diff --git a/example/main.go b/example/main.go index b0e504f..48d900a 100644 --- a/example/main.go +++ b/example/main.go @@ -3,16 +3,18 @@ package main import ( "log" "net/mail" - "net/smtp" + "os" "strconv" + "qoobing.com/gomod/email" + "qoobing.com/gomod/email/smtp" ) -func main() { - fromAddr := "ubbeybox@yqtc.com" - fromPass := "yqtc@018ubox" - fromSmtpAddr := "smtp.gmail.com" - fromSmtpPort := 587 //465;//587 +func main1() { + fromAddr := "qbing2010@163.com" + fromPass := "AHu6KX5z2Pk9RndY" + fromSmtpAddr := "smtp.163.com" + fromSmtpPort := 25 //465 //465;//587 toAddr := "q.bryant@live.com" // compose the message m := email.NewMessage("Hi", "this is the body") @@ -20,10 +22,11 @@ func main() { m.To = []string{toAddr} // add attachments - if err := m.Attach("email.go"); err != nil { + if err := m.Attach("aaa.txt"); err != nil { log.Fatal(err) } + log.Println("fromAddr", fromAddr, "fromPass", fromPass, "fromSmtpAddr", fromSmtpAddr) // send it auth := smtp.PlainAuth("", fromAddr, fromPass, fromSmtpAddr) if err := email.Send(fromSmtpAddr+":"+strconv.Itoa(fromSmtpPort), auth, m); err != nil { @@ -32,3 +35,78 @@ func main() { log.Println("done") } } + +func main2() { + fromAddr := "no-reply@pelsee.com" + fromPass := "zoiqezqjofryuinq" + fromSmtpAddr := "smtp.gmail.com" + fromSmtpPort := 587 //465 //465;//587 + toAddr := "q.bryant@live.com" + // compose the message + m := email.NewMessage("Hi", "this is the body") + m.From = mail.Address{Name: "From", Address: fromAddr} + m.To = []string{toAddr} + + // add attachments + if err := m.Attach("aaa.txt"); err != nil { + log.Fatal(err) + } + + log.Println("fromAddr", fromAddr, "fromPass", fromPass, "fromSmtpAddr", fromSmtpAddr) + // send it + auth := smtp.PlainAuth("", fromAddr, fromPass, fromSmtpAddr) + if err := email.Send(fromSmtpAddr+":"+strconv.Itoa(fromSmtpPort), auth, m); err != nil { + log.Fatal(err) + } else { + log.Println("done") + } +} + +func main3() { + fromAddr := "no-reply@pelsee.com" + fromPass := "zoiqezqjofryuinq" + fromSmtpAddr := "smtp.gmail.com" + fromSmtpPort := 587 //465 //465;//587 + fromSocks5Proxy := "socks5://127.0.0.1:1080" + toAddr := "q.bryant@live.com" + // compose the message + m := email.NewMessage("Hi", "this is the body") + m.From = mail.Address{Name: "From", Address: fromAddr} + m.To = []string{toAddr} + + // add attachments + if err := m.Attach("aaa.txt"); err != nil { + log.Fatal(err) + } + + log.Println("fromAddr", fromAddr, "fromPass", fromPass, "fromSmtpAddr", fromSmtpAddr) + // send it + auth := smtp.PlainAuth("", fromAddr, fromPass, fromSmtpAddr) + if err := email.Send(fromSmtpAddr+":"+strconv.Itoa(fromSmtpPort)+"|"+fromSocks5Proxy, auth, m); err != nil { + log.Fatal(err) + } else { + log.Println("done") + } +} + +func main() { + args := os.Args + witchOne := 3 + var err error + if len(args) > 1 { + witchOne, err = strconv.Atoi(args[1]) + if err != nil { + log.Fatal("invalid argument", err) + } + } + switch witchOne { + case 1: + main1() + case 2: + main2() + case 3: + main3() + default: + log.Fatal("invalid argument", witchOne) + } +} diff --git a/go.mod b/go.mod index 172ced3..f5abead 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,7 @@ module qoobing.com/gomod/email -go 1.14 +go 1.23.0 + +toolchain go1.23.6 + +require golang.org/x/net v0.41.0 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..532fcda --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +golang.org/x/net v0.41.0 h1:vBTly1HeNPEn3wtREYfy4GZ/NECgw2Cnl+nK6Nz3uvw= +golang.org/x/net v0.41.0/go.mod h1:B/K4NNqkfmg07DQYrbwvSluqCJOOXwUjeb/5lOisjbA= diff --git a/smtp/auth.go b/smtp/auth.go new file mode 100644 index 0000000..6d461ac --- /dev/null +++ b/smtp/auth.go @@ -0,0 +1,109 @@ +// 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 + +import ( + "crypto/hmac" + "crypto/md5" + "errors" + "fmt" +) + +// Auth is implemented by an SMTP authentication mechanism. +type Auth interface { + // Start begins an authentication with a server. + // It returns the name of the authentication protocol + // and optionally data to include in the initial AUTH message + // sent to the server. + // If it returns a non-nil error, the SMTP client aborts + // the authentication attempt and closes the connection. + Start(server *ServerInfo) (proto string, toServer []byte, err error) + + // Next continues the authentication. The server has just sent + // the fromServer data. If more is true, the server expects a + // response, which Next should return as toServer; otherwise + // Next should return toServer == nil. + // If Next returns a non-nil error, the SMTP client aborts + // the authentication attempt and closes the connection. + Next(fromServer []byte, more bool) (toServer []byte, err error) +} + +// ServerInfo records information about an SMTP server. +type ServerInfo struct { + Name string // SMTP server name + TLS bool // using TLS, with valid certificate for Name + Auth []string // advertised authentication mechanisms +} + +type plainAuth struct { + identity, username, password string + host string +} + +// 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, username, password, host string) Auth { + return &plainAuth{identity, username, password, host} +} + +func isLocalhost(name string) bool { + return name == "localhost" || name == "127.0.0.1" || name == "::1" +} + +func (a *plainAuth) Start(server *ServerInfo) (string, []byte, error) { + // Must have TLS, or else localhost server. + // Note: If TLS is not true, then we can't trust ANYTHING in ServerInfo. + // In particular, it doesn't matter if the server advertises PLAIN auth. + // That might just be the attacker saying + // "it's ok, you can trust me with your password." + if !server.TLS && !isLocalhost(server.Name) { + return "", nil, errors.New("unencrypted connection") + } + if server.Name != a.host { + return "", nil, errors.New("wrong host name") + } + resp := []byte(a.identity + "\x00" + a.username + "\x00" + a.password) + return "PLAIN", resp, nil +} + +func (a *plainAuth) Next(fromServer []byte, more bool) ([]byte, error) { + if more { + // We've already sent everything. + return nil, errors.New("unexpected server challenge") + } + return nil, nil +} + +type cramMD5Auth struct { + username, secret string +} + +// 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, secret string) Auth { + return &cramMD5Auth{username, secret} +} + +func (a *cramMD5Auth) Start(server *ServerInfo) (string, []byte, error) { + return "CRAM-MD5", nil, nil +} + +func (a *cramMD5Auth) Next(fromServer []byte, more bool) ([]byte, error) { + if more { + d := hmac.New(md5.New, []byte(a.secret)) + d.Write(fromServer) + s := make([]byte, 0, d.Size()) + return fmt.Appendf(nil, "%s %x", a.username, d.Sum(s)), nil + } + return nil, nil +} diff --git a/smtp/smtp.go b/smtp/smtp.go new file mode 100644 index 0000000..3ccb96c --- /dev/null +++ b/smtp/smtp.go @@ -0,0 +1,498 @@ +// 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. +// +// The smtp package is frozen and is not accepting new features. +// Some external packages provide more functionality. See: +// +// https://godoc.org/?q=smtp +package smtp + +import ( + "crypto/tls" + "encoding/base64" + "errors" + "fmt" + "io" + "net" + "net/textproto" + "strings" + "time" + + "golang.org/x/net/proxy" +) + +// 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 + 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 +} + +// 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) { + conn, err := net.Dial("tcp", addr) + if err != nil { + return nil, err + } + host, _, _ := net.SplitHostPort(addr) + return NewClient(conn, host) +} + +// Dial returns a new [Client] connected to an SMTP server at addr. +// The addr must include a port, as in "smtp.example.com:smtp". +// The proxyAddr can be in the format of +// +// "socks5://127.0.0.1:1080" or +// "socks5://user:password@127.0.0.1:1080" +func DialWithProxy(addr string, proxyAddr string) (*Client, error) { + if !strings.HasPrefix(proxyAddr, "socks5://") { + return nil, errors.New("proxyAddr must start with socks5://") + } + proxyAddr = strings.TrimPrefix(proxyAddr, "socks5://") + proxyAuth := func() *proxy.Auth { + if strings.Contains(proxyAddr, "@") { + proxyAddrArray := strings.Split(proxyAddr, "@") + proxyAddr = proxyAddrArray[1] + proxyAuthArray := strings.Split(proxyAddrArray[0], ":") + if len(proxyAuthArray) != 2 { + return nil + } + return &proxy.Auth{ + User: proxyAuthArray[0], + Password: proxyAuthArray[1], + } + } + return nil + }() + dialer, err := proxy.SOCKS5("tcp", proxyAddr, proxyAuth, + &net.Dialer{ + Timeout: 30 * time.Second, + }) + if err != nil { + return nil, fmt.Errorf("proxy.SOCKS5 error: %w", err) + } + fmt.Println("dialer=================", dialer) + + conn, err := dialer.Dial("tcp", addr) + if err != nil { + return nil, err + } + host, _, _ := net.SplitHostPort(addr) + 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 + } + c := &Client{Text: text, conn: conn, serverName: host, localName: "localhost"} + _, c.tls = conn.(*tls.Conn) + 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 +// 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 { + 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() +} + +// 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) { + 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 + _, _, err := c.cmd(250, "HELO %s", c.localName) + 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 { + _, msg, err := c.cmd(250, "EHLO %s", 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 { + k, v, _ := strings.Cut(line, " ") + ext[k] = v + } + } + 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 { + if err := c.hello(); err != nil { + return err + } + _, _, 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() +} + +// 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) { + tc, ok := c.conn.(*tls.Conn) + if !ok { + return + } + return tc.ConnectionState(), true +} + +// 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 { + 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 +} + +// 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 { + if err := c.hello(); err != nil { + return err + } + 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) + code, msg64, err := c.cmd(0, "%s", 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: + // the last message isn't base64 because it isn't a challenge + msg = []byte(msg64) + default: + err = &textproto.Error{Code: code, Msg: msg64} + } + if err == nil { + resp, err = a.Next(msg, code == 334) + } + 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, "%s", 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. 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 { + if err := validateLine(from); err != nil { + return err + } + if err := c.hello(); err != nil { + return err + } + cmdStr := "MAIL FROM:<%s>" + if c.ext != nil { + if _, ok := c.ext["8BITMIME"]; ok { + cmdStr += " BODY=8BITMIME" + } + if _, ok := c.ext["SMTPUTF8"]; ok { + cmdStr += " SMTPUTF8" + } + } + _, _, 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 [Client.Mail] and may be followed by +// a [Client.Data] call or another Rcpt call. +func (c *Client) Rcpt(to string) error { + if err := validateLine(to); err != nil { + return err + } + _, _, 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 +// 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) { + _, _, err := c.cmd(354, "DATA") + if err != nil { + return nil, err + } + return &dataCloser{c, c.Text.DotWriter()}, nil +} + +var testHookStartTLS func(*tls.Config) // nil, except for tests + +// 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 can be in the format of +// +// "smtp.example.com:25" or +// "smtp.example.com:25|socks5://127.0.0.1:1080" or +// "smtp.example.com:25|socks5://user:password@127.0.0.1:1080" +// +// The addresses smtp part 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 { + if err := validateLine(from); err != nil { + return err + } + for _, recp := range to { + if err := validateLine(recp); err != nil { + return err + } + } + var c *Client + var err error + var addrArray = strings.Split(addr, "|") + if len(addrArray) == 1 { + addr = addrArray[0] + c, err = Dial(addr) + } else if len(addrArray) == 2 { + addr = addrArray[0] + socks := addrArray[1] + c, err = DialWithProxy(addr, socks) + } else { + // example 1: smtp.example.com:25 + // example 2: smtp.example.com:25|socks5://127.0.0.1:1080 + // example 3: smtp.example.com:25|socks5://user:password@127.0.0.1:1080 + return errors.New("smtp: invalid smtp address format") + } + if err != nil { + return err + } + defer c.Close() + + if err = c.hello(); err != nil { + return err + } + if ok, _ := c.Extension("STARTTLS"); ok { + config := &tls.Config{ServerName: c.serverName} + if testHookStartTLS != nil { + testHookStartTLS(config) + } + if err = c.StartTLS(config); err != nil { + return err + } + } + if a != nil && c.ext != nil { + if _, ok := c.ext["AUTH"]; !ok { + return errors.New("smtp: server doesn't support AUTH") + } + if err = c.Auth(a); err != nil { + return err + } + } + 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) { + if err := c.hello(); err != nil { + return false, "" + } + 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 { + if err := c.hello(); err != nil { + return err + } + _, _, err := c.cmd(250, "RSET") + return err +} + +// 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 +} + +// Quit sends the QUIT command and closes the connection to the server. +func (c *Client) Quit() error { + if err := c.hello(); err != nil { + return err + } + _, _, err := c.cmd(221, "QUIT") + if err != nil { + return err + } + return c.Text.Close() +} + +// 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 +}