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.
You may call the function too early. You have to wait until the action
'plugins_loaded'
fires.wp_mail()
is defined inwp-includes/pluggable.php
.pluggable.php
is loaded inwp-settings.php
after the plugins are loaded but before'plugins_loaded'
is called.See this answer for an example.