Sending mails using Foundation templates
This commit is contained in:
		@ -59,14 +59,6 @@ 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,8 +1,11 @@
 | 
				
			|||||||
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"
 | 
				
			||||||
@ -32,7 +35,19 @@ 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)
 | 
				
			||||||
	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)
 | 
						err = email.Send(client)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										1524
									
								
								email-templates/drip.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1524
									
								
								email-templates/drip.html
									
									
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							@ -2,6 +2,7 @@ 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"
 | 
				
			||||||
@ -172,6 +173,42 @@ 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)
 | 
				
			||||||
 | 
				
			|||||||
@ -63,8 +63,9 @@ 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
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user