add mailer mechanism
This commit is contained in:
parent
343d4893f0
commit
563e9ba5c0
11
app/app.go
11
app/app.go
@ -6,6 +6,7 @@ import (
|
|||||||
"embed"
|
"embed"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"gopher-toolbox/mail"
|
||||||
"gopher-toolbox/utils"
|
"gopher-toolbox/utils"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"os"
|
"os"
|
||||||
@ -53,6 +54,7 @@ type App struct {
|
|||||||
Database AppDatabase
|
Database AppDatabase
|
||||||
Security AppSecurity
|
Security AppSecurity
|
||||||
AppInfo AppInfo
|
AppInfo AppInfo
|
||||||
|
Mailer mail.Mailer
|
||||||
}
|
}
|
||||||
|
|
||||||
type AppDatabase struct {
|
type AppDatabase struct {
|
||||||
@ -83,10 +85,7 @@ func New(version string) *App {
|
|||||||
var durationTime time.Duration
|
var durationTime time.Duration
|
||||||
var ak paseto.V4AsymmetricSecretKey
|
var ak paseto.V4AsymmetricSecretKey
|
||||||
|
|
||||||
ak, err = paseto.NewV4AsymmetricSecretKeyFromHex(os.Getenv("ASYMMETRICKEY"))
|
|
||||||
if err != nil {
|
|
||||||
ak = paseto.NewV4AsymmetricSecretKey()
|
ak = paseto.NewV4AsymmetricSecretKey()
|
||||||
}
|
|
||||||
pk := ak.Public()
|
pk := ak.Public()
|
||||||
|
|
||||||
duration := os.Getenv("DURATION")
|
duration := os.Getenv("DURATION")
|
||||||
@ -98,6 +97,12 @@ func New(version string) *App {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return &App{
|
return &App{
|
||||||
|
Mailer: mail.New(
|
||||||
|
os.Getenv("SMTP_HOST"),
|
||||||
|
os.Getenv("SMTP_PORT"),
|
||||||
|
os.Getenv("SMTP_USER"),
|
||||||
|
os.Getenv("SMTP_PASS"),
|
||||||
|
),
|
||||||
Database: AppDatabase{
|
Database: AppDatabase{
|
||||||
Migrate: utils.GetBool(os.Getenv("MIGRATE")),
|
Migrate: utils.GetBool(os.Getenv("MIGRATE")),
|
||||||
DriverName: os.Getenv("DRIVERNAME"),
|
DriverName: os.Getenv("DRIVERNAME"),
|
||||||
|
|||||||
55
mail/mail.go
Normal file
55
mail/mail.go
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
package mail
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"net/smtp"
|
||||||
|
"os"
|
||||||
|
"text/template"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Mailer struct {
|
||||||
|
smtpHost string
|
||||||
|
smtpPort string
|
||||||
|
smtpUser string
|
||||||
|
smtpPass string
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(smtpHost string, smtpPort string, smtpUser string, smtpPass string) Mailer {
|
||||||
|
return Mailer{
|
||||||
|
smtpHost: smtpHost,
|
||||||
|
smtpPort: smtpPort,
|
||||||
|
smtpUser: smtpUser,
|
||||||
|
smtpPass: smtpPass,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Mailer) SendMail(to []string, templateName string, data interface{}) error {
|
||||||
|
templateContent := getTemplate(templateName)
|
||||||
|
if templateContent == "" {
|
||||||
|
return fmt.Errorf("template %s not found", templateName)
|
||||||
|
}
|
||||||
|
|
||||||
|
tmpl, err := template.New("email").Parse(templateContent)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
buf := new(bytes.Buffer)
|
||||||
|
if err := tmpl.Execute(buf, data); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
auth := smtp.PlainAuth(m.smtpUser, m.smtpUser, m.smtpPass, m.smtpHost)
|
||||||
|
return smtp.SendMail(m.smtpHost+":"+m.smtpPort, auth, m.smtpUser, to, buf.Bytes())
|
||||||
|
}
|
||||||
|
|
||||||
|
func getTemplate(templateName string) string {
|
||||||
|
templatePath := "templates/" + templateName + ".gotmpl"
|
||||||
|
content, err := os.ReadFile(templatePath)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error leyendo plantilla: %v\n", err)
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return string(content)
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user