forked from TrueCloudLab/distribution
47aa47e3f6
This PR is for issue of "email after registry webapp panic" #41, improving my previous design (closed). It use self setting up hooks, to catch panic in web application. And, send email in hooks handle directly, to no use new http server and handler. Signed-off-by: xiekeyang <keyangxie@126.com>
45 lines
915 B
Go
45 lines
915 B
Go
package handlers
|
|
|
|
import (
|
|
"errors"
|
|
"net/smtp"
|
|
"strings"
|
|
)
|
|
|
|
// mailer provides fields of email configuration for sending.
|
|
type mailer struct {
|
|
Addr, Username, Password, From string
|
|
Insecure bool
|
|
To []string
|
|
}
|
|
|
|
// sendMail allows users to send email, only if mail parameters is configured correctly.
|
|
func (mail *mailer) sendMail(subject, message string) error {
|
|
addr := strings.Split(mail.Addr, ":")
|
|
if len(addr) != 2 {
|
|
return errors.New("Invalid Mail Address")
|
|
}
|
|
host := addr[0]
|
|
msg := []byte("To:" + strings.Join(mail.To, ";") +
|
|
"\r\nFrom: " + mail.From +
|
|
"\r\nSubject: " + subject +
|
|
"\r\nContent-Type: text/plain\r\n\r\n" +
|
|
message)
|
|
auth := smtp.PlainAuth(
|
|
"",
|
|
mail.Username,
|
|
mail.Password,
|
|
host,
|
|
)
|
|
err := smtp.SendMail(
|
|
mail.Addr,
|
|
auth,
|
|
mail.From,
|
|
mail.To,
|
|
[]byte(msg),
|
|
)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|