Skip to content
GitLab
Explore
Projects
Groups
Topics
Snippets
Projects
Groups
Topics
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
MKCLOS
Core Development Platform
corepkgv2
Commits
3445e0ef
Commit
3445e0ef
authored
6 years ago
by
Mayuri Shinde
Browse files
Options
Downloads
Patches
Plain Diff
SendMailWithHandlebar
parent
b6e8490a
Branches
Branches containing commit
Tags
Tags containing commit
2 merge requests
!54
Devbranch
,
!52
Email,SMS Notification and HashAndSalt method
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
notificationmdl/email/email.go
+37
-5
notificationmdl/email/email.go
notificationmdl/email/email_test.go
+105
-6
notificationmdl/email/email_test.go
with
142 additions
and
11 deletions
notificationmdl/email/email.go
+
37
−
5
View file @
3445e0ef
...
...
@@ -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
.
parseTemplate
File
(
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
)
parseTemplate
File
(
templateFilePath
string
,
templateData
interface
{})
error
{
tmplt
,
err
:=
template
.
ParseFiles
(
templateFilePath
)
if
errormdl
.
CheckErr
(
err
)
!=
nil
{
loggermdl
.
LogError
(
"error occured while calling
P
arseFile
s
: "
,
errormdl
.
CheckErr
(
err
))
loggermdl
.
LogError
(
"error occured while calling
p
arse
Template
File: "
,
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 parseTemplate
File
: "
,
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
DialAnd
Send: "
,
errormdl
.
CheckErr1
(
err
))
loggermdl
.
LogError
(
"error occured while calling Send
()
: "
,
errormdl
.
CheckErr1
(
err
))
return
err
}
return
nil
...
...
This diff is collapsed.
Click to expand it.
notificationmdl/email/email_test.go
+
105
−
6
View file @
3445e0ef
...
...
@@ -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++ {
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment
Menu
Explore
Projects
Groups
Topics
Snippets