Compare commits
No commits in common. "c88974788f8f03215b7fa6295f3db64da3bf0c67" and "d40233b4ba70a39c5b8bc559f747e58b4b3d8ab2" have entirely different histories.
c88974788f
...
d40233b4ba
@ -59,6 +59,14 @@ func run() (*driver.DB, error) {
|
|||||||
|
|
||||||
listenForMail()
|
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
|
// change this to true when in production
|
||||||
app.InProduction = false
|
app.InProduction = false
|
||||||
|
|
||||||
|
@ -1,11 +1,8 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"go-udemy-web-1/internal/models"
|
"go-udemy-web-1/internal/models"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
mail "github.com/xhit/go-simple-mail"
|
mail "github.com/xhit/go-simple-mail"
|
||||||
@ -35,19 +32,7 @@ func sendMsg(m models.MailData) {
|
|||||||
|
|
||||||
email := mail.NewMSG()
|
email := mail.NewMSG()
|
||||||
email.SetFrom(m.From).AddTo(m.To).SetSubject(m.Subject)
|
email.SetFrom(m.From).AddTo(m.To).SetSubject(m.Subject)
|
||||||
if m.Template == "" {
|
email.SetBody(mail.TextHTML, m.Content)
|
||||||
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)
|
err = email.Send(client)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -2,7 +2,6 @@ package handlers
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
|
||||||
"go-udemy-web-1/internal/config"
|
"go-udemy-web-1/internal/config"
|
||||||
"go-udemy-web-1/internal/driver"
|
"go-udemy-web-1/internal/driver"
|
||||||
"go-udemy-web-1/internal/forms"
|
"go-udemy-web-1/internal/forms"
|
||||||
@ -173,42 +172,6 @@ func (m *Repository) PostMakeReservation(w http.ResponseWriter, r *http.Request)
|
|||||||
return
|
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)
|
m.App.Session.Put(r.Context(), "reservation", reservation)
|
||||||
|
|
||||||
http.Redirect(w, r, "/reservation-summary", http.StatusSeeOther)
|
http.Redirect(w, r, "/reservation-summary", http.StatusSeeOther)
|
||||||
|
@ -39,12 +39,6 @@ func TestMain(m *testing.M) {
|
|||||||
|
|
||||||
app.Session = session
|
app.Session = session
|
||||||
|
|
||||||
mailChan := make(chan models.MailData)
|
|
||||||
app.MailChan = mailChan
|
|
||||||
defer close(mailChan)
|
|
||||||
|
|
||||||
listenForMail()
|
|
||||||
|
|
||||||
tc, err := CreateTestTemplateCache()
|
tc, err := CreateTestTemplateCache()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("cannot create template cache: %s", err)
|
log.Fatalf("cannot create template cache: %s", err)
|
||||||
@ -141,11 +135,3 @@ func CreateTestTemplateCache() (map[string]*template.Template, error) {
|
|||||||
|
|
||||||
return myCache, nil
|
return myCache, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func listenForMail() {
|
|
||||||
go func() {
|
|
||||||
for {
|
|
||||||
<-app.MailChan
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
|
@ -63,9 +63,8 @@ type RoomRestriction struct {
|
|||||||
|
|
||||||
// MailData holds an email message
|
// MailData holds an email message
|
||||||
type MailData struct {
|
type MailData struct {
|
||||||
To string
|
To string
|
||||||
From string
|
From string
|
||||||
Subject string
|
Subject string
|
||||||
Content string
|
Content string
|
||||||
Template string
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user