update
This commit is contained in:
@@ -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
30
config/mail-config.go
Normal 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"),
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,9 +2,11 @@ package helper
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"email-notification/config"
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
"log"
|
"log"
|
||||||
|
"net/smtp"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
@@ -65,3 +67,21 @@ func FormatSince(t time.Time) string {
|
|||||||
|
|
||||||
return "baru saja"
|
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
|
||||||
|
}
|
||||||
|
|||||||
5
main.go
5
main.go
@@ -3,7 +3,6 @@ package main
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"email-notification/config"
|
"email-notification/config"
|
||||||
"email-notification/middleware"
|
|
||||||
"email-notification/route"
|
"email-notification/route"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -19,10 +18,6 @@ func main() {
|
|||||||
log.Fatal("Error loading .env file")
|
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())
|
db := config.NewDatabase().WithContext(context.Background())
|
||||||
|
|
||||||
route.Route(db)
|
route.Route(db)
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package route
|
package route
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"email-notification/app"
|
|
||||||
"email-notification/helper"
|
"email-notification/helper"
|
||||||
"email-notification/middleware"
|
"email-notification/middleware"
|
||||||
"email-notification/model"
|
"email-notification/model"
|
||||||
@@ -16,6 +15,10 @@ import (
|
|||||||
|
|
||||||
func Route(db *gorm.DB) {
|
func Route(db *gorm.DB) {
|
||||||
http.HandleFunc("/birthday-notification", middleware.ResponseTimeMiddleware(func(w http.ResponseWriter, r *http.Request) {
|
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()
|
sessionID := helper.GenerateSessionID()
|
||||||
module := "BIRTHDAY NOTIFICATION"
|
module := "BIRTHDAY NOTIFICATION"
|
||||||
log.Printf("%+v %+v request %+v", sessionID, module, nil)
|
log.Printf("%+v %+v request %+v", sessionID, module, nil)
|
||||||
@@ -48,7 +51,7 @@ func Route(db *gorm.DB) {
|
|||||||
|
|
||||||
go func(v model.Birthday) {
|
go func(v model.Birthday) {
|
||||||
log.Printf("%+v %+v SENDING EMAIL TO %+v", sessionID, module, v.Email)
|
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 {
|
if err != nil {
|
||||||
log.Printf("%+v %+v response %+v", sessionID, module, err.Error())
|
log.Printf("%+v %+v response %+v", sessionID, module, err.Error())
|
||||||
} else {
|
} else {
|
||||||
@@ -62,6 +65,10 @@ func Route(db *gorm.DB) {
|
|||||||
}))
|
}))
|
||||||
|
|
||||||
http.HandleFunc("/workanniversary-notification", middleware.ResponseTimeMiddleware(func(w http.ResponseWriter, r *http.Request) {
|
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()
|
sessionID := helper.GenerateSessionID()
|
||||||
module := "WORKANNIVERSARY NOTIFICATION"
|
module := "WORKANNIVERSARY NOTIFICATION"
|
||||||
log.Printf("%+v %+v request %+v", sessionID, module, nil)
|
log.Printf("%+v %+v request %+v", sessionID, module, nil)
|
||||||
@@ -99,7 +106,7 @@ func Route(db *gorm.DB) {
|
|||||||
|
|
||||||
go func(v model.Workanniversary) {
|
go func(v model.Workanniversary) {
|
||||||
log.Printf("%+v %+v SENDING EMAIL TO %+v", sessionID, module, v.Email)
|
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 {
|
if err != nil {
|
||||||
log.Printf("%+v %+v response %+v", sessionID, module, err.Error())
|
log.Printf("%+v %+v response %+v", sessionID, module, err.Error())
|
||||||
} else {
|
} else {
|
||||||
@@ -112,7 +119,15 @@ func Route(db *gorm.DB) {
|
|||||||
w.Write([]byte("success"))
|
w.Write([]byte("success"))
|
||||||
}))
|
}))
|
||||||
|
|
||||||
http.HandleFunc("/test", middleware.ResponseTimeMiddleware(func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("*", middleware.ResponseTimeMiddleware(func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Write([]byte("success"))
|
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!"))
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user