While inside mail function

Asked

Viewed 101 times

1

I have the following question: I want to get the results of a While (Basically 10 records), to be inserted inside the mail function. I don’t have a clue how I’m gonna do it, anyone can help me.

I want each result to be displayed exactly where it is "Here comes the result 1 from While" and etc...

Below the most exemplified code for stack friends can help me:

<?php
mysql_select_db($database_microf, $microf);
$query_sa1 = "SELECT * FROM sa1 WHERE  mesvenc='$mes' AND situacao='Pendente'  ";
$sa1 = mysql_query($query_sa1, $microf) or die(mysql_error());
$row_sa1 = mysql_fetch_assoc($sa1);
$totalRows_sa1 = mysql_num_rows($sa1);


mysql_select_db($database_microf, $microf);
$query_sa12 = "SELECT SUM(valor) FROM sa1 WHERE  mesvenc='$mes' AND situacao='Pendente'  ";
$sa12 = mysql_query($query_sa12, $microf) or die(mysql_error());
$row_sa12 = mysql_fetch_assoc($sa12);
$totalRows_sa12 = mysql_num_rows($sa12);

$total=$totalRows_sa1;
$valor=number_format($row_sa12['SUM(valor)'],2,',','.');


if($totalRows_sa1>0){

//hader para o e-mail ir com codificações corretas e tudo mais.
$headers .= "Content-Type:text/html; charset=UTF-8\n";
$headers .= "MIME-Version: 1.0\n";
//
$destino = '[email protected]';
$nome="Sistema Financeiro";
$email="[email protected]";

//Envia mensagem para o administrador do site
$resposta = mail("$destino","Relatorio de Contas Pendentes",
"<p><b>Atenção!</b></p>

-------------------------------------------------</br>

Existem <b>$total</b> contas  do mês de <b>$mes</b> que ainda não foram pagas.</br>
Elas Totalizam <b>R$ $valor</b> </br>

As contas a serem pagas são:
Aqui vem o resultado 1 do While</br>
Aqui vem o resultado 2 do while</br>
Aqui vem o resultado 3 do while</br>




Verifique por favor...</br>
<a href='http://meudominio.com'><img src='http://meudominio.com/cron/images/BtnAcessarSistema.png' /></a></br>

--------------------------------------------------</br>
<p>Essa é uma mensagem enviada através do seu sistema. Não Responda.</p>
","$headers"."From:$nome<$email>");




?>

1 answer

1


There are several ways to do this, one of them would be to assign some variable to the type text and rewrite in that variable the text inside the loop with whatever you want, I would do something like :

<?php
$relatorio = '';
$query = pdo->query($meuSql);
foreach(row as query)
{
    $relatorio .= "coluna1 : ".row['colunatabela']."/n".
                  "coluna2 : ".row['colunatabela2'];
    // ...
}

$headers .= "Content-Type:text/html; charset=UTF-8\n";
$headers .= "MIME-Version: 1.0\n";
$mensagem = "Relatorio mensal das suas contas pendentes. \n\n\n".
            "--------------------------------------------------\n".
            $relatorio.
            "--------------------------------------------------\n".
            "seu rodapé do email.bla bla bla\n";
$destino = '[email protected]';
$nome="Sistema Financeiro";
$email="[email protected]";
$subject = $nome." - Relaório mensal de contas...bla.bla.bla\n";
mail($destino, $subject, $mensagem, $headers);
?>

Lenbrando that to add text in variables in php, you have to use "text". $variavel_text." plus text". $outravar." plus another text"." etc."; and if you want a loop inside the other Oce makes another "foreach(...)" or while (...) inside the first loop, in my case the line: foreach(Row as query), therefore, it is up to you.

This is just the beginning, in addition, it puts the neurons to burn, the "savings" to chain and Lighten the finger in the code, laugh

health and peace!

Browser other questions tagged

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