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]"}
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
if err != nil {
if err != gorm.ErrRecordNotFound {

View File

@ -129,8 +129,6 @@ func processCampaign(c *models.Campaign) {
}
// Parse the templates
var subjBuff bytes.Buffer
var htmlBuff bytes.Buffer
var textBuff bytes.Buffer
tmpl, err := template.New("text_template").Parse(c.Template.Subject)
if err != nil {
Logger.Println(err)
@ -142,24 +140,34 @@ func processCampaign(c *models.Campaign) {
e.SetHeader("Subject", subjBuff.String())
Logger.Println("Creating email using template")
e.SetHeader("To", t.Email)
tmpl, err = template.New("text_template").Parse(c.Template.Text)
if err != nil {
Logger.Println(err)
if c.Template.Text != "" {
var textBuff bytes.Buffer
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 err != nil {
Logger.Println(err)
if c.Template.HTML != "" {
var htmlBuff bytes.Buffer
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
for _, a := range c.Template.Attachments {
e.Attach(func(a models.Attachment) (string, gomail.FileSetting) {