From 1e3a02673c06372756e4142dee501f4a3f84e0ce Mon Sep 17 00:00:00 2001 From: Jordan Wright Date: Fri, 12 Feb 2016 22:19:59 -0600 Subject: [PATCH] Attachments are now fetched on GetTemplateByName Attachments are now attached to emails (whoops!) --- models/template.go | 8 ++++++++ worker/worker.go | 18 ++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/models/template.go b/models/template.go index e66f74bb..cb5f12a6 100644 --- a/models/template.go +++ b/models/template.go @@ -83,6 +83,14 @@ func GetTemplateByName(n string, uid int64) (Template, error) { if err != nil { Logger.Println(err) } + err = db.Where("template_id=?", t.Id).Find(&t.Attachments).Error + if err != nil && err != gorm.RecordNotFound { + Logger.Println(err) + return t, err + } + if err == nil && len(t.Attachments) == 0 { + t.Attachments = make([]Attachment, 0) + } return t, err } diff --git a/worker/worker.go b/worker/worker.go index fdbdb64f..89a49f84 100644 --- a/worker/worker.go +++ b/worker/worker.go @@ -3,6 +3,7 @@ package worker import ( "bytes" "crypto/tls" + "encoding/base64" "encoding/json" "errors" "log" @@ -113,6 +114,14 @@ func processCampaign(c *models.Campaign) { e.Subject = string(subjBuff.Bytes()) Logger.Println("Creating email using template") e.To = []string{t.Email} + // Attach the files + for _, a := range c.Template.Attachments { + decoder := base64.NewDecoder(base64.StdEncoding, strings.NewReader(a.Content)) + _, err = e.Attach(decoder, a.Name, a.Type) + if err != nil { + Logger.Println(err) + } + } Logger.Printf("Sending Email to %s\n", t.Email) err = e.SendWithTLS(c.SMTP.Host, auth, tc) if err != nil { @@ -206,6 +215,15 @@ func SendTestEmail(s *models.SendTestEmailRequest) error { } e.Subject = string(subjBuff.Bytes()) e.To = []string{s.Email} + // Attach the files + for _, a := range s.Template.Attachments { + Logger.Printf("Attaching %s\n", a.Name) + decoder := base64.NewDecoder(base64.StdEncoding, strings.NewReader(a.Content)) + _, err = e.Attach(decoder, a.Name, a.Type) + if err != nil { + Logger.Println(err) + } + } Logger.Printf("Sending Email to %s\n", s.Email) err = e.SendWithTLS(s.SMTP.Host, auth, t) if err != nil {