diff --git a/config/mail.go b/config/mail.go index 72a2ce86..6927dc4d 100644 --- a/config/mail.go +++ b/config/mail.go @@ -10,6 +10,11 @@ type Mail struct { Username string // Password is the auth password for the sender Password string + // UseCommand enable it if you want to send e-mail with the mail command instead of smtp + // + // Host,Port & Password will be ignored + // ONLY FOR UNIX + UseCommand bool } // DefaultMail returns the default configs for Mail diff --git a/mail/service.go b/mail/service.go index a60dbfdb..87f15369 100644 --- a/mail/service.go +++ b/mail/service.go @@ -34,25 +34,26 @@ func New(cfg config.Mail) Service { return &mailer{config: cfg} } -func (m *mailer) authenticate() error { - if m.config.Username == "" || m.config.Password == "" || m.config.Host == "" { - return fmt.Errorf("Username, Password & Host cannot be empty!") - } - m.auth = smtp.PlainAuth("", m.config.Username, m.config.Password, m.config.Host) - m.authenticated = true - return nil -} - // Send sends a mail to recipients // the body can be html also func (m *mailer) Send(to []string, subject, body string) error { + if m.config.UseCommand { + return m.sendCmd(to, subject, body) + } + + return m.sendSMTP(to, subject, body) +} + +func (m *mailer) sendSMTP(to []string, subject, body string) error { buffer := buf.Get() defer buf.Put(buffer) if !m.authenticated { - if err := m.authenticate(); err != nil { - return err + if m.config.Username == "" || m.config.Password == "" || m.config.Host == "" { + return fmt.Errorf("Username, Password, Host cannot be empty when using SMTP!") } + m.auth = smtp.PlainAuth("", m.config.Username, m.config.Password, m.config.Host) + m.authenticated = true } mailArgs := map[string]string{"To": strings.Join(to, ","), "Subject": subject, "Body": body} @@ -70,3 +71,15 @@ func (m *mailer) Send(to []string, subject, body string) error { buffer.Bytes(), ) } + +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 + + cmd.Stdin = buffer + _, err := cmd.Output() + return err +}