Skip to content

Commit

Permalink
Added template support for html/text
Browse files Browse the repository at this point in the history
  • Loading branch information
mrz1836 committed Oct 13, 2019
1 parent 4081c99 commit aa9cc61
Show file tree
Hide file tree
Showing 47 changed files with 14,823 additions and 1 deletion.
48 changes: 48 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

92 changes: 91 additions & 1 deletion email.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
package gomail

import (
"bytes"
"fmt"
"html/template"
"io"
"io/ioutil"
"path/filepath"

"github.com/aymerick/douceur/inliner"
)

// Email represents the fields of the email to send
type Email struct {
Attachments []Attachment `json:"attachments" mapstructure:"attachments"`
AutoText bool `json:"auto_text" mapstructure:"auto_text"`
FromAddress string `json:"from_address" mapstructure:"from_address"`
FromName string `json:"from_name" mapstructure:"from_name"`
HTMLContent string `json:"html_content" mapstructure:"html_content"`
Expand All @@ -17,9 +24,9 @@ type Email struct {
RecipientsBcc []string `json:"recipients_bcc" mapstructure:"recipients_bcc"`
RecipientsCc []string `json:"recipients_cc" mapstructure:"recipients_cc"`
ReplyToAddress string `json:"reply_to_address" mapstructure:"reply_to_address"`
Styles []byte `json:"styles" mapstructure:"styles"`
Subject string `json:"subject" mapstructure:"subject"`
Tags []string `json:"tags" mapstructure:"tags"`
AutoText bool `json:"auto_text" mapstructure:"auto_text"`
TrackClicks bool `json:"track_clicks" mapstructure:"track_clicks"`
TrackOpens bool `json:"track_opens" mapstructure:"track_opens"`
ViewContentLink bool `json:"view_content_link" mapstructure:"view_content_link"`
Expand All @@ -41,6 +48,89 @@ func (e *Email) AddAttachment(name, fileType string, reader io.Reader) {
})
}

// ApplyTemplates will take the template files and process them with the email data
func (e *Email) ApplyTemplates(htmlTemplate *template.Template, textTemplate *template.Template) (err error) {

// Start the buffer
var buffer bytes.Buffer

// Do we have an html template?
if htmlTemplate != nil {

// Read the struct into the HTML buffer
err = htmlTemplate.ExecuteTemplate(&buffer, htmlTemplate.Name(), e)
if err != nil {
return
}

// Turn the buffer to a string
e.HTMLContent = buffer.String()

// Reset the buffer to ""
buffer.Reset()
}

// Do we have a text template?
if textTemplate != nil {

// Read the struct into the text buffer
err = textTemplate.ExecuteTemplate(&buffer, textTemplate.Name(), e)
if err != nil {
return
}

// Turn the buffer to a string
e.PlainTextContent = buffer.String()
}

return
}

// ParseTemplate parse the template, fire error if parse fails
// This method returns the template which should be stored in memory for quick access
func (e *Email) ParseTemplate(filename string) (parsed *template.Template, err error) {
parsed = template.New(filepath.Base(filename))
return parsed.ParseFiles(filename)
}

// ParseTemplateWithStyles parse the templates with inline style injection (html)
// This method returns the template which should be stored in memory for quick access
func (e *Email) ParseTemplateWithStyles(htmlLocation string, emailStyles []byte) (htmlTemplate *template.Template, err error) {

// Read HTML template file
var tempBytes []byte
tempBytes, err = ioutil.ReadFile(htmlLocation)
if err != nil {
err = fmt.Errorf("")
return
}

// Do we have styles to replace?
if bytes.Contains(tempBytes, []byte("{{.Styles}}")) {

// Inject styles
tempBytes = bytes.Replace(tempBytes, []byte("{{.Styles}}"), emailStyles, -1)
var tempString string
tempString, err = inliner.Inline(string(tempBytes))
if err != nil {
return
}

// Replace the string with template
htmlTemplate, err = e.ParseTemplate(htmlLocation)
if err != nil {
return
}
_, err = htmlTemplate.Parse(tempString)

} else {
// Set the html template (didn't find styles
htmlTemplate, err = e.ParseTemplate(htmlLocation)
}

return
}

// NewEmail creates a new email using defaults from the service configuration
func (m *MailService) NewEmail() (email *Email) {

Expand Down
12 changes: 12 additions & 0 deletions vendor/github.com/PuerkitoBio/goquery/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

124 changes: 124 additions & 0 deletions vendor/github.com/PuerkitoBio/goquery/array.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit aa9cc61

Please sign in to comment.