5 Commits

Author SHA1 Message Date
gawells 6a985c1a84 send simple test email to my email on server startup 2026-04-13 09:32:22 -04:00
gawells 590ea73a92 send all emails as HTML 2026-04-13 09:22:57 -04:00
gawells 8de145adbc add email config to config file 2026-04-13 09:19:35 -04:00
gawells 8f0291bb8a make small changes to the email package 2026-04-13 09:13:49 -04:00
gawells cde41b82b2 base URL param for config 2026-04-09 16:45:02 -04:00
6 changed files with 112 additions and 13 deletions
+9 -1
View File
@@ -12,6 +12,14 @@
"logo_path": "./data/astraltech_logo_large.png" "logo_path": "./data/astraltech_logo_large.png"
}, },
"server_config": { "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
View File
@@ -16,35 +16,38 @@ type EmailAccount struct {
} }
type EmailAccountData struct { type EmailAccountData struct {
username string Username string
password string Password string
email string Email string
} }
func createEmailAccount(accountData EmailAccountData, smtpHost string, smtpPort int) EmailAccount { 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) logging.Debugf("Creating Email Account: \n\tUsername: %s\n\tEmail: %s\n\tSMTP Host: %s:%d", accountData.Username, accountData.Email, smtpHost, smtpPort)
account := EmailAccount{ account := EmailAccount{
email: accountData.email, email: accountData.Email,
smtpHost: smtpHost, smtpHost: smtpHost,
smtpPort: strconv.Itoa(smtpPort), smtpPort: strconv.Itoa(smtpPort),
} }
account.auth = smtp.PlainAuth("", accountData.username, accountData.password, smtpHost) account.auth = smtp.PlainAuth("", accountData.Username, accountData.Password, smtpHost)
return account return account
} }
func sendEmail(account EmailAccount, toEmail []string, subject string, message string) { func (account *EmailAccount) SendEmail(toEmails []string, subject string, message string) {
logging.Debugf("Sending an email from %s to %s", account.email, strings.Join(toEmail, "")) 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( messageData := []byte(
"From: " + account.email + "\r\n" + "From: " + account.email + "\r\n" +
"To: " + ToEmailList + "\r\n" + "To: " + ToEmailList + "\r\n" +
"Subject: " + subject + "\r\n" + "Subject: " + subject + "\r\n" +
mime +
"\r\n" + "\r\n" +
message, 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 { if err != nil {
logging.Error("Failed to send email") logging.Error("Failed to send email")
logging.Error(err.Error()) logging.Error(err.Error())
+28
View File
@@ -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
}
+10
View File
@@ -23,12 +23,22 @@ type StyleConfig struct {
type WebserverConfig 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 { type ServerConfig struct {
LDAPConfig LDAPConfig `json:"ldap_config"` LDAPConfig LDAPConfig `json:"ldap_config"`
StyleConfig StyleConfig `json:"style_config"` StyleConfig StyleConfig `json:"style_config"`
WebserverConfig WebserverConfig `json:"server_config"` WebserverConfig WebserverConfig `json:"server_config"`
EmailConfig EmailConfig `json:"email_config"`
} }
func loadServerConfig(path string) (*ServerConfig, error) { func loadServerConfig(path string) (*ServerConfig, error) {
+25
View File
@@ -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",