Sending mails using Foundation templates
This commit is contained in:
		@ -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, <strong>world</strong>",
 | 
			
		||||
	}
 | 
			
		||||
	app.MailChan <- msg
 | 
			
		||||
 | 
			
		||||
	// change this to true when in production
 | 
			
		||||
	app.InProduction = false
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -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 {
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user