wp_mail() not sending email in multisite subdomain

I have a multisite enabled WordPress configuration. My main domain is like http://www.example.com, with several subdomains like http://test1.example.com, http://test2.example.com, and so on.

I have written a function in my multisite theme's function file to send some notifications to the multisite users.

 $headers[] = 'Content-Type: text/html; charset=UTF-8';
 wp_mail('[email protected]', 'Test','Test', $headers);

It send the mails as desired when on the main domain, but it does not send any mails when used in subdomain function files.

For example, [email protected] is a registered user for http://test1.example.com and he submitting some information in his subdomain page http://test1.example.com/information-form. I want to send that information to that user's email.

EDIT: Scenarios:

  1. I have a form which have some input fields.
  2. Users fills it and upon submitting, using ajax the values transferred to functions file.
  3. There is a add_action('wp_ajax_xxx', 'myFunction'); hook in function page (User is logged in)
  4. In myFunction the above mentioned code resides.

Topic wp-mail email multisite Wordpress

Category Web


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';
});

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.