Good point from : the characters between addresses are line breaks, not spaces. Different sources may use LF ("\n"), CRLF ("\r\n"), CR ("\r") or even HTML "<br>" tags, so a replacement should handle all of those at once.
A simple normalization (replace all newline types and HTML breaks, collapse repeated commas/spaces, trim ends) works well:
When the input contains mailto anchor tags it’s safer to extract addresses from the hrefs (or from the text) rather than relying on tag-stripping. Example using regex to pull mailto: values or fallback to a standard email regex:
If the list comes from a plain text file, using file() with FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES is simpler and less error-prone than manual fgets loops (as suggested by ). After loading lines, trim, validate with filter_var(..., FILTER_VALIDATE_EMAIL), remove duplicates, then join with commas.
Troubleshooting notes: watch for invisible characters (tabs, zero-width spaces), remove HTML before sending to mail headers, avoid trailing commas, and validate addresses to prevent malformed recipients.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.