| 1 | package mailyak |
| 2 | |
| 3 | import ( |
| 4 | "bufio" |
| 5 | "crypto/tls" |
| 6 | "io" |
| 7 | "net" |
| 8 | "net/smtp" |
| 9 | ) |
| 10 | |
| 11 | // emailSender abstracts the connection and protocol conversation required to |
| 12 | // send an email with a remote SMTP server. |
| 13 | type emailSender interface { |
| 14 | Send(m sendableMail) error |
| 15 | } |
| 16 | |
| 17 | // sendableMail provides a set of methods to describe an email to a SMTP server. |
| 18 | type sendableMail interface { |
| 19 | // getLocalName should return the sender domain to be used in the EHLO/HELO |
| 20 | // command. |
| 21 | getLocalName() string |
| 22 | |
| 23 | // getToAddrs should return a slice of email addresses to be added to the |
| 24 | // RCPT TO command. |
| 25 | getToAddrs() []string |
| 26 | |
| 27 | // getFromAddr should return the address to be used in the MAIL FROM |
| 28 | // command. |
| 29 | getFromAddr() string |
| 30 | |
| 31 | // getAuth should return the smtp.Auth if configured, nil if not. |
| 32 | getAuth() smtp.Auth |
| 33 | |
| 34 | // buildMime should write the generated MIME to w. |
| 35 | // |
| 36 | // The emailSender implementation is responsible for providing appropriate |
| 37 | // buffering of writes. |
| 38 | buildMime(w io.Writer) error |
| 39 | } |
| 40 | |
| 41 | // smtpExchange performs the SMTP protocol conversation necessary to send m over |
| 42 | // conn. |
| 43 | // |
| 44 | // serverName must be the hostname (or IP address) of the remote endpoint. |
| 45 | func smtpExchange(m sendableMail, conn net.Conn, serverName string, tryTLSUpgrade bool) error { |
| 46 | // Connect to the SMTP server |
| 47 | c, err := smtp.NewClient(conn, serverName) |
| 48 | if err != nil { |
| 49 | return err |
| 50 | } |
| 51 | defer func() { _ = c.Quit() }() |
| 52 | |
| 53 | if localName := m.getLocalName(); localName != "" { |
| 54 | if err := c.Hello(localName); err != nil { |
| 55 | return err |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | if tryTLSUpgrade { |
| 60 | if ok, _ := c.Extension("STARTTLS"); ok { |
| 61 | //nolint:gosec |
| 62 | config := &tls.Config{ |
| 63 | ServerName: serverName, |
| 64 | } |
| 65 | if err = c.StartTLS(config); err != nil { |
| 66 | return err |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | // Attempt to authenticate if credentials were provided |
| 72 | var nilAuth smtp.Auth |
| 73 | if auth := m.getAuth(); auth != nilAuth { |
| 74 | if err = c.Auth(auth); err != nil { |
| 75 | return err |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | // Set the from address |
| 80 | if err = c.Mail(m.getFromAddr()); err != nil { |
| 81 | return err |
| 82 | } |
| 83 | |
| 84 | // Add all the recipients |
| 85 | for _, to := range m.getToAddrs() { |
| 86 | if err = c.Rcpt(to); err != nil { |
| 87 | return err |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | // Start the data session and write the email body |
| 92 | dataSession, err := c.Data() |
| 93 | if err != nil { |
| 94 | return err |
| 95 | } |
| 96 | |
| 97 | // Wrap the socket in a small buffer (~4k) to avoid making lots of small |
| 98 | // syscalls and therefore reducing CPU usage. |
| 99 | buf := bufio.NewWriter(dataSession) |
| 100 | if err := m.buildMime(buf); err != nil { |
| 101 | return err |
| 102 | } |
| 103 | if err := buf.Flush(); err != nil { |
| 104 | return err |
| 105 | } |
| 106 | |
| 107 | return dataSession.Close() |
| 108 | } |