update
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
79
main.go
79
main.go
@@ -1,8 +1,11 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"email-notification/app"
|
||||
"email-notification/config"
|
||||
"email-notification/model"
|
||||
"email-notification/query"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
@@ -18,81 +21,47 @@ func main() {
|
||||
log.Fatal("Error loading .env file")
|
||||
}
|
||||
|
||||
config.NewDatabase()
|
||||
db := config.NewDatabase().WithContext(context.Background())
|
||||
|
||||
to := []string{"teddy.nutech@gmail.com"}
|
||||
cc := []string{"teddy.nutech@gmail.com"}
|
||||
subject := "test"
|
||||
message := "test"
|
||||
|
||||
// conn, err := amqp091.Dial("amqp://guest:guest@36.66.3.44:7012/")
|
||||
// if err != nil {
|
||||
// fmt.Println("Failed to connect to RabbitMQ: ", err)
|
||||
// }
|
||||
// defer conn.Close()
|
||||
fmt.Println(to, cc, subject, message)
|
||||
|
||||
// ch, err := conn.Channel()
|
||||
// if err != nil {
|
||||
// fmt.Println("Failed to open a channel: ", err)
|
||||
// }
|
||||
// defer ch.Close()
|
||||
http.HandleFunc("/birthday-notification", func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
http.HandleFunc("/notif-email", func(w http.ResponseWriter, r *http.Request) {
|
||||
data := new([]model.Birthday)
|
||||
|
||||
// ch.Publish("notification", "email", false, false, amqp091.Publishing{
|
||||
// ContentType: "application/json",
|
||||
// Body: []byte("test-email"),
|
||||
// })
|
||||
err := query.GetBirthday(db, data, "1", "1")
|
||||
if err != nil {
|
||||
w.WriteHeader(500)
|
||||
w.Write([]byte(err.Error()))
|
||||
}
|
||||
|
||||
fmt.Println("hasil query data", data)
|
||||
|
||||
for _, v := range *data {
|
||||
fmt.Println("Birthday", v.Fullname)
|
||||
sendTo := []string{v.Email}
|
||||
go func() {
|
||||
fmt.Println("sending email...")
|
||||
err := app.SendEmail(to, cc, subject, message)
|
||||
err := app.SendEmail(sendTo, sendTo, subject, message)
|
||||
if err != nil {
|
||||
fmt.Println("error", err.Error())
|
||||
}
|
||||
w.WriteHeader(500)
|
||||
w.Write([]byte(err.Error()))
|
||||
} else {
|
||||
fmt.Println("email sent")
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
w.Write([]byte{})
|
||||
w.Write([]byte("success"))
|
||||
})
|
||||
|
||||
port := os.Getenv("PORT")
|
||||
|
||||
http.ListenAndServe(":"+port, nil)
|
||||
|
||||
// q, err := ch.QueueDeclare(
|
||||
// "email", // name
|
||||
// false,
|
||||
// false,
|
||||
// false,
|
||||
// false,
|
||||
// nil,
|
||||
// )
|
||||
// if err != nil {
|
||||
// fmt.Println("Failed to declare a queue:", err)
|
||||
// }
|
||||
|
||||
// msgs, err := ch.Consume(
|
||||
// "email",
|
||||
// "", // consumer
|
||||
// true, // auto-ack
|
||||
// false, // exclusive
|
||||
// false, // no-local
|
||||
// false, // no-wait
|
||||
// nil,
|
||||
// )
|
||||
// if err != nil {
|
||||
// fmt.Println("Failed to register a consumer: ", err)
|
||||
// }
|
||||
|
||||
// forever := make(chan bool)
|
||||
|
||||
// go func() {
|
||||
// for d := range msgs {
|
||||
// log.Printf("Received a message: %s", d.Body)
|
||||
// }
|
||||
// }()
|
||||
|
||||
// fmt.Println(" [*] Waiting for messages. To exit press CTRL+C")
|
||||
// <-forever
|
||||
}
|
||||
|
||||
15
model/model.go
Normal file
15
model/model.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package model
|
||||
|
||||
import "time"
|
||||
|
||||
type Birthday struct {
|
||||
Email string `json:"email"`
|
||||
Fullname string `json:"fullname"`
|
||||
Birthday time.Time `json:"birthday"`
|
||||
}
|
||||
|
||||
type Workanniversary struct {
|
||||
Email string `json:"email"`
|
||||
Fullname string `json:"fullname"`
|
||||
HireDate time.Time `json:"hire_date"`
|
||||
}
|
||||
23
query/query.go
Normal file
23
query/query.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package query
|
||||
|
||||
import (
|
||||
"email-notification/model"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func GetBirthday(db *gorm.DB, data *[]model.Birthday, month, day string) error {
|
||||
return db.Table("user").
|
||||
Where("status_id", 1).
|
||||
Where("EXTRACT(MONTH FROM birthday) = ?", month).
|
||||
Where("EXTRACT(DAY FROM birthday) = ?", day).
|
||||
Find(&data).Error
|
||||
}
|
||||
|
||||
func GetWorkanniversary(db *gorm.DB, data *[]model.Workanniversary, month, day string) error {
|
||||
return db.Table("user").
|
||||
Where("status_id", 1).
|
||||
Where("EXTRACT(MONTH FROM hire_date) = ?", month).
|
||||
Where("EXTRACT(DAY FROM hire_date) = ?", day).
|
||||
Find(&data).Error
|
||||
}
|
||||
Reference in New Issue
Block a user