Calling PHP function by BAT file

Asked

Viewed 1,842 times

3

I need to create a BAT file that should run every 25th day. It should perform a function called emailAniversariantes(), which has been set for programming a website within two files (controller and model).

Since I took over this site from another developer and the same used an application to run this function every 25th, I would need the BAT file to do the same. I believe that using PHP commands for the Windows prompt may be the solution. But I still haven’t found on the internet the correct commands to directly execute this function of the controller.

My function inside the Model directory file:

public function emailAniversariantes(){
$data = getdate();
$mes = $data[mon];
$ano = $data[year];
if($mes==12){
    $mes = 1;
    $ano = $ano+1;
}else{
    $mes = $mes+1;
    if($mes<10){
        $mes = '0'.$mes;
    }
}

$sqlUnidades = "select id, nome from unidade where ind_unidade_ativa=1 order by nome";
$resUnidades = System::element('db')->query($sqlUnidades);

$corpo = "";
$corpo .= "<div style='float:left;width:100%; height:3px; background-color:gray; margin-bottom:20px;'></div>";

$corpo .="<div style='font-family:Lucida Sans; font-size:13px;' color='black'>
            <br>Bom dia.<br>Segue abaixo a relação dos aniversariantes do próximo mês.<br>
            Se possível, sugerimos que entrem em contato com eles para desejar feliz aniversário!<br><br><br>
            </div>";

foreach ($resUnidades as $rUnidades){
    $sql = "Select u.id as id_usuario, i.id as id_investidor, uu.id_unidade, un.nome nome_unidade, u.email, 
            DATE_FORMAT(i.data_nasc, '%d/%m/%Y') as data_nasc, i.razao_social as nome, i.telefone, i.nome_responsavel,
            (Select count(*) from usuario u join usuario_unidade uu on (uu.id_usuario = u.id) join investidor i on (i.id_usuario = u.id)
            where uu.id_unidade = ".$rUnidades['id']." and month(i.data_nasc)=".$mes.") AS count
            from usuario u join usuario_unidade uu on (uu.id_usuario = u.id) 
            join unidade un on (un.id = uu.id_unidade)
            join investidor i on (i.id_usuario = u.id)
            where uu.id_unidade = ".$rUnidades['id']." and month(i.data_nasc)=".$mes."
            ORDER BY i.razao_social";
    $res = System::element('db')->query($sql);

    if($res[0]['count']>=1){
        $corpo .="<div style='width:100%; height:100%; float: left; margin: 25px 0;'>";

        $corpo .="<b>Unidade: ".$rUnidades['nome']."</b>";

        $corpo .="<table style='width: 100%; border-collapse: collapse; display: table; background-color: #FFFFFF; border: 3px solid #E2E1E0; padding: 5px;'>
                    <thead>
                        <tr stile='border-collapse: collapse; border: 3px solid #E2E1E0; padding: 5px;'>
                            <th style='border-collapse: collapse; background-color: #F7F7F7; border: 3px solid #E2E1E0; padding: 5px; text-align: center;'>Nome</th>
                            <th style='border-collapse: collapse; background-color: #F7F7F7; border: 3px solid #E2E1E0; padding: 5px; text-align: center;'>Telefone</th>
                            <th style='border-collapse: collapse; background-color: #F7F7F7; border: 3px solid #E2E1E0; padding: 5px; text-align: center;'>E-mail</th>
                            <th style='border-collapse: collapse; background-color: #F7F7F7; border: 3px solid #E2E1E0; padding: 5px; text-align: center;'>Responsável</th>
                            <th style='border-collapse: collapse; background-color: #F7F7F7; border: 3px solid #E2E1E0; padding: 5px; text-align: center;'>Data de Nascimento</th>
                        </tr>
                    </thead>";
        $corpo .="<tbody>";

        foreach ($res as $r){

            $corpo .= "<tr stile='border-collapse: collapse; border: 3px solid #E2E1E0; padding: 5px;'>";
                $corpo .= "<td style='border-collapse: collapse; background-color: #FFFFFF; border: 3px solid #E2E1E0; padding: 5px; text-align: center;'>".$r['nome']."</td>";
                $corpo .= "<td style='border-collapse: collapse; background-color: #FFFFFF; border: 3px solid #E2E1E0; padding: 5px; text-align: center;'>".$r['telefone']."</td>";
                $corpo .= "<td style='border-collapse: collapse; background-color: #FFFFFF; border: 3px solid #E2E1E0; padding: 5px; text-align: center;'>".$r['email']."</td>";
                $corpo .= "<td style='border-collapse: collapse; background-color: #FFFFFF; border: 3px solid #E2E1E0; padding: 5px; text-align: center;'>".$r['nome_responsavel']."</td>";
                $corpo .= "<td style='border-collapse: collapse; background-color: #FFFFFF; border: 3px solid #E2E1E0; padding: 5px; text-align: center;'>".$r['data_nasc']."</td>";
            $corpo .= "</tr>";
        }
        $corpo .="</tbody>";
        $corpo .="</table></div>";
    }
}

$corpo .= "<div style='width:100%; height:56px; float:left;'>";
$corpo .= "<table><font face='Lucida Sans'>";
$corpo .= "<tr>";
$corpo .= "<td style='float:left;'><div style='font-size:10px; float:left; width:200px; margin-top:5px; position:relative; margin-right:15px; float:left;'>Tel: +55 11 3577-7173<br>";
$corpo .= "[email protected]<br>";
$corpo .= "http://www.meusite.com.br/</td>";

$corpo .= "<td><img style='float:left; position:relative'></td>";

$corpo .= "</table></font></div></div>";
$corpo .= "</div>";

$arrayEmails = array();
$arrayEmails[0]="[email protected]";
$arrayEmails[1]="[email protected]";
$arrayEmails[2]="[email protected]";
Util::enviaEmail("Remetente", $arrayEmails, "Aniversariantes ".$mes."/".$ano, $corpo,implode(";",$arrayEmails));
return "1";
}

How the function is called by the Controller file:

public function emailAniversariantes() {
$retorno = CadAdmin::emailAniversariantes();
echo $retorno;
}

I appreciate anyone who can help me.

  • 1

    See if that answer helping

  • I copied the script of the function in PHP and had it executed by windows prompt. But it didn’t work. Is that it has in the code functions like System::element('db')->query($sql); and Util::enviaEmail("Sender", $arrayEmails, "Aniversariants ".$mes."/".$year, $body,implode(";",$arrayEmails)); that use functions of other files of the site?

1 answer

4

You will have to create a PHP file, it may even be the same as you defined the function, but it will have more or less the following structure

<?php
suachamadadefuncao();

function suafuncao() {}

?>

Then you can create a . bat with the structure like this:

@echo off

caminhoparaseuphp.exe caminhoparaoarquivophp.php

Let’s assume that your PHP.exe stays in the C directory: just like this php file you created, the command would be

@echo off
C:/php.exe C:/seuphp.php

This way you will be running the PHP file inside the command prompt, which in turn would call its function. Now just use the windows Scheduler itself (taskschd.Msc) to schedule for the 25th.

  • I copied the script of the function in PHP and had it executed by windows prompt. But it didn’t work. Is that it has in the code functions like System::element('db')->query($sql); and Util::enviaEmail("Sender", $arrayEmails, "Aniversariants ".$mes."/".$year, $body,implode(";",$arrayEmails)); that use functions of other files of the site?

  • This should not interfere as long as the paths are right. You would be running php.exe with a file parameter that it would run

Browser other questions tagged

You are not signed in. Login or sign up in order to post.