0

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('user@example.com', '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, user@example.com 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.
  • And what is the problem you have? "Not working" is very ambiguos. I see in your code that you are trying to set custom $headers, are the email sent with incorrect $headers? Are the email not sent at all? Please, clarify. Also, can you post the context where you are executing your code (i.e. some action hook)? – cybmeta Apr 27 '16 at 7:34
  • email not sent at all @cybmeta. The same headers are working at the main domain – mpsbhat Apr 27 '16 at 9:07
0

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 support@test1.example.com. 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 <noreply@example.com>';

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 'noreply@example.com';
});

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.