mirror of
https://github.com/semaphoreui/semaphore.git
synced 2024-11-27 02:52:14 +01:00
17fa7bb407
extract some error checking and logging in places where linting needed or errors not checked
49 lines
753 B
Go
49 lines
753 B
Go
package util
|
|
|
|
import (
|
|
"bytes"
|
|
"net/smtp"
|
|
log "github.com/Sirupsen/logrus"
|
|
"io"
|
|
)
|
|
|
|
// SendMail dispatches a mail using smtp
|
|
func SendMail(emailHost, mailSender, mailRecipient string, mail bytes.Buffer) error {
|
|
c, err := smtp.Dial(emailHost)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
defer func (c *smtp.Client) {
|
|
err = c.Close()
|
|
if err != nil {
|
|
log.Error(err)
|
|
}
|
|
}(c)
|
|
|
|
// Set the sender and recipient.
|
|
err = c.Mail(mailSender)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = c.Rcpt(mailRecipient)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Send the email body.
|
|
wc, err := c.Data()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
defer func (wc io.WriteCloser) {
|
|
err = wc.Close()
|
|
if err != nil {
|
|
log.Error(err)
|
|
}
|
|
}(wc)
|
|
_, err = mail.WriteTo(wc)
|
|
return err
|
|
}
|