udemy-go-web-1/cmd/web/send-mail.go

44 lines
752 B
Go

package main
import (
"go-udemy-web-1/internal/models"
"log"
"time"
mail "github.com/xhit/go-simple-mail"
)
func listenForMail() {
go func() {
for {
msg := <-app.MailChan
sendMsg(msg)
}
}()
}
func sendMsg(m models.MailData) {
server := mail.NewSMTPClient()
server.Host = "localhost"
server.Port = 1025 // fake port
server.KeepAlive = false
server.ConnectTimeout = 10 * time.Second
server.SendTimeout = 10 * time.Second
client, err := server.Connect()
if err != nil {
errorLog.Println(err)
}
email := mail.NewMSG()
email.SetFrom(m.From).AddTo(m.To).SetSubject(m.Subject)
email.SetBody(mail.TextHTML, m.Content)
err = email.Send(client)
if err != nil {
log.Println(err)
} else {
log.Println("Email sent")
}
}