This commit is contained in:
teddy
2025-04-18 15:10:49 +07:00
parent a0984902e0
commit f7db944b97
4 changed files with 95 additions and 67 deletions

View File

@@ -6,6 +6,7 @@ import (
"log"
"net/smtp"
"os"
"strconv"
"text/template"
)
@@ -15,7 +16,15 @@ const CONFIG_SENDER_NAME = "HIS <hats.nutech@gmail.com>"
const CONFIG_AUTH_EMAIL = "hats.nutech@gmail.com"
const CONFIG_AUTH_PASSWORD = "lbfjfuywqktkexij"
func Mail() {
type MailConfig struct {
SMTPHost string
SMTPPort int
SMTPSenderName string
AuthEmail string
AuthPassword string
}
func Mail() *MailConfig {
// dialer := gomail.NewDialer(
// CONFIG_SMTP_HOST,
// CONFIG_SMTP_PORT,
@@ -23,7 +32,18 @@ func Mail() {
// CONFIG_AUTH_PASSWORD,
// )
// return dialer
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, subject, message string) error {
@@ -54,16 +74,17 @@ func SendEmail(to []string, cc []string, subject, message string) error {
htmlString := buf.String()
sender := "From: " + CONFIG_SENDER_NAME + "\n"
sender := "From: " + Mail().SMTPSenderName + "\n"
subjectt := "Subject: 👋 Hello from Go\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("", CONFIG_AUTH_EMAIL, CONFIG_AUTH_PASSWORD, CONFIG_SMTP_HOST)
smtpAddr := fmt.Sprintf("%s:%d", CONFIG_SMTP_HOST, CONFIG_SMTP_PORT)
auth := smtp.PlainAuth("", Mail().AuthEmail, Mail().AuthPassword, Mail().SMTPHost)
smtpAddr := fmt.Sprintf("%s:%d", Mail().SMTPHost, Mail().SMTPPort)
fmt.Println("cek dong", Mail())
err = smtp.SendMail(smtpAddr, auth, CONFIG_AUTH_EMAIL, append(to, cc...), messages)
err = smtp.SendMail(smtpAddr, auth, Mail().AuthEmail, append(to, cc...), messages)
if err != nil {
return err
}