6
votes

Hi im using this function by Wordpress in a Cron webpage and is throwing this error on my email

Fatal error: Call to undefined function wp_mail() in /home/meusite/public_html/wp-content/themes/escotec/page-cron.php on line 33

Here the code

foreach($inscricoes as $key => $item){



    $emailSent = false;



    $emailTo = "$item->getEmail()";

//echo "..1";

    $subject = '[Escotec]: Dados para pagamento de inscrição ';
    $body = "Parabéns $inscricao->nome, sua inscrição no curso ".$item->getTurmas()[0]->getCurso()->getNome()." foi efetuada. <p>Para concluir o pagamento da inscrição clique no link abaixo ou cole-o diretamente na barra de endereços de seu Navegador: </p><br>";
    $body .= "<a href=\"http://escotecnordeste.com.br/pagamento/?email=".$item->getEmail()."&pedido=".$item->getPagamentoId()."\" target=\"_blank\">http://escotecnordeste.com.br/pagamento/?email=".$item->getEmail()."&pedido=".$item->getPagamentoId()."</a>";
    $headers = 'From: Escotec Nordeste <[email protected]>' . "\r\n" . 'Reply-To: ' . '[email protected]';





    wp_mail($emailTo, $subject, $body, $headers);


    $emailSent = true;
// http://escotecnordeste.com.br/pagamento/[email protected]&pedido=11

// Codificar envio do e-mail
    if ($emailSent) {
    // Atualizar registro do pedido para email_enviado = 'S'

        InscricaoDAO::RegistraEnvioEmail($item->getPagamentoId());
    }
}

Ty for help

6
Its weird I did not nothing and the error dissapear, but sometimes back to giving the email with error - lucas_cezar

6 Answers

15
votes

please add below code in you file. where you have called wp_mail() function.

Add this code top of your file.

require_once("../../../wp-load.php");

or change your function wp_mail() to mail()

3
votes

You are calling function wp_mail() which can be included by wp-load.php .

require_once("wp-load.php");
1
votes

Solution provided by Tonny works for me. My code:

function your_function_name() {
        $to =' [email protected]';
        $subject = 'The subject';
        $body = 'The email body content';
        $headers = array('Content-Type: text/html; charset=UTF-8');
        wp_mail( $to, $subject, $body, $headers );
}
add_action( 'wp_loaded', 'your_function_name' );
0
votes

The function wp_mail() has not been defined yet. Is this all the code on this page? wp_mail() is in this file. wp-includes/pluggable.php. You have to include it before the function is called.

0
votes

You need to require wp-load.php, in that way the function can be used with no problems, that file loads every function of wordpress

If your path is /home/meusite/public_html/wp-content/themes/escotec/page-cron.php then use this path to require wp-load:

require_once( dirname(__FILE__) . '/wp-load.php' );"





foreach($inscricoes as $key => $item){



$emailSent = false;



$emailTo = "$item->getEmail()";

//echo "..1";

$subject = '[Escotec]: Dados para pagamento de inscrição ';
$body = "Parabéns $inscricao->nome, sua inscrição no curso ".$item->getTurmas()[0]->getCurso()->getNome()." foi efetuada. <p>Para concluir o pagamento da inscrição clique no link abaixo ou cole-o diretamente na barra de endereços de seu Navegador: </p><br>";
$body .= "<a href=\"http://escotecnordeste.com.br/pagamento/?email=".$item->getEmail()."&pedido=".$item->getPagamentoId()."\" target=\"_blank\">http://escotecnordeste.com.br/pagamento/?email=".$item->getEmail()."&pedido=".$item->getPagamentoId()."</a>";
$headers = 'From: Escotec Nordeste <[email protected]>' . "\r\n" . 'Reply-To: ' . '[email protected]';





wp_mail($emailTo, $subject, $body, $headers);


$emailSent = true;
// http://escotecnordeste.com.br/pagamento/[email protected]&pedido=11

// Codificar envio do e-mail
if ($emailSent) {
// Atualizar registro do pedido para email_enviado = 'S'

    InscricaoDAO::RegistraEnvioEmail($item->getPagamentoId());
}
}
0
votes

This worked for me:

add_action( 'wp_loaded', 'cron_time' );

It works everywhere and almost always and especially for ajax.

Do not include wordpress core files. It is bad practice. Like this:

require_once( dirname(__FILE__) . '/wp-load.php' );