1

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.
2
  • 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, 2016 at 7:34
  • email not sent at all @cybmeta. The same headers are working at the main domain
    – mpsbhat
    Apr 27, 2016 at 9:07

1 Answer 1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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