2023-09-08 02:26:00 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/joho/godotenv"
|
|
|
|
"gopkg.in/gomail.v2"
|
|
|
|
)
|
|
|
|
|
2024-02-28 11:53:39 +01:00
|
|
|
// SendEmail sends an email to the email address configured in the .env file
|
|
|
|
// with the supplied body.
|
2023-09-08 03:03:57 +02:00
|
|
|
func (app *application) SendEmail(subject, body string) {
|
2023-09-08 02:26:00 +02:00
|
|
|
err := godotenv.Load()
|
|
|
|
if err != nil {
|
|
|
|
app.Log.Fatal("Error loading .env")
|
|
|
|
}
|
|
|
|
hostname := os.Getenv("EMAIL_HOST")
|
|
|
|
login := os.Getenv("EMAIL_LOGIN")
|
|
|
|
password := os.Getenv("EMAIL_PASS")
|
|
|
|
emailFrom := os.Getenv("EMAIL_FROM")
|
|
|
|
emailTo := os.Getenv("EMAIL_TO")
|
|
|
|
d := gomail.NewDialer(hostname, 587, login, password)
|
|
|
|
|
|
|
|
d.TLSConfig = &tls.Config{InsecureSkipVerify: true}
|
|
|
|
m := gomail.NewMessage()
|
|
|
|
m.SetHeader("From", emailFrom)
|
|
|
|
m.SetHeader("To", emailTo)
|
2023-09-08 03:03:57 +02:00
|
|
|
m.SetHeader("Subject", subject)
|
|
|
|
m.SetBody("text/plain", body)
|
2023-09-08 02:26:00 +02:00
|
|
|
|
|
|
|
if err := d.DialAndSend(m); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|