Files
email-notification/app/mail-config.go
2025-04-18 18:15:59 +07:00

50 lines
1.2 KiB
Go

package app
import (
"fmt"
"net/smtp"
"os"
"strconv"
)
type MailConfig struct {
SMTPHost string
SMTPPort int
SMTPSenderName string
AuthEmail string
AuthPassword string
}
func Mail() *MailConfig {
port, err := strconv.Atoi(os.Getenv("SMTP_PORT"))
if err != nil {
fmt.Println("error parsing to int", err.Error())
}
return &MailConfig{
SMTPHost: os.Getenv("SMTP_HOST"),
SMTPPort: port,
SMTPSenderName: os.Getenv("SMTP_SENDER"),
AuthEmail: os.Getenv("AUTH_EMAIL"),
AuthPassword: os.Getenv("AUTH_PASSWORD"),
}
}
func SendEmail(to []string, cc []string, name, subject, message, htmlString string) error {
sender := "From: " + Mail().SMTPSenderName + "\n"
subjectt := "Subject: " + subject + "\n"
mime := "MIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\n\n"
body := htmlString
messages := []byte(sender + subjectt + mime + body)
auth := smtp.PlainAuth("", Mail().AuthEmail, Mail().AuthPassword, Mail().SMTPHost)
smtpAddr := fmt.Sprintf("%s:%d", Mail().SMTPHost, Mail().SMTPPort)
err := smtp.SendMail(smtpAddr, auth, Mail().AuthEmail, append(to, cc...), messages)
if err != nil {
return err
}
return nil
}