You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
76 lines
1.4 KiB
76 lines
1.4 KiB
|
4 weeks ago
|
package main
|
||
|
|
|
||
|
|
import (
|
||
|
|
"github.com/wneessen/go-mail"
|
||
|
|
"fmt"
|
||
|
|
"context"
|
||
|
|
"github.com/redis/go-redis/v9"
|
||
|
|
"time"
|
||
|
|
"encoding/json"
|
||
|
|
"MY/webapp/data"
|
||
|
|
)
|
||
|
|
|
||
|
|
func SendEmail(msg data.EmailMessage) {
|
||
|
|
email_msg := mail.NewMsg()
|
||
|
|
err := email_msg.From(msg.From)
|
||
|
|
if err != nil { panic(err) }
|
||
|
|
|
||
|
|
err = email_msg.To(msg.To)
|
||
|
|
if err != nil { panic(err) }
|
||
|
|
|
||
|
|
email_msg.Subject(msg.Subject)
|
||
|
|
email_msg.SetBodyString(mail.TypeTextPlain, msg.Text)
|
||
|
|
email_msg.SetBodyString(mail.TypeTextHTML, msg.HTML)
|
||
|
|
|
||
|
|
client, err := mail.NewClient("localhost",
|
||
|
|
mail.WithPort(1025),
|
||
|
|
mail.WithTLSPolicy(mail.NoTLS))
|
||
|
|
|
||
|
|
if err != nil { panic(err) }
|
||
|
|
|
||
|
|
err = client.DialAndSend(email_msg)
|
||
|
|
if err != nil { panic(err) }
|
||
|
|
}
|
||
|
|
|
||
|
|
func LoadMessage(result string) data.EmailMessage {
|
||
|
|
var msg data.EmailMessage
|
||
|
|
|
||
|
|
err := json.Unmarshal([]byte(result), &msg)
|
||
|
|
if err != nil { panic(err) }
|
||
|
|
|
||
|
|
return msg
|
||
|
|
}
|
||
|
|
|
||
|
|
func EmailListener(ctx context.Context) {
|
||
|
|
client := redis.NewClient(&redis.Options{
|
||
|
|
Addr: "127.0.0.1:6379",
|
||
|
|
Password: "",
|
||
|
|
DB: 0,
|
||
|
|
})
|
||
|
|
|
||
|
|
for {
|
||
|
|
result, err := client.BRPop(ctx, 0, "queue").Result()
|
||
|
|
|
||
|
|
if err != nil { panic(err) }
|
||
|
|
|
||
|
|
fmt.Printf("received: %v\n", result)
|
||
|
|
|
||
|
|
// NOTE: 0=queue name, 1=message
|
||
|
|
msg := LoadMessage(result[1])
|
||
|
|
|
||
|
|
SendEmail(msg)
|
||
|
|
|
||
|
|
fmt.Println("SENT EMAIL")
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func main() {
|
||
|
|
ctx := context.Background()
|
||
|
|
|
||
|
|
go EmailListener(ctx)
|
||
|
|
|
||
|
|
time.Sleep(time.Second * 100)
|
||
|
|
}
|
||
|
|
|
||
|
|
|