Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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 = 'navanitachora@gmail.com';
    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.

share|improve this question

1 Answer 1

up vote 6 down vote accepted

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.

share|improve this answer
    
How would I call the function after plugins_loaded fires and still make it send the email when required? –  navanitachora Mar 4 '12 at 16:31
    
Just do everything after 'plugins_loaded'. Then you don’t have to worry about the availability of pluggable functions. –  toscho Mar 4 '12 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. –  navanitachora Mar 5 '12 at 13:10
    
Take a look at my plugin Report Broken Video – I check several conditions in it before I send an email. –  toscho Mar 5 '12 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. –  navanitachora Mar 7 '12 at 2:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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