Skip to content

Commit

Permalink
email listener
Browse files Browse the repository at this point in the history
  • Loading branch information
Pomog committed Dec 10, 2023
1 parent 7acea61 commit 55941b0
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 20 deletions.
Binary file modified bookings
Binary file not shown.
54 changes: 35 additions & 19 deletions cmd/web/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,19 @@ func main() {
}

defer db.SQL.Close()
defer close(app.MailChan)

// test mail
/*
from := "thoryur@gmail.com"
password := "juzt yqwj advj oksj"
to := "thoryur@gmail.com"
subject := "Test Email"
body := "Hello, this is a test email from Golang."
fmt.Println("Starting Mail listener")
listenForMail()

msg := "To: " + to + "\r\n" +
"Subject: " + subject + "\r\n" +
"\r\n" + body
// msg := models.MailData{
// To: "john@do.com",
// From: "me@here.com",
// Subject: "Test",
// Content: "Hello",
// }

auth := smtp.PlainAuth("", from, password, "smtp.gmail.com")
err = smtp.SendMail("smtp.gmail.com:587", auth, from, []string{to}, []byte(msg))
if err != nil {
log.Fatal(err)
} else {
log.Println("Email sent successfully.")
}
*/
// app.MailChan <- msg

fmt.Printf("Server starting on port %s\n", port)

Expand All @@ -76,6 +67,9 @@ func run() (*driver.DB, error) {
gob.Register(models.Room{})
gob.Register(models.Restriction{})

mailChan := make(chan models.MailData)
app.MailChan = mailChan

// change this to true when in production
app.InProduction = false

Expand Down Expand Up @@ -119,3 +113,25 @@ func run() (*driver.DB, error) {

return db, nil
}

// test mail
/*
from := "thoryur@gmail.com"
password := "juzt yqwj advj oksj"
to := "thoryur@gmail.com"
subject := "Test Email"
body := "Hello, this is a test email from Golang."
msg := "To: " + to + "\r\n" +
"Subject: " + subject + "\r\n" +
"\r\n" + body
auth := smtp.PlainAuth("", from, password, "smtp.gmail.com")
err = smtp.SendMail("smtp.gmail.com:587", auth, from, []string{to}, []byte(msg))
if err != nil {
log.Fatal(err)
} else {
log.Println("Email sent successfully.")
}
*/
43 changes: 43 additions & 0 deletions cmd/web/send-mail.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package main

import (
"time"
"udemyCourse1/internal/models"

mail "github.com/xhit/go-simple-mail/v2"
)

func listenForMail() {
go func() {
for {
msg := <-app.MailChan
sendMsg(msg)
}
}()
}

func sendMsg(m models.MailData) {
server := mail.NewSMTPClient()
server.Host = "localhost"
server.Port = 1025
server.KeepAlive = false
server.ConnectTimeout = 10 * time.Second
server.SendTimeout = 10 * time.Second

client, err := server.Connect()
if err != nil {
errorlog.Println(err)
}

email := mail.NewMSG()
email.SetFrom(m.From).AddTo(m.To).SetSubject(m.Subject)
email.SetBody(mail.TextHTML, m.Content)

err = email.Send(client)
if err != nil {
errorlog.Println(err)
} else {
infolog.Println("Email sent")
}

}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ require (
github.com/jackc/pgx/v5 v5.5.0 // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/justinas/nosurf v1.1.1 // indirect
github.com/toorop/go-dkim v0.0.0-20201103131630-e1cd1a0a5208 // indirect
github.com/xhit/go-simple-mail/v2 v2.16.0 // indirect
golang.org/x/crypto v0.9.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/text v0.9.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/toorop/go-dkim v0.0.0-20201103131630-e1cd1a0a5208 h1:PM5hJF7HVfNWmCjMdEfbuOBNXSVF2cMFGgQTPdKCbwM=
github.com/toorop/go-dkim v0.0.0-20201103131630-e1cd1a0a5208/go.mod h1:BzWtXXrXzZUvMacR0oF/fbDDgUPO8L36tDMmRAf14ns=
github.com/xhit/go-simple-mail/v2 v2.16.0 h1:ouGy/Ww4kuaqu2E2UrDw7SvLaziWTB60ICLkIkNVccA=
github.com/xhit/go-simple-mail/v2 v2.16.0/go.mod h1:b7P5ygho6SYE+VIqpxA6QkYfv4teeyG4MKqB3utRu98=
golang.org/x/crypto v0.9.0 h1:LF6fAI+IutBocDJ2OT0Q1g8plpYljMZ4+lty+dsqw3g=
golang.org/x/crypto v0.9.0/go.mod h1:yrmDGqONDYtNj3tH8X9dzUun2m2lzPa9ngI6/RUPGR0=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
Expand Down
2 changes: 2 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"log"
"text/template"
"udemyCourse1/internal/models"

"github.com/alexedwards/scs/v2"
)
Expand All @@ -15,4 +16,5 @@ type AppConfig struct {
Session *scs.SessionManager
InfoLog *log.Logger
ErrorLog *log.Logger
MailChan chan models.MailData
}
12 changes: 11 additions & 1 deletion internal/models/models.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package models

import "time"
import (
"time"
)

// User is the model for the user table in the database
type User struct {
Expand Down Expand Up @@ -59,3 +61,11 @@ type RoomRestriction struct {
Reservation Reservation
Restriction Restriction
}

// MailData holds an email message
type MailData struct {
To string
From string
Subject string
Content string
}

0 comments on commit 55941b0

Please sign in to comment.