123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448 |
- package emailer
- import (
- "crypto/tls"
- "encoding/base64"
- "errors"
- "fmt"
- "io"
- "net"
- "net/textproto"
- "strings"
- "time"
- )
- type Client struct {
-
-
- Text *textproto.Conn
-
-
- conn net.Conn
-
- tls bool
- serverName string
-
- ext map[string]string
-
- auth []string
- localName string
- didHello bool
- helloError error
- }
- func Dial(addr string, tlsEnabled bool) (cl *Client, err error) {
- host, _, _ := net.SplitHostPort(addr)
- var conn net.Conn
- if tlsEnabled {
- tlsconfig := &tls.Config{
- InsecureSkipVerify: true,
- ServerName: host,
- }
- if conn, err = tls.Dial("tcp", addr, tlsconfig); err != nil {
- return
- }
- } else {
- if conn, err = net.DialTimeout("tcp", addr, time.Second*10); err != nil {
- return
- }
- }
- return NewClient(conn, host)
- }
- func NewClient(conn net.Conn, host string) (*Client, error) {
- text := textproto.NewConn(conn)
-
- conn.SetReadDeadline(time.Now().Add(time.Second * 5))
- _, _, 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
- }
- 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 ...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
- }
- func (c *Client) helo() error {
- c.ext = nil
- _, _, err := c.cmd(250, "HELO %s", c.localName)
- return err
- }
- 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
- }
- func (c *Client) StartTLS(config *tls.Config) error {
- if err := c.hello(); err != nil {
- return err
- }
- c.tls = true
-
- 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 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, 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 = &textproto.Error{Code: code, Msg: msg64}
- }
- if err == nil {
- resp, err = a.Next(msg, code == 334)
- }
- if err != nil {
-
- 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
- }
- 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
- }
- 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
- }
- 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
- 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
- }
- }
- c, err := Dial(addr, a.IsTLS())
- if err != nil {
- return err
- }
- defer c.Close()
- if err = c.hello(); 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()
- }
- 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
- }
- func (c *Client) Reset() error {
- if err := c.hello(); err != nil {
- return err
- }
- _, _, err := c.cmd(250, "RSET")
- return err
- }
- func (c *Client) Noop() error {
- if err := c.hello(); err != nil {
- return err
- }
- _, _, err := c.cmd(250, "NOOP")
- return err
- }
- 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()
- }
- 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
- }
|