- // Add any CC and BCC recipients
- if ( !empty( $cc ) ) {
- foreach ( (array) $cc as $recipient ) {
- try {
- // Break $recipient into name and address parts if in the format "Foo <bar@baz.com>"
- $recipient_name = '';
- if ( preg_match( '/(.*)<(.+)>/', $recipient, $matches ) ) {
- if ( count( $matches ) == 3 ) {
- $recipient_name = $matches[1];
- $recipient = $matches[2];
- }
- }
- $phpmailer->AddCc( $recipient, $recipient_name );
- } catch ( phpmailerException $e ) {
- continue;
- }
+ // Use appropriate methods for handling addresses, rather than treating them as generic headers
+ $address_headers = compact( 'to', 'cc', 'bcc', 'reply_to' );
+
+ foreach ( $address_headers as $address_header => $addresses ) {
+ if ( empty( $addresses ) ) {
+ continue;