sending email with phpmailer

Asked

Viewed 31 times

-3

Hello.

I have a query (SQLSRV) that brings me a list of clients with expired financial securities. I have to send an e-mail (PHPMAILER) to each one with their outstanding expired securities. As I am doing, I can send an email to each line, so following the table below, I am expected to send ONE email to client1 with the two values of the same, and an email to cliente2 with the two values of the same.

How I would handle this case ?

Client Valor
cliente1 valor1
cliente1 value2
cliente2 Valor3
cliente2 value4

Output of query values:

$vencimentos = $query->fetchAll();
$content =
'<table>
  <thead>
    <tr>
      <th>Nro. Nota</th>
      <th>Nome</th>
      <th>Emissão</th>
      <th>Parcela</th>
      <th>Valor</th>
    </tr>
  </thead>
  <tbody>';
    foreach($vencimentos as $v){
      $name = $v["A1_NOME"];
      $email = $v["A1_EMAIL"];
      $content .= 
      '<tr>
        <td>'.$v["E1_NUM"].'</td>
        <td>'.$v["A1_NOME"].'</td>
        <td>'.$v["E1_EMISSAO"].'</td>
        <td>'.$v["E1_PARCELA"].'</td>
        <td>'.number_format($v["E1_VALOR"], 2, ",", "") .'</td>
      </tr>';
    }
$content .= '</tbody></table>';
echo $content;
die;

1 answer

-1


First of all you need to group the titles per client, something like:

$vencimentosPorCliente = [];

foreach ($vencimentos as $vencimento) {
    $vencimentosPorCliente[$vencimento["A1_EMAIL"]][] = $vencimento;
}

Having the titles grouped you can build a <table> for each customer and send it to the respective email, something like:

foreach ($vencimentosPorCliente as $cliente => $vencimentos) {
    $content = '<table>
        <thead>
            <tr>
            <th>Nro. Nota</th>
            <th>Nome</th>
            <th>Emissão</th>
            <th>Parcela</th>
            <th>Valor</th>
            </tr>
        </thead>
        <tbody>';

    foreach ($vencimentos as $vencimento) {
        $content .= '<tr>
            <td>' . $vencimento["E1_NUM"] . '</td>
            <td>' . $vencimento["A1_NOME"] . '</td>
            <td>' . $vencimento["E1_EMISSAO"] .'</td>
            <td>' . $vencimento["E1_PARCELA"] . '</td>
            <td>' . number_format($vencimento["E1_VALOR"], 2, ",", "") . '</td>
        </tr>';
    }

    $content .= '</tbody></table>';

    // enviarEmail($cliente, $content);
}

Behold a functional example

  • Hello Daniel, I did exactly as your tip in the example and it worked super well, thanks for the help.

Browser other questions tagged

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