send simple test email to my email on server startup
This commit is contained in:
25
data/email-templates/expired-password.html
Normal file
25
data/email-templates/expired-password.html
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
<!doctype html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8" />
|
||||||
|
<title>Hi</title>
|
||||||
|
<style>
|
||||||
|
body {
|
||||||
|
font-family: Arial, sans-serif;
|
||||||
|
text-align: center;
|
||||||
|
margin-top: 50px;
|
||||||
|
}
|
||||||
|
img {
|
||||||
|
margin-top: 20px;
|
||||||
|
width: 150px;
|
||||||
|
height: 150px;
|
||||||
|
border-radius: 50%;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>Hi {{.Username}}</h1>
|
||||||
|
|
||||||
|
<img src="{{avatar .Username}}" alt="Profile Picture" />
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@@ -2,19 +2,24 @@ package email
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"path/filepath"
|
||||||
"text/template"
|
"text/template"
|
||||||
)
|
)
|
||||||
|
|
||||||
func RenderTemplate(baseURL string, path string, data any) (string, error) {
|
func RenderTemplate(path string, data any, funcMap template.FuncMap) (string, error) {
|
||||||
// funcMap := template.FuncMap{
|
tmpl := template.New("")
|
||||||
// "asset": func(p string) string {
|
|
||||||
// return baseURL + "/" + strings.TrimPrefix(p, "/")
|
if funcMap != nil {
|
||||||
// },
|
tmpl = tmpl.Funcs(funcMap)
|
||||||
// }
|
}
|
||||||
|
|
||||||
|
tmpl, err := tmpl.ParseFiles(path)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
tmpl := template.Must(template.ParseFiles(path))
|
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
err := tmpl.Execute(&buf, data)
|
err = tmpl.ExecuteTemplate(&buf, filepath.Base(path), data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,10 +5,12 @@ import (
|
|||||||
"html/template"
|
"html/template"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"astraltech.xyz/accountmanager/src/components"
|
"astraltech.xyz/accountmanager/src/components"
|
||||||
|
"astraltech.xyz/accountmanager/src/email"
|
||||||
"astraltech.xyz/accountmanager/src/helpers"
|
"astraltech.xyz/accountmanager/src/helpers"
|
||||||
"astraltech.xyz/accountmanager/src/ldap"
|
"astraltech.xyz/accountmanager/src/ldap"
|
||||||
"astraltech.xyz/accountmanager/src/logging"
|
"astraltech.xyz/accountmanager/src/logging"
|
||||||
@@ -19,6 +21,7 @@ var (
|
|||||||
ldapServer ldap.LDAPServer
|
ldapServer ldap.LDAPServer
|
||||||
serverConfig *ServerConfig
|
serverConfig *ServerConfig
|
||||||
sessionManager *session.SessionManager
|
sessionManager *session.SessionManager
|
||||||
|
noReplyEmail email.EmailAccount
|
||||||
)
|
)
|
||||||
|
|
||||||
type UserData struct {
|
type UserData struct {
|
||||||
@@ -250,6 +253,28 @@ func main() {
|
|||||||
log.Fatal("Could not load server config")
|
log.Fatal("Could not load server config")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
noReplyEmail = email.CreateEmailAccount(email.EmailAccountData{
|
||||||
|
Username: serverConfig.EmailConfig.Username,
|
||||||
|
Password: serverConfig.EmailConfig.Password,
|
||||||
|
Email: serverConfig.EmailConfig.Email,
|
||||||
|
}, serverConfig.EmailConfig.SMTPURL, serverConfig.EmailConfig.SMTPPort)
|
||||||
|
|
||||||
|
funcs := template.FuncMap{
|
||||||
|
"avatar": func(username string) string {
|
||||||
|
return serverConfig.WebserverConfig.BaseURL + "/avatar?user=" + url.QueryEscape(username)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
data := map[string]any{
|
||||||
|
"Username": "gawells",
|
||||||
|
}
|
||||||
|
|
||||||
|
email_template, err := email.RenderTemplate("./data/email-templates/expired-password.html", data, funcs)
|
||||||
|
if err != nil {
|
||||||
|
logging.Errorf("Failed to load email template: %s", err.Error())
|
||||||
|
}
|
||||||
|
noReplyEmail.SendEmail([]string{"gawells@astraltech.xyz"}, "Test", email_template)
|
||||||
|
|
||||||
ldapServer = ldap.LDAPServer{
|
ldapServer = ldap.LDAPServer{
|
||||||
URL: serverConfig.LDAPConfig.LDAPURL,
|
URL: serverConfig.LDAPConfig.LDAPURL,
|
||||||
StartTLS: serverConfig.LDAPConfig.Security == "tls",
|
StartTLS: serverConfig.LDAPConfig.Security == "tls",
|
||||||
|
|||||||
Reference in New Issue
Block a user