diff options
author | Steve Manuel <nilslice@gmail.com> | 2016-11-09 18:09:36 -0800 |
---|---|---|
committer | Steve Manuel <nilslice@gmail.com> | 2016-11-09 18:09:36 -0800 |
commit | e70c231df82f4e5f09869f8d570d809a5f70993c (patch) | |
tree | e1a0c76ab0483c29b78ba7c0458f5f59b5ca0598 /cmd/ponzu/vendor/github.com/nilslice/email/email.go | |
parent | ea3d29602be45c49629c2a8b6b8199e4f9e9076d (diff) |
adding initial partial implementation of account recovery flow
Diffstat (limited to 'cmd/ponzu/vendor/github.com/nilslice/email/email.go')
-rw-r--r-- | cmd/ponzu/vendor/github.com/nilslice/email/email.go | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/cmd/ponzu/vendor/github.com/nilslice/email/email.go b/cmd/ponzu/vendor/github.com/nilslice/email/email.go new file mode 100644 index 0000000..ed843e3 --- /dev/null +++ b/cmd/ponzu/vendor/github.com/nilslice/email/email.go @@ -0,0 +1,114 @@ +package email + +import ( + "fmt" + "net" + "net/smtp" + "strings" +) + +// Message creates a email to be sent +type Message struct { + To string + From string + Subject string + Body string +} + +var ( + ports = []int{25, 2525, 587} +) + +// Send sends a message to recipient(s) listed in the 'To' field of a Message +func (m Message) Send() error { + if !strings.Contains(m.To, "@") { + return fmt.Errorf("Invalid recipient address: <%s>", m.To) + } + + host := strings.Split(m.To, "@")[1] + addrs, err := net.LookupMX(host) + if err != nil { + return err + } + + c, err := newClient(addrs, ports) + if err != nil { + return err + } + + err = send(m, c) + if err != nil { + return err + } + + return nil +} + +func newClient(mx []*net.MX, ports []int) (*smtp.Client, error) { + for i := range mx { + for j := range ports { + server := strings.TrimSuffix(mx[i].Host, ".") + hostPort := fmt.Sprintf("%s:%d", server, ports[j]) + client, err := smtp.Dial(hostPort) + if err != nil { + if j == len(ports)-1 { + return nil, err + } + + continue + } + + return client, nil + } + } + + return nil, fmt.Errorf("Coudln't connect to servers %v on any common port.", mx) +} + +func send(m Message, c *smtp.Client) error { + if err := c.Mail(m.From); err != nil { + return err + } + + if err := c.Rcpt(m.To); err != nil { + return err + } + + msg, err := c.Data() + if err != nil { + return err + } + + if m.Subject != "" { + _, err = msg.Write([]byte("Subject: " + m.Subject + "\r\n")) + if err != nil { + return err + } + } + + if m.From != "" { + _, err = msg.Write([]byte("From: " + m.From + "\r\n")) + if err != nil { + return err + } + } + + if m.To != "" { + _, err = msg.Write([]byte("To: " + m.To + "\r\n")) + if err != nil { + return err + } + } + + _, err = fmt.Fprint(msg, m.Body) + if err != nil { + return err + } + + err = msg.Close() + if err != nil { + return err + } + + return nil +} |