Compare commits

..

No commits in common. "c88974788f8f03215b7fa6295f3db64da3bf0c67" and "d40233b4ba70a39c5b8bc559f747e58b4b3d8ab2" have entirely different histories.

6 changed files with 13 additions and 1596 deletions

View File

@ -59,6 +59,14 @@ func run() (*driver.DB, error) {
listenForMail()
msg := models.MailData{
To: "John@do.do",
From: "me@here.com",
Subject: "mail",
Content: "Hello, <strong>world</strong>",
}
app.MailChan <- msg
// change this to true when in production
app.InProduction = false

View File

@ -1,11 +1,8 @@
package main
import (
"fmt"
"go-udemy-web-1/internal/models"
"log"
"os"
"strings"
"time"
mail "github.com/xhit/go-simple-mail"
@ -35,19 +32,7 @@ func sendMsg(m models.MailData) {
email := mail.NewMSG()
email.SetFrom(m.From).AddTo(m.To).SetSubject(m.Subject)
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)
}
}
email.SetBody(mail.TextHTML, m.Content)
err = email.Send(client)
if err != nil {

File diff suppressed because it is too large Load Diff

View File

@ -2,7 +2,6 @@ package handlers
import (
"encoding/json"
"fmt"
"go-udemy-web-1/internal/config"
"go-udemy-web-1/internal/driver"
"go-udemy-web-1/internal/forms"
@ -173,42 +172,6 @@ func (m *Repository) PostMakeReservation(w http.ResponseWriter, r *http.Request)
return
}
// send notif to guest
htmlMessage := fmt.Sprintf(`
<strong>Reservation Confirmation</strong><br>
Dear %s: <br>
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(`
<strong>Reservation Notification</strong><br>
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)

View File

@ -39,12 +39,6 @@ func TestMain(m *testing.M) {
app.Session = session
mailChan := make(chan models.MailData)
app.MailChan = mailChan
defer close(mailChan)
listenForMail()
tc, err := CreateTestTemplateCache()
if err != nil {
log.Fatalf("cannot create template cache: %s", err)
@ -141,11 +135,3 @@ func CreateTestTemplateCache() (map[string]*template.Template, error) {
return myCache, nil
}
func listenForMail() {
go func() {
for {
<-app.MailChan
}
}()
}

View File

@ -63,9 +63,8 @@ type RoomRestriction struct {
// MailData holds an email message
type MailData struct {
To string
From string
Subject string
Content string
Template string
To string
From string
Subject string
Content string
}