-
Notifications
You must be signed in to change notification settings - Fork 75
/
sender_starttls.go
47 lines (41 loc) · 1.37 KB
/
sender_starttls.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package mailyak
import (
"bytes"
"net"
)
// senderWithStartTLS connects to the remote SMTP server, upgrades the
// connection using STARTTLS if available, and sends the email.
type senderWithStartTLS struct {
hostAndPort string
hostname string
buf *bytes.Buffer
}
func (s *senderWithStartTLS) Send(m sendableMail) error {
conn, err := net.Dial("tcp", s.hostAndPort)
if err != nil {
return err
}
defer func() { _ = conn.Close() }()
return smtpExchange(m, conn, s.hostname, true)
}
func newSenderWithStartTLS(hostAndPort string) *senderWithStartTLS {
hostName, _, err := net.SplitHostPort(hostAndPort)
if err != nil {
// Really this should be an error, but we can't return it from the New()
// constructor without breaking compatability. Fortunately by the time
// it gets to the dial() the user will get a pretty clear error as this
// hostAndPort value is almost certainly invalid.
//
// This hostname must be split from the port so the correct value is
// used when performing the SMTP AUTH as the Go SMTP implementation
// refuses to send credentials over non-localhost plaintext connections,
// and including the port messes this check up (and is probably the
// wrong thing to be sending anyway).
hostName = hostAndPort
}
return &senderWithStartTLS{
hostAndPort: hostAndPort,
hostname: hostName,
buf: &bytes.Buffer{},
}
}