30 lines
589 B
Go
30 lines
589 B
Go
package email
|
|
|
|
import (
|
|
"fmt"
|
|
"net/smtp"
|
|
"strings"
|
|
)
|
|
|
|
type Mail struct {
|
|
Sender string
|
|
To []string
|
|
Subject string
|
|
Body string
|
|
}
|
|
|
|
func (mail Mail) Compose() string {
|
|
msg := fmt.Sprintf("From: %s\r\n", mail.Sender)
|
|
msg += fmt.Sprintf("To: %s\r\n", strings.Join(mail.To, ";"))
|
|
msg += fmt.Sprintf("Subject: %s\r\n", mail.Subject)
|
|
msg += fmt.Sprintf("\r\n%s\r\n", mail.Body)
|
|
return msg
|
|
}
|
|
|
|
func (mail Mail) SendMail(addr string, a smtp.Auth) error {
|
|
if err := smtp.SendMail(addr, a, mail.Sender, mail.To, []byte(mail.Compose())); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|