Cleaning up some email sending bugs. Fixes #329

pull/336/head
Jordan Wright 2016-08-02 23:18:53 -05:00
parent 0d8a793550
commit b403e97eee
2 changed files with 31 additions and 18 deletions

View File

@ -140,6 +140,11 @@ func (c *Campaign) getDetails() error {
c.Template = Template{Name: "[Deleted]"} c.Template = Template{Name: "[Deleted]"}
Logger.Printf("%s: template not found for campaign\n", err) Logger.Printf("%s: template not found for campaign\n", err)
} }
err = db.Where("template_id=?", c.Template.Id).Find(&c.Template.Attachments).Error
if err != nil && err != gorm.ErrRecordNotFound {
Logger.Println(err)
return err
}
err = db.Table("pages").Where("id=?", c.PageId).Find(&c.Page).Error err = db.Table("pages").Where("id=?", c.PageId).Find(&c.Page).Error
if err != nil { if err != nil {
if err != gorm.ErrRecordNotFound { if err != gorm.ErrRecordNotFound {

View File

@ -129,8 +129,6 @@ func processCampaign(c *models.Campaign) {
} }
// Parse the templates // Parse the templates
var subjBuff bytes.Buffer var subjBuff bytes.Buffer
var htmlBuff bytes.Buffer
var textBuff bytes.Buffer
tmpl, err := template.New("text_template").Parse(c.Template.Subject) tmpl, err := template.New("text_template").Parse(c.Template.Subject)
if err != nil { if err != nil {
Logger.Println(err) Logger.Println(err)
@ -142,24 +140,34 @@ func processCampaign(c *models.Campaign) {
e.SetHeader("Subject", subjBuff.String()) e.SetHeader("Subject", subjBuff.String())
Logger.Println("Creating email using template") Logger.Println("Creating email using template")
e.SetHeader("To", t.Email) e.SetHeader("To", t.Email)
tmpl, err = template.New("text_template").Parse(c.Template.Text) if c.Template.Text != "" {
if err != nil { var textBuff bytes.Buffer
Logger.Println(err) tmpl, err = template.New("text_template").Parse(c.Template.Text)
if err != nil {
Logger.Println(err)
}
err = tmpl.Execute(&textBuff, td)
if err != nil {
Logger.Println(err)
}
e.SetBody("text/plain", textBuff.String())
} }
err = tmpl.Execute(&textBuff, td) if c.Template.HTML != "" {
if err != nil { var htmlBuff bytes.Buffer
Logger.Println(err) tmpl, err = template.New("html_template").Parse(c.Template.HTML)
if err != nil {
Logger.Println(err)
}
err = tmpl.Execute(&htmlBuff, td)
if err != nil {
Logger.Println(err)
}
if c.Template.Text == "" {
e.SetBody("text/html", htmlBuff.String())
} else {
e.AddAlternative("text/html", htmlBuff.String())
}
} }
e.SetBody("text/plain", textBuff.String())
tmpl, err = template.New("html_template").Parse(c.Template.HTML)
if err != nil {
Logger.Println(err)
}
err = tmpl.Execute(&htmlBuff, td)
if err != nil {
Logger.Println(err)
}
e.AddAlternative("text/html", htmlBuff.String())
// Attach the files // Attach the files
for _, a := range c.Template.Attachments { for _, a := range c.Template.Attachments {
e.Attach(func(a models.Attachment) (string, gomail.FileSetting) { e.Attach(func(a models.Attachment) (string, gomail.FileSetting) {