Browse Source

Attachments are now fetched on GetTemplateByName

Attachments are now attached to emails (whoops!)
pull/147/head
Jordan Wright 5 years ago
parent
commit
1e3a02673c
2 changed files with 26 additions and 0 deletions
  1. +8
    -0
      models/template.go
  2. +18
    -0
      worker/worker.go

+ 8
- 0
models/template.go View File

@ -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
}

+ 18
- 0
worker/worker.go View File

@ -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 {

Loading…
Cancel
Save