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