Files
ares/services/email_service.go
Beyhan Oğur 4d92991817 first commit
2026-04-26 21:30:42 +03:00

54 lines
1.4 KiB
Go

package services
import (
"fmt"
"net/smtp"
"strings"
configs "ares/config"
)
type EmailService struct{}
func NewEmailService() *EmailService {
return &EmailService{}
}
func (s *EmailService) Send(to, subject, body string) error {
host := strings.TrimSpace(configs.AppConfig.EmailHost)
port := strings.TrimSpace(configs.AppConfig.EmailPort)
from := strings.TrimSpace(configs.AppConfig.EmailFrom)
if host == "" || port == "" || from == "" {
return fmt.Errorf("email configuration is incomplete")
}
addr := host + ":" + port
username := strings.TrimSpace(configs.AppConfig.EmailHostUser)
password := strings.TrimSpace(configs.AppConfig.EmailHostPassword)
var auth smtp.Auth
if username != "" && password != "" {
auth = smtp.PlainAuth("", username, password, host)
}
message := "From: " + from + "\r\n" +
"To: " + to + "\r\n" +
"Subject: " + subject + "\r\n" +
"MIME-Version: 1.0\r\n" +
"Content-Type: text/plain; charset=UTF-8\r\n\r\n" +
body
return smtp.SendMail(addr, auth, from, []string{to}, []byte(message))
}
func (s *EmailService) SendVerificationEmail(to, firstName, verifyURL string) error {
subject := "Email verification"
body := fmt.Sprintf(
"Hi %s,\n\nPlease verify your email by opening this link:\n%s\n\nIf you did not create this account, you can ignore this email.",
firstName,
verifyURL,
)
return s.Send(to, subject, body)
}