diff --git a/internal/email/service.go b/internal/email/service.go index e02c9c2..9fc4dd7 100644 --- a/internal/email/service.go +++ b/internal/email/service.go @@ -2,81 +2,163 @@ package email import ( "fmt" + "net/smtp" "os" - - "github.com/resend/resend-go/v2" + "strconv" ) type Service struct { - client *resend.Client - from string + smtpServer string + smtpPort int + smtpUser string + smtpPassword string + from string } func NewService() *Service { - senderEmail := os.Getenv("SENDER_EMAIL") - if senderEmail == "" { - senderEmail = "noreply@rideaware.app" + smtpServer := os.Getenv("SMTP_SERVER") + if smtpServer == "" { + smtpServer = "localhost" } - apiKey := os.Getenv("RESEND_API_KEY") - if apiKey == "" { - apiKey = "re_test" + smtpPort := os.Getenv("SMTP_PORT") + if smtpPort == "" { + smtpPort = "587" + } + + port, err := strconv.Atoi(smtpPort) + if err != nil { + port = 587 + } + + smtpUser := os.Getenv("SMTP_USER") + if smtpUser == "" { + smtpUser = "noreply@rideaware.app" + } + + smtpPassword := os.Getenv("SMTP_PASSWORD") + + from := os.Getenv("SENDER_EMAIL") + if from == "" { + from = "noreply@rideaware.app" } return &Service{ - client: resend.NewClient(apiKey), - from: senderEmail, + smtpServer: smtpServer, + smtpPort: port, + smtpUser: smtpUser, + smtpPassword: smtpPassword, + from: from, } } +func (s *Service) sendEmail(to []string, subject, htmlBody string) error { + // Create message + headers := fmt.Sprintf("From: %s\r\nTo: %s\r\nSubject: %s\r\nMIME-Version: 1.0\r\nContent-Type: text/html; charset=\"UTF-8\"\r\n\r\n", + s.from, + to[0], + subject, + ) + + message := headers + htmlBody + + // SMTP server address + addr := fmt.Sprintf("%s:%d", s.smtpServer, s.smtpPort) + + // Create SMTP authentication + auth := smtp.PlainAuth("", s.smtpUser, s.smtpPassword, s.smtpServer) + + // Send email + err := smtp.SendMail(addr, auth, s.from, to, []byte(message)) + if err != nil { + return fmt.Errorf("failed to send email: %w", err) + } + + return nil +} + func (s *Service) SendPasswordResetEmail(email, username, resetLink string) error { - params := &resend.SendEmailRequest{ - From: s.from, - To: []string{email}, - Subject: "Reset Your RideAware Password", - Html: fmt.Sprintf(` -

Password Reset Request

-

Hi %s,

-

We received a request to reset your password. Click the link below to create a new password:

-

Reset Password

-

This link will expire in 1 hour.

-

If you didn't request this, you can ignore this email.

- `, username, resetLink), - } + subject := "Reset Your RideAware Password" + htmlBody := fmt.Sprintf(` + + + + + + +
+
+

Password Reset Request

+
+
+

Hi %s,

+

We received a request to reset your password. Click the button below to create a new password:

+

Reset Password

+

Note: This link will expire in 1 hour.

+

If you didn't request this, you can safely ignore this email.

+
+ +
+ + + `, username, resetLink) - sent, err := s.client.Emails.Send(params) - if err != nil { - return fmt.Errorf("failed to send email: %w", err) - } - - if sent.Id == "" { - return fmt.Errorf("failed to send email") - } - - return nil + return s.sendEmail([]string{email}, subject, htmlBody) } func (s *Service) SendWelcomeEmail(email, username string) error { - params := &resend.SendEmailRequest{ - From: s.from, - To: []string{email}, - Subject: "Welcome to RideAware", - Html: fmt.Sprintf(` -

Welcome to RideAware

-

Hi %s,

-

Your account has been created successfully!

-

Start tracking your rides and improve your performance.

- `, username), - } + subject := "Welcome to RideAware" + htmlBody := fmt.Sprintf(` + + + + + + +
+
+

Welcome to RideAware

+
+
+

Hi %s,

+

Your account has been created successfully! 🚀

+

You're now ready to:

+ +

Get started by logging in to your account and setting up your profile.

+

Go to RideAware

+
+ +
+ + + `, username) - sent, err := s.client.Emails.Send(params) - if err != nil { - return fmt.Errorf("failed to send email: %w", err) - } + return s.sendEmail([]string{email}, subject, htmlBody) +} - if sent.Id == "" { - return fmt.Errorf("failed to send email") - } - - return nil +func (s *Service) SendNewsletterEmail(email, subject, htmlBody string) error { + return s.sendEmail([]string{email}, subject, htmlBody) } \ No newline at end of file