package models
import (
"bytes"
"errors"
"html/template"
"net/mail"
"time"
"github.com/jinzhu/gorm"
)
// Template models hold the attributes for an email template to be sent to targets
type Template struct {
Id int64 `json:"id" gorm:"column:id; primary_key:yes"`
UserId int64 `json:"-" gorm:"column:user_id"`
Name string `json:"name"`
EnvelopeSender string `json:"envelope_sender"`
Subject string `json:"subject"`
Text string `json:"text"`
HTML string `json:"html" gorm:"column:html"`
ModifiedDate time.Time `json:"modified_date"`
Attachments []Attachment `json:"attachments"`
}
// ErrTemplateNameNotSpecified is thrown when a template name is not specified
var ErrTemplateNameNotSpecified = errors.New("Template name not specified")
// ErrTemplateMissingParameter is thrown when a needed parameter is not provided
var ErrTemplateMissingParameter = errors.New("Need to specify at least plaintext or HTML content")
// Validate checks the given template to make sure values are appropriate and complete
func (t *Template) Validate() error {
switch {
case t.Name == "":
return ErrTemplateNameNotSpecified
case t.Text == "" && t.HTML == "":
return ErrTemplateMissingParameter
case t.EnvelopeSender != "":
_, err := mail.ParseAddress(t.EnvelopeSender)
if err != nil {
return err
}
}
var buff bytes.Buffer
// Test that the variables used in the template
// validate with no issues
td := struct {
Result
URL string
TrackingURL string
Tracker string
From string
}{
Result{
Email: "foo@bar.com",
FirstName: "Foo",
LastName: "Bar",
Position: "Test",
},
"http://foo.bar",
"http://foo.bar/track",
"