wp_mail is undefined

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:

Read More

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.

Related posts

Leave a Reply

1 comment

  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.