55 lines
1.3 KiB
Go
55 lines
1.3 KiB
Go
package notifier
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"net/smtp"
|
|
|
|
"consumer/internal/logging"
|
|
)
|
|
|
|
// Notifier maneja el envío de notificaciones
|
|
type Notifier struct {
|
|
smtpHost string
|
|
smtpPort string
|
|
smtpUser string
|
|
smtpPass string
|
|
from string
|
|
logger *logging.LoggerSystem
|
|
}
|
|
|
|
// NewNotifier crea un nuevo notificador
|
|
func NewNotifier(smtpHost, smtpPort, smtpUser, smtpPass string, logger *logging.LoggerSystem) *Notifier {
|
|
return &Notifier{
|
|
smtpHost: smtpHost,
|
|
smtpPort: smtpPort,
|
|
smtpUser: smtpUser,
|
|
smtpPass: smtpPass,
|
|
from: smtpUser,
|
|
logger: logger,
|
|
}
|
|
}
|
|
|
|
// SendEmail envía un email de notificación
|
|
func (n *Notifier) SendEmail(to, subject, body string) error {
|
|
if n.smtpHost == "" || n.smtpPort == "" || n.smtpUser == "" {
|
|
n.logger.Error.Println("Configuración SMTP incompleta, no se enviará correo")
|
|
return errors.New("configuración SMTP incompleta")
|
|
}
|
|
|
|
auth := smtp.PlainAuth("", n.smtpUser, n.smtpPass, n.smtpHost)
|
|
msg := []byte(
|
|
"To: " + to + "\r\n" +
|
|
"Subject: " + subject + "\r\n" +
|
|
"MIME-Version: 1.0;\r\nContent-Type: text/plain; charset=\"UTF-8\";\r\n\r\n" +
|
|
body + "\r\n",
|
|
)
|
|
|
|
err := smtp.SendMail(n.smtpHost+":"+n.smtpPort, auth, n.smtpUser, []string{to}, msg)
|
|
if err != nil {
|
|
return fmt.Errorf("error enviando correo: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|