From b1d3095c89a98b3ecb64dfb13acfebc4f66847ad Mon Sep 17 00:00:00 2001 From: vinchent Date: Wed, 17 Jul 2024 22:34:03 +0200 Subject: [PATCH] Sending mails using Foundation templates --- cmd/web/main.go | 8 - cmd/web/send-mail.go | 17 +- email-templates/drip.html | 1524 +++++++++++++++++++++++++++++++++ internal/handlers/handlers.go | 37 + internal/models/models.go | 9 +- 5 files changed, 1582 insertions(+), 13 deletions(-) create mode 100644 email-templates/drip.html diff --git a/cmd/web/main.go b/cmd/web/main.go index 7e2c875..0593420 100644 --- a/cmd/web/main.go +++ b/cmd/web/main.go @@ -59,14 +59,6 @@ func run() (*driver.DB, error) { listenForMail() - msg := models.MailData{ - To: "John@do.do", - From: "me@here.com", - Subject: "mail", - Content: "Hello, world", - } - app.MailChan <- msg - // change this to true when in production app.InProduction = false diff --git a/cmd/web/send-mail.go b/cmd/web/send-mail.go index 1522a01..12a5e1e 100644 --- a/cmd/web/send-mail.go +++ b/cmd/web/send-mail.go @@ -1,8 +1,11 @@ package main import ( + "fmt" "go-udemy-web-1/internal/models" "log" + "os" + "strings" "time" mail "github.com/xhit/go-simple-mail" @@ -32,7 +35,19 @@ func sendMsg(m models.MailData) { email := mail.NewMSG() email.SetFrom(m.From).AddTo(m.To).SetSubject(m.Subject) - email.SetBody(mail.TextHTML, m.Content) + if m.Template == "" { + email.SetBody(mail.TextHTML, m.Content) + } else { + data, err := os.ReadFile(fmt.Sprintf("./email-templates/%s", m.Template)) + if err != nil { + app.ErrorLog.Println(err) + email.SetBody(mail.TextHTML, m.Content) + } else { + mailTemplate := string(data) + msgToSend := strings.Replace(mailTemplate, "[%body%]", m.Content, 1) + email.SetBody(mail.TextHTML, msgToSend) + } + } err = email.Send(client) if err != nil { diff --git a/email-templates/drip.html b/email-templates/drip.html new file mode 100644 index 0000000..0d6773d --- /dev/null +++ b/email-templates/drip.html @@ -0,0 +1,1524 @@ + + + + + + + Title + + + + + + + + + + + +
+
+ + + + + + +
 
+ + + + + + +
+ + + + + + +
+ + + + + +
+
+
+ + + + + + +
+ + + + + + +
 
+
+ + + + + + +
 
+ + + + + + +
+ + + + + +
+

Hello, world!

+
+
+
+ + + + + + +
+ + + + + +
+

[%body%]

+
+
+ + + + + + + +
+
+
+ + + diff --git a/internal/handlers/handlers.go b/internal/handlers/handlers.go index 87582cd..f3bdc7c 100644 --- a/internal/handlers/handlers.go +++ b/internal/handlers/handlers.go @@ -2,6 +2,7 @@ package handlers import ( "encoding/json" + "fmt" "go-udemy-web-1/internal/config" "go-udemy-web-1/internal/driver" "go-udemy-web-1/internal/forms" @@ -172,6 +173,42 @@ func (m *Repository) PostMakeReservation(w http.ResponseWriter, r *http.Request) return } + // send notif to guest + htmlMessage := fmt.Sprintf(` + Reservation Confirmation
+ Dear %s:
+ This is to confirm your reservation from %s to %s. + + `, reservation.FirstName, reservation.StartDate.Format("2006-01-02"), + reservation.EndDate.Format("2006-01-02")) + + msg := models.MailData{ + To: reservation.Email, + From: "me@here.com", + Subject: "Reservation Confirmation", + Content: htmlMessage, + Template: "drip.html", + } + + m.App.MailChan <- msg + + // send notif to property owner + htmlMessage = fmt.Sprintf(` + Reservation Notification
+ A reservation has been made for %s from %s to %s. + + `, reservation.Room.RoomName, reservation.StartDate.Format("2006-01-02"), + reservation.EndDate.Format("2006-01-02")) + + msg = models.MailData{ + To: "me@here.com", + From: "me@here.com", + Subject: "Reservation Notification", + Content: htmlMessage, + } + + m.App.MailChan <- msg + m.App.Session.Put(r.Context(), "reservation", reservation) http.Redirect(w, r, "/reservation-summary", http.StatusSeeOther) diff --git a/internal/models/models.go b/internal/models/models.go index d244557..b1167ce 100644 --- a/internal/models/models.go +++ b/internal/models/models.go @@ -63,8 +63,9 @@ type RoomRestriction struct { // MailData holds an email message type MailData struct { - To string - From string - Subject string - Content string + To string + From string + Subject string + Content string + Template string }