How can PHP developers ensure compliance with RFC 2822 when constructing email address arrays for setReplyTo in SwiftMailer?

When constructing email address arrays for setReplyTo in SwiftMailer, PHP developers can ensure compliance with RFC 2822 by validating each email address using filter_var with the FILTER_VALIDATE_EMAIL flag. This will ensure that the email addresses are properly formatted before being added to the array.

// Example code snippet to ensure compliance with RFC 2822 when constructing email address arrays for setReplyTo in SwiftMailer
$replyTo = ['john.doe@example.com', 'jane.doe@example.com'];

$validEmails = array_filter($replyTo, function($email) {
    return filter_var($email, FILTER_VALIDATE_EMAIL);
});

// Use $validEmails array for setReplyTo in SwiftMailer