package utils import ( "fmt" "log" "net/smtp" "os" _ "github.com/joho/godotenv/autoload" ) func SendVerificationEmail(to string, token string) error { from := os.Getenv("EMAIL_FROM") password := os.Getenv("EMAIL_PASSWORD") smtpHost := os.Getenv("SMTP_HOST") smtpPort := os.Getenv("SMTP_PORT") verificationLink := fmt.Sprintf("%s/api/v1/verify/%s", os.Getenv("APP_URL"), token) subject := "Verify Your Email" body := fmt.Sprintf(`
Please verify your email address by clicking the link below:
Verify EmailIf you didn't create this account, please ignore this email.
`, verificationLink) message := fmt.Sprintf("To: %s\r\n"+ "Subject: %s\r\n"+ "MIME-Version: 1.0\r\n"+ "Content-Type: text/html; charset=UTF-8\r\n"+ "\r\n"+ "%s\r\n", to, subject, body) auth := smtp.PlainAuth("", from, password, smtpHost) addr := fmt.Sprintf("%s:%s", smtpHost, smtpPort) err := smtp.SendMail(addr, auth, from, []string{to}, []byte(message)) if err != nil { log.Printf("SMTP error: %v", err) // Add this line for more details } return err } func SendPasswordResetEmail(to string, token string) error { from := os.Getenv("EMAIL_FROM") password := os.Getenv("EMAIL_PASSWORD") smtpHost := os.Getenv("SMTP_HOST") smtpPort := os.Getenv("SMTP_PORT") //resetLink := fmt.Sprintf("%s/auth/reset-password?reset_token=%s", os.Getenv("FRONTEND_URL"), token) resetLink := fmt.Sprintf("http://localhost:3000/auth/reset-password?reset_token=%s", token) log.Printf("Generated reset link: %s", resetLink) subject := "Reset Your Password" body := fmt.Sprintf(`Click the link below to reset your password:
Reset PasswordIf you didn't request this, please ignore this email.
This link will expire in 1 hour.
`, resetLink) message := fmt.Sprintf("To: %s\r\n"+ "Subject: %s\r\n"+ "MIME-Version: 1.0\r\n"+ "Content-Type: text/html; charset=UTF-8\r\n"+ "\r\n"+ "%s\r\n", to, subject, body) auth := smtp.PlainAuth("", from, password, smtpHost) addr := fmt.Sprintf("%s:%s", smtpHost, smtpPort) return smtp.SendMail(addr, auth, from, []string{to}, []byte(message)) }