Commit 3445e0ef authored by Mayuri Shinde's avatar Mayuri Shinde
Browse files

SendMailWithHandlebar

parent b6e8490a
Branches
Tags
2 merge requests!54Devbranch,!52Email,SMS Notification and HashAndSalt method
Showing with 142 additions and 11 deletions
......@@ -12,6 +12,7 @@ import (
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/configmdl"
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/errormdl"
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/loggermdl"
raymond "github.com/aymerick/raymond"
)
type EmailConfig struct {
......@@ -62,8 +63,9 @@ func NewEmail(to, cc, bcc, attachments []string, from, subject, body string) *Em
}
// SendMail - send email service sends email as per the given html template and data
//templateData can contain any type of values, including array, slice, map, struct and func
func (email *Email) SendMail(templateFilePath string, templateData interface{}) error {
err := email.parseTemplate(templateFilePath, templateData)
err := email.parseTemplateFile(templateFilePath, templateData)
if errormdl.CheckErr(err) != nil {
loggermdl.LogError(err)
return err
......@@ -75,21 +77,51 @@ func (email *Email) SendMail(templateFilePath string, templateData interface{})
return nil
}
func (email *Email) parseTemplate(templateFilePath string, templateData interface{}) error {
func (email *Email) parseTemplateFile(templateFilePath string, templateData interface{}) error {
tmplt, err := template.ParseFiles(templateFilePath)
if errormdl.CheckErr(err) != nil {
loggermdl.LogError("error occured while calling ParseFiles: ", errormdl.CheckErr(err))
loggermdl.LogError("error occured while calling parseTemplateFile: ", errormdl.CheckErr(err))
return err
}
buffer := new(bytes.Buffer)
if err = tmplt.Execute(buffer, templateData); errormdl.CheckErr1(err) != nil {
loggermdl.LogError("error occured while calling parseTemplate: ", errormdl.CheckErr1(err))
loggermdl.LogError("error occured while calling parseTemplateFile: ", errormdl.CheckErr1(err))
return err
}
email.body = buffer.String()
return nil
}
// SendMailWithHandlebar - send email service sends email as per the given html text and data
//templateData can contain any type of values, including array, slice, map, struct and func
func (email *Email) SendMailWithHandlebar(templateText string, templateData interface{}) error {
err := email.parseTemplateText(templateText, templateData)
if errormdl.CheckErr(err) != nil {
loggermdl.LogError(err)
return err
}
if err := email.Send(); errormdl.CheckErr1(err) != nil {
loggermdl.LogError("error occured while calling SendMailWithHandlebar: ", errormdl.CheckErr1(err))
return errormdl.Wrap("Failed to send the email to: " + strings.Join(email.to, ", "))
}
return nil
}
func (email *Email) parseTemplateText(templateText string, templateData interface{}) error {
tmplt, err := raymond.Parse(templateText)
if errormdl.CheckErr(err) != nil {
loggermdl.LogError("error occured while calling parseTemplateText: ", errormdl.CheckErr(err))
return err
}
emailbody, err := tmplt.Exec(templateData)
if errormdl.CheckErr1(err) != nil {
loggermdl.LogError("error occured while calling parseTemplateText: ", errormdl.CheckErr1(err))
return err
}
email.body = emailbody
return nil
}
// Send -send email
func (email *Email) Send() error {
message := gomail.NewMessage()
......@@ -105,7 +137,7 @@ func (email *Email) Send() error {
dialer := gomail.Dialer{Host: config.Server, Port: config.Port, Username: config.Username, Password: config.Password, SSL: config.SSL}
dialer.TLSConfig = &tls.Config{InsecureSkipVerify: !config.SSL}
if err := dialer.DialAndSend(message); errormdl.CheckErr1(err) != nil {
loggermdl.LogError("error occured while calling DialAndSend: ", errormdl.CheckErr1(err))
loggermdl.LogError("error occured while calling Send(): ", errormdl.CheckErr1(err))
return err
}
return nil
......
......@@ -27,7 +27,7 @@ func TestSendMail(t *testing.T) {
}
from := "mayuris@mkcl.org"
subject := "Test Email gomail"
to := []string{"mayuri92shinde@gmail.com", "mayuri2288shinde@gmail.com"}
to := []string{"mayuri92shinde@gmail.com"}
// cc := []string{"onkarh@mkcl.org", "kumargauravs@mkcl.org"}
// bcc := []string{"Prajkty@mkcl.org", "rakeshd@mkcl.org"}
//attachments := []string{"go.jpg"}
......@@ -45,7 +45,7 @@ func TestSendMailErrorWrongTemplt(t *testing.T) {
}
from := "mayuris@mkcl.org"
subject := "Test Email gomail"
to := []string{"mayuri92shinde@gmail.com", "mayuri2288shinde@gmail.com"}
to := []string{"mayuri92shinde@gmail.com"}
Init("email-config.toml")
mail := NewEmail(to, []string{}, []string{}, []string{}, from, subject, "")
err := mail.SendMail("wrong.html", persondata)
......@@ -60,7 +60,7 @@ func TestSendMailError(t *testing.T) {
}
from := "mayuris@mkcl.org"
subject := "Test Email gomail"
to := []string{"mayuri92shinde@gmail.com", "mayuri2288shinde@gmail.com"}
to := []string{"mayuri92shinde@gmail.com"}
Init("email-config.toml")
mail := NewEmail(to, []string{}, []string{}, []string{}, from, subject, "")
errormdl.IsTestingNegetiveCaseOn1 = true
......@@ -77,7 +77,7 @@ func TestSendMailError1(t *testing.T) {
}
from := "mayuris@mkcl.org"
subject := "Test Email gomail"
to := []string{"mayuri92shinde@gmail.com", "mayuri2288shinde@gmail.com"}
to := []string{"mayuri92shinde@gmail.com"}
Init("email-config.toml")
mail := NewEmail(to, []string{}, []string{}, []string{}, from, subject, "")
errormdl.IsTestingNegetiveCaseOn1 = true
......@@ -94,13 +94,112 @@ func BenchmarkSendMail(b *testing.B) {
}
from := "mayuris@mkcl.org"
subject := "Test Email smtp"
to := []string{"mayuri92shinde@gmail.com", "mayuri2288shinde@gmail.com"}
to := []string{"mayuri92shinde@gmail.com"}
Init("email-config.toml")
mail := NewEmail(to, []string{}, []string{}, []string{}, from, subject, "")
for i := 0; i < b.N; i++ {
mail.SendMail("email-template.html", persondata)
}
}
func TestSendMailWithHandlebar(t *testing.T) {
tpl1 := `<div class="post">
<h1>By {{author.firstName}} {{author.lastName}}</h1>
<div class="body">{{body}}</div>
<h1>Comments</h1>
{{#each comments}}
<h2>By {{author.firstName}} {{author.lastName}}</h2>
<div class="body">{{content}}</div>
{{/each}}
</div>`
type Person struct {
FirstName string
LastName string
}
type Comment struct {
Author Person
Body string `handlebars:"content"`
}
type Post struct {
Author Person
Body string
Comments []Comment
}
ctx1 := Post{
Person{"Jean", "Valjean"},
"Life is difficult",
[]Comment{
Comment{
Person{"Marcel", "Beliveau"},
"LOL!",
},
},
}
from := "mayuris@mkcl.org"
subject := "Test SendMailWithHandlebar"
to := []string{"mayuri92shinde@gmail.com"}
Init("email-config.toml")
mail := NewEmail(to, []string{}, []string{}, []string{}, from, subject, "")
err := mail.SendMailWithHandlebar(tpl1, ctx1)
assert.NoError(t, err, "This should not return error")
}
func TestSendMailWithHandlebarError(t *testing.T) {
tpl2 := `<div class="entry">
<h1>{{title}}</h1>
<div class="body">
{{body}}
</div>
</div>
`
ctx2 := []map[string]string{
{
"title": "My New Post",
"body": "This is my first post!",
},
{
"title": "Here is another post",
"body": "This is my second post!",
},
}
from := "mayuris@mkcl.org"
subject := "Test SendMailWithHandlebarError"
to := []string{"mayuri92shinde@gmail.com"}
Init("email-config.toml")
mail := NewEmail(to, []string{}, []string{}, []string{}, from, subject, "")
errormdl.IsTestingNegetiveCaseOn = true
err := mail.SendMailWithHandlebar(tpl2, ctx2)
errormdl.IsTestingNegetiveCaseOn = false
assert.NoError(t, err, "This should not return error")
}
func TestSendMailWithHandlebarError1(t *testing.T) {
tpl1 := `<div class="entry">
<h1>{{title}}</h1>
<div class="body">
{{body}}
</div>
</div>
`
ctx1 := map[string]string{
"title": "My New Post",
"body": "This is my first post!",
}
from := "mayuris@mkcl.org"
subject := "Test SendMailWithHandlebarError1"
to := []string{"mayuri92shinde@gmail.com"}
Init("email-config.toml")
mail := NewEmail(to, []string{}, []string{}, []string{}, from, subject, "")
errormdl.IsTestingNegetiveCaseOn1 = true
err := mail.SendMailWithHandlebar(tpl1, ctx1)
errormdl.IsTestingNegetiveCaseOn1 = false
assert.Error(t, err, "This should return error")
}
// func BenchmarkSendMailSMTP(b *testing.B) {
// persondata := Person{
......@@ -110,7 +209,7 @@ func BenchmarkSendMail(b *testing.B) {
// }
// from := "mayuris@mkcl.org"
// subject := "Test Email smtp"
// to := []string{"mayuri92shinde@gmail.com", "mayuri2288shinde@gmail.com"}
// to := []string{"mayuri92shinde@gmail.com"}
// Init("email-config.toml")
// mail := NewEmail(to, []string{}, []string{}, []string{}, from, subject, "")
// for i := 0; i < b.N; i++ {
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment