What is the most efficient way to send out a group email like this?
By letting your MTA do the hard work. I'd do one of the following, in order of preference:
- Use dedicated mass-mailing software. (Mailing list software, not spam).
- Cheat (qmail solution): write the addresses to send to /var/qmail/aliases/.qmail-maillinglist (one address per line), then send an email to maillinglist@mydomain.
- Fork, opening a pipe between the two processes (using open "|-" or open "-|". Process that reads from the pipe does an exec "sendmail", @addresses (assuming the addresses to send to are in @addresses). Process that writes to the pipe composes the email, and prints it to the pipe handle. If you have more addresses than you can pass into exec, just splice the array in smaller parts and open various loops.
What code should I use to capture an email address that it couldn't send to and force it to continue the script?
No code. Undeliverable addresses is something for the MTA to worry about (retry, bounce, etc).