11package utils
22
33import (
4- "fmt"
4+ "bytes"
5+ "encoding/json"
6+ "html/template"
57
68 "github.com/authorizerdev/authorizer/server/constants"
79 "github.com/authorizerdev/authorizer/server/email"
@@ -15,10 +17,9 @@ func SendVerificationMail(toEmail, token string) error {
1517 Receiver := []string {toEmail }
1618
1719 Subject := "Please verify your email"
18- message := fmt . Sprintf ( `
20+ message := `
1921 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2022 <html xmlns="http://www.w3.org/1999/xhtml" xmlns:o="urn:schemas-microsoft-com:office:office">
21-
2223 <head>
2324 <meta charset="UTF-8">
2425 <meta content="width=device-width, initial-scale=1" name="viewport">
@@ -41,7 +42,6 @@ func SendVerificationMail(toEmail, token string) error {
4142 </xml>
4243 <![endif]-->
4344 </head>
44-
4545 <body style="font-family: sans-serif;">
4646 <div class="es-wrapper-color">
4747 <!--[if gte mso 9]>
@@ -68,14 +68,15 @@ func SendVerificationMail(toEmail, token string) error {
6868 <table width="100%%" cellspacing="0" cellpadding="0">
6969 <tbody>
7070 <tr>
71- <td class="esd-block-image es-m-txt-c es-p5b" style="font-size:0;padding:10px" align="center"><a target="_blank"><img src="%s " alt="icon" style="display: block;" title="icon" width="30"></a></td>
71+ <td class="esd-block-image es-m-txt-c es-p5b" style="font-size:0;padding:10px" align="center"><a target="_blank" clicktracking="off" ><img src="{{.OrgLogo}} " alt="icon" style="display: block;" title="icon" width="30"></a></td>
7272 </tr>
7373
7474 <tr style="background: rgb(249,250,251);padding: 10px;margin-bottom:10px;border-radius:5px;">
7575 <td class="esd-block-text es-m-txt-c es-p15t" align="center" style="padding:10px;padding-bottom:30px;">
7676 <p>Hey there 👋</p>
77- <p>We received a request to sign-up / login for <b>%s</b>. If this is correct, please confirm your email address by clicking the button below.</p> <br/>
78- <a href="%s" class="es-button" target="_blank" style="text-decoration: none;padding:10px 15px;background-color: rgba(59,130,246,1);color: #fff;font-size: 1em;border-radius:5px;">Confirm Email</a>
77+ <p>We received a request to sign-up / login for <b>{{.OrgName}}</b>. If this is correct, please confirm your email address by clicking the button below.</p> <br/>
78+ <a
79+ clicktracking="off" href="{{.AuthUrl}}" class="es-button" target="_blank" style="text-decoration: none;padding:10px 15px;background-color: rgba(59,130,246,1);color: #fff;font-size: 1em;border-radius:5px;">Confirm Email</a>
7980 </td>
8081 </tr>
8182 </tbody>
@@ -100,10 +101,15 @@ func SendVerificationMail(toEmail, token string) error {
100101 <div style="position: absolute; left: -9999px; top: -9999px; margin: 0px;"></div>
101102 </body>
102103 </html>
103- ` , constants .ORGANIZATION_LOGO , constants .ORGANIZATION_NAME , constants .AUTHORIZER_URL + "/verify_email" + "?token=" + token )
104- bodyMessage := sender .WriteHTMLEmail (Receiver , Subject , message )
105-
106- return sender .SendMail (Receiver , Subject , bodyMessage )
104+ `
105+ data := make (map [string ]interface {}, 3 )
106+ data ["OrgLogo" ] = constants .ORGANIZATION_LOGO
107+ data ["OrgName" ] = constants .ORGANIZATION_NAME
108+ data ["AuthUrl" ] = constants .AUTHORIZER_URL + "/verify_email?token=" + token
109+ message = AddEmailTemplate (message , data , "verify_email.tmpl" )
110+ // bodyMessage := sender.WriteHTMLEmail(Receiver, Subject, message)
111+
112+ return sender .SendMail (Receiver , Subject , message )
107113}
108114
109115// SendForgotPasswordMail to send verification email
@@ -119,10 +125,9 @@ func SendForgotPasswordMail(toEmail, token, host string) error {
119125
120126 Subject := "Reset Password"
121127
122- message := fmt . Sprintf ( `
128+ message := `
123129 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
124130 <html xmlns="http://www.w3.org/1999/xhtml" xmlns:o="urn:schemas-microsoft-com:office:office">
125-
126131 <head>
127132 <meta charset="UTF-8">
128133 <meta content="width=device-width, initial-scale=1" name="viewport">
@@ -145,7 +150,6 @@ func SendForgotPasswordMail(toEmail, token, host string) error {
145150 </xml>
146151 <![endif]-->
147152 </head>
148-
149153 <body style="font-family: sans-serif;">
150154 <div class="es-wrapper-color">
151155 <!--[if gte mso 9]>
@@ -172,14 +176,14 @@ func SendForgotPasswordMail(toEmail, token, host string) error {
172176 <table width="100%%" cellspacing="0" cellpadding="0">
173177 <tbody>
174178 <tr>
175- <td class="esd-block-image es-m-txt-c es-p5b" style="font-size:0;padding:10px" align="center"><a target="_blank"><img src="%s " alt="icon" style="display: block;" title="icon" width="30"></a></td>
179+ <td class="esd-block-image es-m-txt-c es-p5b" style="font-size:0;padding:10px" align="center"><a target="_blank" clicktracking="off" ><img src="{{.OrgLogo}} " alt="icon" style="display: block;" title="icon" width="30"></a></td>
176180 </tr>
177181
178182 <tr style="background: rgb(249,250,251);padding: 10px;margin-bottom:10px;border-radius:5px;">
179183 <td class="esd-block-text es-m-txt-c es-p15t" align="center" style="padding:10px;padding-bottom:30px;">
180184 <p>Hey there 👋</p>
181- <p>We received a request to reset password for email: <b>%s </b>. If this is correct, please reset the password clicking the button below.</p> <br/>
182- <a href="%s " class="es-button" target="_blank" style="text-decoration: none;padding:10px 15px;background-color: rgba(59,130,246,1);color: #fff;font-size: 1em;border-radius:5px;">Reset Password</a>
185+ <p>We received a request to reset password for email: <b>{{.ToEmail}} </b>. If this is correct, please reset the password clicking the button below.</p> <br/>
186+ <a clicktracking="off" href="{{.AuthUrl}} " class="es-button" target="_blank" style="text-decoration: none;padding:10px 15px;background-color: rgba(59,130,246,1);color: #fff;font-size: 1em;border-radius:5px;">Reset Password</a>
183187 </td>
184188 </tr>
185189 </tbody>
@@ -204,9 +208,28 @@ func SendForgotPasswordMail(toEmail, token, host string) error {
204208 <div style="position: absolute; left: -9999px; top: -9999px; margin: 0px;"></div>
205209 </body>
206210 </html>
207- ` , constants . ORGANIZATION_LOGO , toEmail , constants . RESET_PASSWORD_URL + "?token=" + token )
211+ `
208212
209- bodyMessage := sender .WriteHTMLEmail (Receiver , Subject , message )
213+ data := make (map [string ]interface {}, 3 )
214+ data ["OrgLogo" ] = constants .ORGANIZATION_LOGO
215+ data ["ToEmail" ] = constants .ORGANIZATION_NAME
216+ data ["AuthUrl" ] = constants .RESET_PASSWORD_URL + "?token=" + token
217+ message = AddEmailTemplate (message , data , "reset_password_email.tmpl" )
210218
211- return sender .SendMail (Receiver , Subject , bodyMessage )
219+ return sender .SendMail (Receiver , Subject , message )
220+ }
221+
222+ func AddEmailTemplate (a string , b map [string ]interface {}, templateName string ) string {
223+ tmpl , err := template .New (templateName ).Parse (a )
224+ if err != nil {
225+ output , _ := json .Marshal (b )
226+ return string (output )
227+ }
228+ buf := & bytes.Buffer {}
229+ err = tmpl .Execute (buf , b )
230+ if err != nil {
231+ panic (err )
232+ }
233+ s := buf .String ()
234+ return s
212235}
0 commit comments