Compare commits
5 Commits
v0.0.5
...
6a985c1a84
| Author | SHA1 | Date | |
|---|---|---|---|
| 6a985c1a84 | |||
| 590ea73a92 | |||
| 8de145adbc | |||
| 8f0291bb8a | |||
| cde41b82b2 |
@@ -12,6 +12,14 @@
|
||||
"logo_path": "./data/astraltech_logo_large.png"
|
||||
},
|
||||
"server_config": {
|
||||
"port": 8080
|
||||
"port": 8080,
|
||||
"base_url": "https://profile.example.com"
|
||||
},
|
||||
"email_config": {
|
||||
"username": "noreply",
|
||||
"email": "noreply@example.com",
|
||||
"password": "",
|
||||
"smtp_url": "mx.example.com",
|
||||
"smtp_port": 587
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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>
|
||||
+14
-11
@@ -16,35 +16,38 @@ type EmailAccount struct {
|
||||
}
|
||||
|
||||
type EmailAccountData struct {
|
||||
username string
|
||||
password string
|
||||
email string
|
||||
Username string
|
||||
Password string
|
||||
Email string
|
||||
}
|
||||
|
||||
func createEmailAccount(accountData EmailAccountData, smtpHost string, smtpPort int) EmailAccount {
|
||||
logging.Debugf("Creating Email Account: \n\tUsername: %s\n\tEmail: %s\n\tSMTP Host: %s:%d", accountData.username, accountData.email, smtpHost, smtpPort)
|
||||
func CreateEmailAccount(accountData EmailAccountData, smtpHost string, smtpPort int) EmailAccount {
|
||||
logging.Debugf("Creating Email Account: \n\tUsername: %s\n\tEmail: %s\n\tSMTP Host: %s:%d", accountData.Username, accountData.Email, smtpHost, smtpPort)
|
||||
account := EmailAccount{
|
||||
email: accountData.email,
|
||||
email: accountData.Email,
|
||||
smtpHost: smtpHost,
|
||||
smtpPort: strconv.Itoa(smtpPort),
|
||||
}
|
||||
account.auth = smtp.PlainAuth("", accountData.username, accountData.password, smtpHost)
|
||||
account.auth = smtp.PlainAuth("", accountData.Username, accountData.Password, smtpHost)
|
||||
return account
|
||||
}
|
||||
|
||||
func sendEmail(account EmailAccount, toEmail []string, subject string, message string) {
|
||||
logging.Debugf("Sending an email from %s to %s", account.email, strings.Join(toEmail, ""))
|
||||
func (account *EmailAccount) SendEmail(toEmails []string, subject string, message string) {
|
||||
logging.Debugf("Sending an email from %s to %s", account.email, strings.Join(toEmails, ""))
|
||||
|
||||
ToEmailList := strings.Join(toEmail, "")
|
||||
ToEmailList := strings.Join(toEmails, "")
|
||||
|
||||
mime := "MIME-version: 1.0;\r\nContent-Type: text/html; charset=\"UTF-8\";\r\n\r\n"
|
||||
|
||||
messageData := []byte(
|
||||
"From: " + account.email + "\r\n" +
|
||||
"To: " + ToEmailList + "\r\n" +
|
||||
"Subject: " + subject + "\r\n" +
|
||||
mime +
|
||||
"\r\n" +
|
||||
message,
|
||||
)
|
||||
err := smtp.SendMail(account.smtpHost+":"+account.smtpPort, account.auth, account.email, toEmail, messageData)
|
||||
err := smtp.SendMail(account.smtpHost+":"+account.smtpPort, account.auth, account.email, toEmails, messageData)
|
||||
if err != nil {
|
||||
logging.Error("Failed to send email")
|
||||
logging.Error(err.Error())
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package email
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"path/filepath"
|
||||
"text/template"
|
||||
)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
err = tmpl.ExecuteTemplate(&buf, filepath.Base(path), data)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return buf.String(), nil
|
||||
}
|
||||
+11
-1
@@ -22,13 +22,23 @@ type StyleConfig struct {
|
||||
}
|
||||
|
||||
type WebserverConfig struct {
|
||||
Port int `json:"port"`
|
||||
Port int `json:"port"`
|
||||
BaseURL string `json:"base_url"`
|
||||
}
|
||||
|
||||
type EmailConfig struct {
|
||||
Username string `json:"username"`
|
||||
Email string `json:"email"`
|
||||
Password string `json:"password"`
|
||||
SMTPURL string `json:"smtp_url"`
|
||||
SMTPPort int `json:"smtp_port"`
|
||||
}
|
||||
|
||||
type ServerConfig struct {
|
||||
LDAPConfig LDAPConfig `json:"ldap_config"`
|
||||
StyleConfig StyleConfig `json:"style_config"`
|
||||
WebserverConfig WebserverConfig `json:"server_config"`
|
||||
EmailConfig EmailConfig `json:"email_config"`
|
||||
}
|
||||
|
||||
func loadServerConfig(path string) (*ServerConfig, error) {
|
||||
|
||||
@@ -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