This commit is contained in:
teddy
2025-04-18 21:36:12 +07:00
parent 90270a2ab8
commit 2a67bb260f
5 changed files with 70 additions and 59 deletions

View File

@@ -1,49 +0,0 @@
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
}

30
config/mail-config.go Normal file
View File

@@ -0,0 +1,30 @@
package config
import (
"fmt"
"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"),
}
}

View File

@@ -2,9 +2,11 @@ package helper
import (
"bytes"
"email-notification/config"
"fmt"
"html/template"
"log"
"net/smtp"
"time"
"github.com/google/uuid"
@@ -65,3 +67,21 @@ func FormatSince(t time.Time) string {
return "baru saja"
}
func SendEmail(to []string, cc []string, name, subject, message, htmlString string) error {
sender := "From: " + config.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("", config.Mail().AuthEmail, config.Mail().AuthPassword, config.Mail().SMTPHost)
smtpAddr := fmt.Sprintf("%s:%d", config.Mail().SMTPHost, config.Mail().SMTPPort)
err := smtp.SendMail(smtpAddr, auth, config.Mail().AuthEmail, append(to, cc...), messages)
if err != nil {
return err
}
return nil
}

View File

@@ -3,7 +3,6 @@ package main
import (
"context"
"email-notification/config"
"email-notification/middleware"
"email-notification/route"
"log"
"net/http"
@@ -19,10 +18,6 @@ func main() {
log.Fatal("Error loading .env file")
}
http.Handle("/", middleware.ResponseTimeMiddleware(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Welcome!"))
}))
db := config.NewDatabase().WithContext(context.Background())
route.Route(db)

View File

@@ -1,7 +1,6 @@
package route
import (
"email-notification/app"
"email-notification/helper"
"email-notification/middleware"
"email-notification/model"
@@ -16,6 +15,10 @@ import (
func Route(db *gorm.DB) {
http.HandleFunc("/birthday-notification", middleware.ResponseTimeMiddleware(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
}
sessionID := helper.GenerateSessionID()
module := "BIRTHDAY NOTIFICATION"
log.Printf("%+v %+v request %+v", sessionID, module, nil)
@@ -48,7 +51,7 @@ func Route(db *gorm.DB) {
go func(v model.Birthday) {
log.Printf("%+v %+v SENDING EMAIL TO %+v", sessionID, module, v.Email)
err := app.SendEmail(sendTo, sendTo, "", subject, "", htmlString)
err := helper.SendEmail(sendTo, sendTo, "", subject, "", htmlString)
if err != nil {
log.Printf("%+v %+v response %+v", sessionID, module, err.Error())
} else {
@@ -62,6 +65,10 @@ func Route(db *gorm.DB) {
}))
http.HandleFunc("/workanniversary-notification", middleware.ResponseTimeMiddleware(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
}
sessionID := helper.GenerateSessionID()
module := "WORKANNIVERSARY NOTIFICATION"
log.Printf("%+v %+v request %+v", sessionID, module, nil)
@@ -99,7 +106,7 @@ func Route(db *gorm.DB) {
go func(v model.Workanniversary) {
log.Printf("%+v %+v SENDING EMAIL TO %+v", sessionID, module, v.Email)
err := app.SendEmail(sendTo, sendTo, "", subject, "", htmlString)
err := helper.SendEmail(sendTo, sendTo, "", subject, "", htmlString)
if err != nil {
log.Printf("%+v %+v response %+v", sessionID, module, err.Error())
} else {
@@ -112,7 +119,15 @@ func Route(db *gorm.DB) {
w.Write([]byte("success"))
}))
http.HandleFunc("/test", middleware.ResponseTimeMiddleware(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("success"))
http.HandleFunc("*", middleware.ResponseTimeMiddleware(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(404)
w.Write([]byte("what are you looking for!?"))
}))
http.Handle("/", middleware.ResponseTimeMiddleware(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
}
w.Write([]byte("Welcome!"))
}))
}