mirror of
https://github.com/Dvorinka/Bookra.git
synced 2026-06-03 20:13:00 +00:00
78 lines
2.1 KiB
Go
78 lines
2.1 KiB
Go
package email
|
|
|
|
import (
|
|
"crypto/tls"
|
|
"fmt"
|
|
"net/smtp"
|
|
|
|
"github.com/jordan-wright/email"
|
|
)
|
|
|
|
type Config struct {
|
|
Host string
|
|
Port int
|
|
Username string
|
|
Password string
|
|
From string
|
|
}
|
|
|
|
type Service struct {
|
|
config Config
|
|
}
|
|
|
|
func New(config Config) *Service {
|
|
return &Service{config: config}
|
|
}
|
|
|
|
// SendMagicLink sends a magic link authentication email with proper branding
|
|
func (s *Service) SendMagicLink(toEmail, toName, linkURL, locale string) error {
|
|
template := MagicLinkEmail(toName, linkURL, locale)
|
|
return s.sendTemplate(toEmail, template)
|
|
}
|
|
|
|
// SendWelcomeEmail sends a welcome email to new users
|
|
func (s *Service) SendWelcomeEmail(toEmail, name, locale string) error {
|
|
template := WelcomeEmail(name, locale)
|
|
return s.sendTemplate(toEmail, template)
|
|
}
|
|
|
|
// SendBookingConfirmation sends booking confirmation to customers
|
|
func (s *Service) SendBookingConfirmation(toEmail, customerName, businessName, serviceName, dateTime, location, locale string) error {
|
|
template := BookingConfirmationEmail(customerName, businessName, serviceName, dateTime, location, locale)
|
|
return s.sendTemplate(toEmail, template)
|
|
}
|
|
|
|
// SendPasswordReset sends password reset email
|
|
func (s *Service) SendPasswordReset(toEmail, name, resetURL, locale string) error {
|
|
template := PasswordResetEmail(name, resetURL, locale)
|
|
return s.sendTemplate(toEmail, template)
|
|
}
|
|
|
|
// sendTemplate sends an email using the provided template
|
|
func (s *Service) sendTemplate(toEmail string, template EmailTemplate) error {
|
|
e := email.NewEmail()
|
|
e.From = fmt.Sprintf("Bookra <%s>", s.config.From)
|
|
e.To = []string{toEmail}
|
|
e.Subject = template.Subject
|
|
e.Text = []byte(template.Text)
|
|
e.HTML = []byte(template.HTML)
|
|
|
|
return s.send(e)
|
|
}
|
|
|
|
// send delivers the email via SMTP
|
|
func (s *Service) send(e *email.Email) error {
|
|
addr := fmt.Sprintf("%s:%d", s.config.Host, s.config.Port)
|
|
|
|
var auth smtp.Auth
|
|
if s.config.Username != "" {
|
|
auth = smtp.PlainAuth("", s.config.Username, s.config.Password, s.config.Host)
|
|
}
|
|
|
|
if s.config.Port == 465 {
|
|
return e.SendWithTLS(addr, auth, &tls.Config{ServerName: s.config.Host})
|
|
}
|
|
|
|
return e.Send(addr, auth)
|
|
}
|