| 1 | package mailyakscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/ |
| 2 | |
| 3 | import ( |
| 4 | "bufioscip-go gomod github.com/golang/go/src go1.12 bufio/" |
| 5 | "crypto/tlsscip-go gomod github.com/golang/go/src go1.12 `crypto/tls`/" |
| 6 | "ioscip-go gomod github.com/golang/go/src go1.12 io/" |
| 7 | "netscip-go gomod github.com/golang/go/src go1.12 net/" |
| 8 | "net/smtpscip-go gomod github.com/golang/go/src go1.12 `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 emailSenderscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/emailSender# interface { |
| 14 | Sendscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/emailSender#Send.(m sendableMailscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/sendableMail#) error |
| 15 | } |
| 16 | |
| 17 | // sendableMail provides a set of methods to describe an email to a SMTP server. |
| 18 | type sendableMailscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/sendableMail# interface { |
| 19 | // getLocalName should return the sender domain to be used in the EHLO/HELO |
| 20 | // command. |
| 21 | getLocalNamescip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/sendableMail#getLocalName.() string |
| 22 | |
| 23 | // getToAddrs should return a slice of email addresses to be added to the |
| 24 | // RCPT TO command. |
| 25 | getToAddrsscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/sendableMail#getToAddrs.() []string |
| 26 | |
| 27 | // getFromAddr should return the address to be used in the MAIL FROM |
| 28 | // command. |
| 29 | getFromAddrscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/sendableMail#getFromAddr.() string |
| 30 | |
| 31 | // getAuth should return the smtp.Auth if configured, nil if not. |
| 32 | getAuthscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/sendableMail#getAuth.() smtpscip-go gomod github.com/golang/go/src go1.12 `net/smtp`/.Authscip-go gomod github.com/golang/go/src go1.12 `net/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 | buildMimescip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/sendableMail#buildMime.(w ioscip-go gomod github.com/golang/go/src go1.12 io/.Writerscip-go gomod github.com/golang/go/src go1.12 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 smtpExchangescip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/smtpExchange().(m sendableMailscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/sendableMail#, conn netscip-go gomod github.com/golang/go/src go1.12 net/.Connscip-go gomod github.com/golang/go/src go1.12 net/Conn#, serverName string, tryTLSUpgrade bool) error { |
| 46 | // Connect to the SMTP server |
| 47 | c, err := smtpscip-go gomod github.com/golang/go/src go1.12 `net/smtp`/.NewClientscip-go gomod github.com/golang/go/src go1.12 `net/smtp`/NewClient().(conn, serverName) |
| 48 | if err != nil { |
| 49 | return err |
| 50 | } |
| 51 | defer func() { _ = c.Quitscip-go gomod github.com/golang/go/src go1.12 `net/smtp`/Client#Quit().() }() |
| 52 | |
| 53 | if localName := m.getLocalNamescip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/sendableMail#getLocalName.(); localName != "" { |
| 54 | if err := c.Helloscip-go gomod github.com/golang/go/src go1.12 `net/smtp`/Client#Hello().(localName); err != nil { |
| 55 | return err |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | if tryTLSUpgrade { |
| 60 | if ok, _ := c.Extensionscip-go gomod github.com/golang/go/src go1.12 `net/smtp`/Client#Extension().("STARTTLS"); ok { |
| 61 | //nolint:gosec |
| 62 | config := &tlsscip-go gomod github.com/golang/go/src go1.12 `crypto/tls`/.Configscip-go gomod github.com/golang/go/src go1.12 `crypto/tls`/Config#{ |
| 63 | ServerNamescip-go gomod github.com/golang/go/src go1.12 `crypto/tls`/Config#ServerName.: serverName, |
| 64 | } |
| 65 | if err = c.StartTLSscip-go gomod github.com/golang/go/src go1.12 `net/smtp`/Client#StartTLS().(config); err != nil { |
| 66 | return err |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | // Attempt to authenticate if credentials were provided |
| 72 | var nilAuth smtpscip-go gomod github.com/golang/go/src go1.12 `net/smtp`/.Authscip-go gomod github.com/golang/go/src go1.12 `net/smtp`/Auth# |
| 73 | if auth := m.getAuthscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/sendableMail#getAuth.(); auth != nilAuth { |
| 74 | if err = c.Authscip-go gomod github.com/golang/go/src go1.12 `net/smtp`/Client#Auth().(auth); err != nil { |
| 75 | return err |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | // Set the from address |
| 80 | if err = c.Mailscip-go gomod github.com/golang/go/src go1.12 `net/smtp`/Client#Mail().(m.getFromAddrscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/sendableMail#getFromAddr.()); err != nil { |
| 81 | return err |
| 82 | } |
| 83 | |
| 84 | // Add all the recipients |
| 85 | for _, to := range m.getToAddrsscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/sendableMail#getToAddrs.() { |
| 86 | if err = c.Rcptscip-go gomod github.com/golang/go/src go1.12 `net/smtp`/Client#Rcpt().(to); err != nil { |
| 87 | return err |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | // Start the data session and write the email body |
| 92 | dataSession, err := c.Datascip-go gomod github.com/golang/go/src go1.12 `net/smtp`/Client#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 := bufioscip-go gomod github.com/golang/go/src go1.12 bufio/.NewWriterscip-go gomod github.com/golang/go/src go1.12 bufio/NewWriter().(dataSession) |
| 100 | if err := m.buildMimescip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/sendableMail#buildMime.(buf); err != nil { |
| 101 | return err |
| 102 | } |
| 103 | if err := buf.Flushscip-go gomod github.com/golang/go/src go1.12 bufio/Writer#Flush().(); err != nil { |
| 104 | return err |
| 105 | } |
| 106 | |
| 107 | return dataSession.Closescip-go gomod github.com/golang/go/src go1.12 io/Closer#Close().() |
| 108 | } |