E-mail through linux command with UseCommand configuration field

This commit is contained in:
Makis Maropoulos 2016-06-02 23:29:29 +03:00
parent b58c16113a
commit 679b707751
3 changed files with 9 additions and 4 deletions

View File

@ -22,3 +22,8 @@ type Mail struct {
func DefaultMail() Mail { func DefaultMail() Mail {
return 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
}

View File

@ -176,7 +176,7 @@ func (s *Iris) initWebsocketServer() {
func (s *Iris) initMailService() { func (s *Iris) initMailService() {
if s.mailService == nil { if s.mailService == nil {
// enable mail sender service if configs are valid // 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) s.mailService = mail.New(s.config.Mail)
} }
} }

View File

@ -49,8 +49,8 @@ func (m *mailer) sendSMTP(to []string, subject, body string) error {
defer buf.Put(buffer) defer buf.Put(buffer)
if !m.authenticated { if !m.authenticated {
if m.config.Username == "" || m.config.Password == "" || m.config.Host == "" { if m.config.Username == "" || m.config.Password == "" || m.config.Host == "" || m.config.Port <= 0 {
return fmt.Errorf("Username, Password, Host cannot be empty when using SMTP!") 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.auth = smtp.PlainAuth("", m.config.Username, m.config.Password, m.config.Host)
m.authenticated = true 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 { func (m *mailer) sendCmd(to []string, subject, body string) error {
buffer := buf.Get() buffer := buf.Get()
defer buf.Put(buffer) defer buf.Put(buffer)
// buffer.WriteString(body)
cmd := utils.CommandBuilder("mail", "-s", subject, strings.Join(to, ",")) cmd := utils.CommandBuilder("mail", "-s", subject, strings.Join(to, ","))
cmd.AppendArguments("-a", "Content-type: text/html") //always html on cmd.AppendArguments("-a", "Content-type: text/html") //always html on