Parse display-name recipients for compose send

This commit is contained in:
DonVoo 2026-07-13 12:32:14 +02:00
parent 29fd3f5cae
commit c3d3f8e677

View file

@ -73,7 +73,11 @@ func mailSendHandler(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
to := splitRecipients(r.FormValue("to"))
to, err := splitRecipients(r.FormValue("to"))
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
subject := strings.TrimSpace(r.FormValue("subject"))
body := r.FormValue("body")
if len(to) == 0 {
@ -91,21 +95,27 @@ func mailSendHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "gesendet")
}
func splitRecipients(value string) []string {
fields := strings.FieldsFunc(value, func(r rune) bool {
return r == ',' || r == ';' || r == '\n' || r == '\r' || r == '\t' || r == ' '
})
recipients := make([]string, 0, len(fields))
seen := make(map[string]bool, len(fields))
for _, field := range fields {
addr := strings.TrimSpace(field)
if addr == "" || seen[strings.ToLower(addr)] {
func splitRecipients(value string) ([]string, error) {
normalized := strings.NewReplacer(";", ",", "\r\n", ",", "\n", ",", "\r", ",").Replace(strings.TrimSpace(value))
if normalized == "" {
return nil, nil
}
parsed, err := mail.ParseAddressList(normalized)
if err != nil {
return nil, fmt.Errorf("Ungueltiger Empfaenger: %s", err)
}
recipients := make([]string, 0, len(parsed))
seen := make(map[string]bool, len(parsed))
for _, address := range parsed {
addr := strings.TrimSpace(address.Address)
key := strings.ToLower(addr)
if addr == "" || seen[key] {
continue
}
recipients = append(recipients, addr)
seen[strings.ToLower(addr)] = true
seen[key] = true
}
return recipients
return recipients, nil
}
func homeHandler(w http.ResponseWriter, r *http.Request) {