I am writing a plugin that opens up a form to invite people to a website by email and want to use wp_mail().

Whenever I use the wp_mail() function in any file in my plugin folder I always end up with:

PHP Fatal error: Call to undefined function wp_mail()

The code segment in question is:

function send_email() {
    $subject = 'test';
    $message = 'this is a test from send invitation';
    $to = '[email protected]';
    wp_mail($to, $subject, $message);

I am at my wits end as to why this should be happening when so many plugins use wp_mail().

Thanks in advance.


1 Answer 1


You may call the function too early. You have to wait until the action 'plugins_loaded' fires.

wp_mail() is defined in wp-includes/pluggable.php.
pluggable.php is loaded in wp-settings.php after the plugins are loaded but before 'plugins_loaded' is called.

See this answer for an example.

  • How would I call the function after plugins_loaded fires and still make it send the email when required?
    – nandac
    Mar 4, 2012 at 16:31
  • Just do everything after 'plugins_loaded'. Then you don’t have to worry about the availability of pluggable functions.
    – fuxia
    Mar 4, 2012 at 20:37
  • Thank you @toscho for your reply I added the add_action method with "plugins_loaded" and wp_mail sends out the email but it sends it out everytime the page loads is there a way to have wp_mail defined but not send out the email unless I press submit on my form. There is something simple I am missing here but I do not exactly know what.
    – nandac
    Mar 5, 2012 at 13:10
  • Take a look at my plugin Report Broken Video – I check several conditions in it before I send an email.
    – fuxia
    Mar 5, 2012 at 16:29
  • Thanks @toscho for pointing me to your Report Broken Video code it has been most helpful and I have modified my own code to use a class namespace. However, I am having a few problems using the class because I am not sure how to actually use or access those methods in my template. Should I instantiate it in my template and then echo out the form code using the appropriate get form code or should this happen automatically. I have been writing OOP code for quite a while but I am not sure how to proceed in the case. Do let me know if I should ask this as a new question.
    – nandac
    Mar 7, 2012 at 2:39

Your Answer

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

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