app/email/email.go

64 lines
1.3 KiB
Go

package email
import (
"github.com/spf13/viper"
"gopkg.in/gomail.v2"
)
func init() {
// viper.SetDefault("email.type", "smtp")
viper.SetDefault("email.host", "mail.kumoly.io")
viper.SetDefault("email.port", 587)
viper.SetDefault("email.username", "test@kumoly.io")
viper.SetDefault("email.password", "test")
viper.SetDefault("logo", "http://www.duchess-france.org/wp-content/uploads/2016/01/gopher.png")
}
type App struct {
Name string
Link string
Logo string
Copyright string
}
type Body struct {
// App replaced with global config
App App
Intros []string
Content string
Outros []string
// Greeting ignored if empty
Greeting string
// Receiver ignored if Greeting or Receiver empty
Receiver string
// Signature default `Best Regards`
Signature string
// Sender default {{.App.Name}}
Sender string
}
// host string, port int, username string, password string
var dial *gomail.Dialer
func Send(From string, To []string, Title string, b *Body) error {
m := gomail.NewMessage()
m.SetHeader("From", From)
m.SetHeader("To", To...)
m.SetHeader("Subject", Title)
// m.SetAddressHeader("Cc", "dan@example.com", "Dan")
// m.Attach("/home/Alex/lolcat.jpg")
body, err := Parse(b)
if err != nil {
return err
}
m.SetBody("text/html", body)
// Send the email
return dial.DialAndSend(m)
}