mirror of
https://github.com/kataras/iris.git
synced 2025-02-02 07:20:35 +01:00
Try #1 Sending mails though linux mail command - Untested: https://github.com/kataras/iris/issues/166
This commit is contained in:
parent
126a2b2f7c
commit
b58c16113a
|
@ -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
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user