github.com/mika/distribution@v2.2.2-0.20160108133430-a75790e3d8e0+incompatible/registry/handlers/mail.go (about) 1 package handlers 2 3 import ( 4 "errors" 5 "net/smtp" 6 "strings" 7 ) 8 9 // mailer provides fields of email configuration for sending. 10 type mailer struct { 11 Addr, Username, Password, From string 12 Insecure bool 13 To []string 14 } 15 16 // sendMail allows users to send email, only if mail parameters is configured correctly. 17 func (mail *mailer) sendMail(subject, message string) error { 18 addr := strings.Split(mail.Addr, ":") 19 if len(addr) != 2 { 20 return errors.New("Invalid Mail Address") 21 } 22 host := addr[0] 23 msg := []byte("To:" + strings.Join(mail.To, ";") + 24 "\r\nFrom: " + mail.From + 25 "\r\nSubject: " + subject + 26 "\r\nContent-Type: text/plain\r\n\r\n" + 27 message) 28 auth := smtp.PlainAuth( 29 "", 30 mail.Username, 31 mail.Password, 32 host, 33 ) 34 err := smtp.SendMail( 35 mail.Addr, 36 auth, 37 mail.From, 38 mail.To, 39 []byte(msg), 40 ) 41 if err != nil { 42 return err 43 } 44 return nil 45 }