Files
goGin/pkg/utis/email.go
Beyhan Oğur 2a5b661443 first commit
2026-04-26 21:46:42 +03:00

59 lines
1.6 KiB
Go

package utils
import (
"fmt"
config "goGin/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@gogin.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
// ApiAppURL
// http://localhost:3000/auth/verify-email?token=TURNSITE_TOKEN_BURAYA
// http://localhost:3000/auth/verify-email?token=e4dab20a06260dabee0b90f8a6e6e2f2b5d0aa45e36f04bbc41ba4943ced2fb8
verifyLink := fmt.Sprintf("%s/auth/verify-email?token=%s", config.AppConfig.ApiAppURL, 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
}