send simple test email to my email on server startup
This commit is contained in:
@@ -2,19 +2,24 @@ package email
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"path/filepath"
|
||||
"text/template"
|
||||
)
|
||||
|
||||
func RenderTemplate(baseURL string, path string, data any) (string, error) {
|
||||
// funcMap := template.FuncMap{
|
||||
// "asset": func(p string) string {
|
||||
// return baseURL + "/" + strings.TrimPrefix(p, "/")
|
||||
// },
|
||||
// }
|
||||
func RenderTemplate(path string, data any, funcMap template.FuncMap) (string, error) {
|
||||
tmpl := template.New("")
|
||||
|
||||
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
|
||||
err := tmpl.Execute(&buf, data)
|
||||
err = tmpl.ExecuteTemplate(&buf, filepath.Base(path), data)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
@@ -5,10 +5,12 @@ import (
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"astraltech.xyz/accountmanager/src/components"
|
||||
"astraltech.xyz/accountmanager/src/email"
|
||||
"astraltech.xyz/accountmanager/src/helpers"
|
||||
"astraltech.xyz/accountmanager/src/ldap"
|
||||
"astraltech.xyz/accountmanager/src/logging"
|
||||
@@ -19,6 +21,7 @@ var (
|
||||
ldapServer ldap.LDAPServer
|
||||
serverConfig *ServerConfig
|
||||
sessionManager *session.SessionManager
|
||||
noReplyEmail email.EmailAccount
|
||||
)
|
||||
|
||||
type UserData struct {
|
||||
@@ -250,6 +253,28 @@ func main() {
|
||||
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{
|
||||
URL: serverConfig.LDAPConfig.LDAPURL,
|
||||
StartTLS: serverConfig.LDAPConfig.Security == "tls",
|
||||
|
||||
Reference in New Issue
Block a user