test sending emain

This commit is contained in:
vinchent 2024-08-21 09:39:34 +02:00
parent 21c5f42aff
commit 2642f706bf
4 changed files with 55 additions and 2 deletions

View File

@ -424,4 +424,23 @@ func (app *application) SendPasswordResetEmail(w http.ResponseWriter, r *http.Re
data.Link = "http://www.vinchent.xyz" data.Link = "http://www.vinchent.xyz"
// send mail // send mail
err = app.SendMail(
"info@widgets.com",
"info@widgets.com",
"Password Reset Request",
"password-reset",
data,
)
if err != nil {
app.errorLog.Println(err)
app.badRequest(w, r, err)
return
}
var resp struct {
Error bool `json:"error"`
Message string `json:"message"`
}
app.writeJSON(w, http.StatusCreated, resp)
} }

View File

@ -14,7 +14,7 @@ import (
var emailTemplateFS embed.FS var emailTemplateFS embed.FS
func (app *application) SendMail(from, to, subject, tmpl string, data interface{}) error { func (app *application) SendMail(from, to, subject, tmpl string, data interface{}) error {
templateToRender := fmt.Sprintf("templates/%s.html.tmpl", tmpl) templateToRender := fmt.Sprintf("templates/%s.html.gohtml", tmpl)
t, err := template.New("email-html").ParseFS(emailTemplateFS, templateToRender) t, err := template.New("email-html").ParseFS(emailTemplateFS, templateToRender)
if err != nil { if err != nil {
@ -30,7 +30,7 @@ func (app *application) SendMail(from, to, subject, tmpl string, data interface{
formattedMessage := tpl.String() formattedMessage := tpl.String()
templateToRender = fmt.Sprintf("templates/%s.plain.tmpl", tmpl) templateToRender = fmt.Sprintf("templates/%s.plain.gohtml", tmpl)
t, err = template.New("email-plain").ParseFS(emailTemplateFS, templateToRender) t, err = template.New("email-plain").ParseFS(emailTemplateFS, templateToRender)
if err != nil { if err != nil {
app.errorLog.Println(err) app.errorLog.Println(err)

View File

@ -0,0 +1,22 @@
{{ define "body" }}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<p>Hello:</p>
<p>You recently requested a link to reset your password.</p>
<p>Click on the link below to get started:</p>
<p>
<a href="{{.Link}}">{{.Link}}</a>
</p>
<p>
--
<br>
Widget Co.
</p>
</body>
</html>
{{ end }}

View File

@ -0,0 +1,12 @@
{{ define "body" }}
Hello:
You recently requested a link to reset your password.
Click on the link below to get started:
{{.Link}}
--
Widget Co.
{{ end }}