105 lines
2.9 KiB
Go
105 lines
2.9 KiB
Go
package mail
|
|
|
|
import (
|
|
"fmt"
|
|
"net/smtp"
|
|
"os"
|
|
"time"
|
|
)
|
|
|
|
// Config holds SMTP server configuration
|
|
type Config struct {
|
|
Host string
|
|
Port string
|
|
Username string
|
|
Password string
|
|
From string
|
|
}
|
|
|
|
// Client handles email operations
|
|
type Client struct {
|
|
Config Config
|
|
}
|
|
|
|
// Mailer is an alias for Client to maintain compatibility with backup package
|
|
type Mailer struct {
|
|
Config Config
|
|
}
|
|
|
|
// NewClient creates a new mail client from environment variables
|
|
func NewClient() *Client {
|
|
return &Client{
|
|
Config: Config{
|
|
Host: os.Getenv("SMTP_HOST"),
|
|
Port: os.Getenv("SMTP_PORT"),
|
|
Username: os.Getenv("SMTP_USERNAME"),
|
|
Password: os.Getenv("SMTP_PASSWORD"),
|
|
From: os.Getenv("SMTP_FROM"),
|
|
},
|
|
}
|
|
}
|
|
|
|
// NewMailer creates a new mailer from environment variables
|
|
func NewMailer() *Mailer {
|
|
client := NewClient()
|
|
return &Mailer{
|
|
Config: client.Config,
|
|
}
|
|
}
|
|
|
|
// SendMail sends an email notification
|
|
func (c *Client) SendMail(to []string, subject, body string) error {
|
|
addr := fmt.Sprintf("%s:%s", c.Config.Host, c.Config.Port)
|
|
// Compose message
|
|
message := []byte(fmt.Sprintf("From: %s\r\n"+
|
|
"To: %s\r\n"+
|
|
"Subject: %s\r\n"+
|
|
"\r\n"+
|
|
"%s\r\n", c.Config.From, to[0], subject, body))
|
|
// Authenticate
|
|
auth := smtp.PlainAuth("", c.Config.Username, c.Config.Password, c.Config.Host)
|
|
// Send mail
|
|
return smtp.SendMail(addr, auth, c.Config.From, to, message)
|
|
}
|
|
|
|
// SendMail sends an email notification (Mailer version)
|
|
func (m *Mailer) SendMail(to []string, subject, body string) error {
|
|
addr := fmt.Sprintf("%s:%s", m.Config.Host, m.Config.Port)
|
|
// Compose message
|
|
message := []byte(fmt.Sprintf("From: %s\r\n"+
|
|
"To: %s\r\n"+
|
|
"Subject: %s\r\n"+
|
|
"\r\n"+
|
|
"%s\r\n", m.Config.From, to[0], subject, body))
|
|
// Authenticate
|
|
auth := smtp.PlainAuth("", m.Config.Username, m.Config.Password, m.Config.Host)
|
|
// Send mail
|
|
return smtp.SendMail(addr, auth, m.Config.From, to, message)
|
|
}
|
|
|
|
// SendSuccessNotification sends a backup success notification
|
|
func (m *Mailer) SendSuccessNotification(serviceName string, duration time.Duration) error {
|
|
recipients := []string{os.Getenv("NOTIFICATION_EMAIL")}
|
|
if recipients[0] == "" {
|
|
recipients[0] = m.Config.From // Fallback to sender if no notification email set
|
|
}
|
|
|
|
subject := fmt.Sprintf("Backup Successful: %s", serviceName)
|
|
body := fmt.Sprintf("The backup for service %s completed successfully in %v.", serviceName, duration)
|
|
|
|
return m.SendMail(recipients, subject, body)
|
|
}
|
|
|
|
// SendErrorNotification sends a backup error notification
|
|
func (m *Mailer) SendErrorNotification(serviceName string, err error) error {
|
|
recipients := []string{os.Getenv("NOTIFICATION_EMAIL")}
|
|
if recipients[0] == "" {
|
|
recipients[0] = m.Config.From // Fallback to sender if no notification email set
|
|
}
|
|
|
|
subject := fmt.Sprintf("Backup Failed: %s", serviceName)
|
|
body := fmt.Sprintf("The backup for service %s failed with error: %v", serviceName, err)
|
|
|
|
return m.SendMail(recipients, subject, body)
|
|
}
|