wp_mail() not sending email in multisite subdomain

Finally I figured out the cause and solution to my problem above.

When I send email in sub domain such as http://test1.example.com without a custom “From” address in header, the wp_mail() function itself sets the default “From” address as [email protected]. This is invalid so the email was not sent.

To solve this, I added a custom “From” email to the header like this:

$headers[] = 'From: Sender Name <[email protected]>';

This sets a valid email address in the header.

You can also set the same using filters like this:

add_filter('wp_mail_from', function( $email ) {
    return '[email protected]';
});

add_filter('wp_mail_from_name', function( $name ) {
    return 'Sender Name';
});