first commit
This commit is contained in:
112
pkg/utils/email.go
Normal file
112
pkg/utils/email.go
Normal file
@@ -0,0 +1,112 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"gobeyhan/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
|
||||
}
|
||||
|
||||
func SendContactEmail(name, email, subject, message, ip string) error {
|
||||
// Get config
|
||||
host := config.AppConfig.EmailHost
|
||||
port := config.AppConfig.EmailPort
|
||||
from := config.AppConfig.EmailFrom
|
||||
if from == "" {
|
||||
from = "noreply@gauth.local"
|
||||
}
|
||||
|
||||
// Typically, contact form emails are sent TO the admin, not the user who filled the form.
|
||||
// However, the original code seemed to imply sending it somewhere.
|
||||
// Let's assume we send it to a configured admin email or the same 'from' address for now.
|
||||
// Or maybe we send a confirmation to the user?
|
||||
// The original python code `send_contact_email` likely sent it to the site admins.
|
||||
// Let's send it to the configured "EmailFrom" address (acting as admin) for this example.
|
||||
toEmail := config.AppConfig.EmailFrom
|
||||
if toEmail == "" {
|
||||
toEmail = "admin@gauth.local"
|
||||
}
|
||||
|
||||
// Email content
|
||||
emailSubject := fmt.Sprintf("Subject: New Contact Message: %s\n", subject)
|
||||
mime := "MIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\n\n"
|
||||
body := fmt.Sprintf(`
|
||||
<html>
|
||||
<body>
|
||||
<h2>New Contact Message</h2>
|
||||
<p><strong>Name:</strong> %s</p>
|
||||
<p><strong>Email:</strong> %s</p>
|
||||
<p><strong>IP:</strong> %s</p>
|
||||
<p><strong>Subject:</strong> %s</p>
|
||||
<hr>
|
||||
<p><strong>Message:</strong></p>
|
||||
<p>%s</p>
|
||||
</body>
|
||||
</html>
|
||||
`, name, email, ip, subject, message)
|
||||
|
||||
msg := []byte(emailSubject + 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
|
||||
}
|
||||
Reference in New Issue
Block a user