diff --git a/config/mail.go b/config/mail.go index 6927dc4d..c0e6bb66 100644 --- a/config/mail.go +++ b/config/mail.go @@ -22,3 +22,8 @@ type Mail struct { func DefaultMail() Mail { return Mail{} } + +// IsValid returns true if the mail configs are valid +func (m Mail) IsValid() bool { + return (m.Host != "" && m.Port > 0 && m.Username != "" && m.Password != "") || m.UseCommand +} diff --git a/iris.go b/iris.go index 09f24c8a..00dfcb09 100644 --- a/iris.go +++ b/iris.go @@ -176,7 +176,7 @@ func (s *Iris) initWebsocketServer() { func (s *Iris) initMailService() { if s.mailService == nil { // enable mail sender service if configs are valid - if s.config.Mail.Host != "" && s.config.Mail.Username != "" && s.config.Mail.Password != "" { + if s.config.Mail.IsValid() { s.mailService = mail.New(s.config.Mail) } } diff --git a/mail/service.go b/mail/service.go index 87f15369..9012cf28 100644 --- a/mail/service.go +++ b/mail/service.go @@ -49,8 +49,8 @@ func (m *mailer) sendSMTP(to []string, subject, body string) error { defer buf.Put(buffer) if !m.authenticated { - if m.config.Username == "" || m.config.Password == "" || m.config.Host == "" { - return fmt.Errorf("Username, Password, Host cannot be empty when using SMTP!") + if m.config.Username == "" || m.config.Password == "" || m.config.Host == "" || m.config.Port <= 0 { + return fmt.Errorf("Username, Password, Host & Port cannot be empty when using SMTP!") } m.auth = smtp.PlainAuth("", m.config.Username, m.config.Password, m.config.Host) m.authenticated = true @@ -75,7 +75,7 @@ func (m *mailer) sendSMTP(to []string, subject, body string) error { func (m *mailer) sendCmd(to []string, subject, body string) error { buffer := buf.Get() defer buf.Put(buffer) - // buffer.WriteString(body) + cmd := utils.CommandBuilder("mail", "-s", subject, strings.Join(to, ",")) cmd.AppendArguments("-a", "Content-type: text/html") //always html on