Files
AuthCentral/pkg/utils/email.go
Beyhan Oğur 8b1fbdee99 first commit
2026-04-26 21:37:58 +03:00

56 lines
1.5 KiB
Go

package utils
import (
"fmt"
"gauth-central/config"
"net/smtp"
)
func SendVerificationEmail(toEmail, token string) error {
// Get config
host := config.AppConfig.EmailHost
port := config.AppConfig.EmailPort
from := config.AppConfig.EmailFrom
if from == "" {
from = "noreply@gauth.local"
}
// Construct verification link
// Assuming frontend handles verification at /verify-email?token=...
// Or backend endpoint directly: /api/v1/auth/verify-email?token=...
// Let's use APP_URL from config
verifyLink := fmt.Sprintf("%s/v1/auth/verify-email?token=%s", config.AppConfig.AppURL, token)
// Email content
subject := "Subject: Verify your email address\n"
mime := "MIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\n\n"
body := fmt.Sprintf(`
<html>
<body>
<h2>Welcome to GAuth-Central!</h2>
<p>Please click the link below to verify your email address:</p>
<p><a href="%s">Verify Email</a></p>
<p>Or copy and paste this link: %s</p>
</body>
</html>
`, verifyLink, verifyLink)
msg := []byte(subject + mime + body)
// Address
addr := fmt.Sprintf("%s:%s", host, port)
// Auth (if needed)
var auth smtp.Auth
if config.AppConfig.EmailHostUser != "" && config.AppConfig.EmailHostPassword != "" {
auth = smtp.PlainAuth("", config.AppConfig.EmailHostUser, config.AppConfig.EmailHostPassword, host)
}
// Send email
if err := smtp.SendMail(addr, auth, from, []string{toEmail}, msg); err != nil {
return err
}
return nil
}