Sending email
This commit is contained in:
@ -25,6 +25,12 @@ type config struct {
|
||||
secret string
|
||||
key string
|
||||
}
|
||||
smtp struct {
|
||||
host string
|
||||
port int
|
||||
username string
|
||||
password string
|
||||
}
|
||||
}
|
||||
|
||||
type application struct {
|
||||
@ -69,6 +75,10 @@ func main() {
|
||||
"root:example@tcp(localhost:3306)/widgets?parseTime=true&tls=false",
|
||||
"Application environment {development|production}",
|
||||
)
|
||||
flag.StringVar(&cfg.smtp.host, "smtphost", "0.0.0.0", "smtp host")
|
||||
flag.IntVar(&cfg.smtp.port, "smtpport", 1025, "smtp host")
|
||||
flag.StringVar(&cfg.smtp.username, "smtpuser", "user", "smtp user")
|
||||
flag.StringVar(&cfg.smtp.password, "smtppwd", "password", "smtp password")
|
||||
|
||||
flag.Parse()
|
||||
|
||||
|
@ -5,6 +5,9 @@ import (
|
||||
"embed"
|
||||
"fmt"
|
||||
"text/template"
|
||||
"time"
|
||||
|
||||
mail "github.com/xhit/go-simple-mail/v2"
|
||||
)
|
||||
|
||||
//go:embed templates
|
||||
@ -42,5 +45,34 @@ func (app *application) SendMail(from, to, subject, tmpl string, data interface{
|
||||
|
||||
app.infoLog.Println(formattedMessage, plainMessage)
|
||||
|
||||
// send the mail
|
||||
server := mail.NewSMTPClient()
|
||||
server.Host = app.config.smtp.host
|
||||
server.Port = app.config.smtp.port
|
||||
// NOTE: not needed for MailHog
|
||||
// server.Username = app.config.smtp.username
|
||||
// server.Password = app.config.smtp.password
|
||||
// server.Encryption = mail.EncryptionTLS
|
||||
server.KeepAlive = false
|
||||
server.ConnectTimeout = 10 * time.Second
|
||||
server.SendTimeout = 10 * time.Second
|
||||
|
||||
smtpClient, err := server.Connect()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
email := mail.NewMSG()
|
||||
email.SetFrom(from).AddTo(to).SetSubject(subject)
|
||||
email.SetBody(mail.TextHTML, formattedMessage)
|
||||
email.AddAlternative(mail.TextPlain, plainMessage)
|
||||
|
||||
err = email.Send(smtpClient)
|
||||
if err != nil {
|
||||
app.errorLog.Println(err)
|
||||
return err
|
||||
}
|
||||
|
||||
app.infoLog.Println("send mail")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user