display array result within phpmailer message

Asked

Viewed 25 times

-2

Good evening folks, I need to display the results of an array in a message body phpmailer, so send the same by email. My sending code is working and I have done several tests, the email arrives perfectly, minus the result of the array, and when it arrives, only one item comes, the first, the others do not, usually 5 to 10 items.

I tested the array with sending to a "normal" PHP page and the array works, the items are displayed on the screen, I believe it is syntax, because the code is the same with the exception of single or double quotes. In short, how can I put the for inside the body $mensagem to reach the table by e-mail?

Follows the code:

$mensagem = "
<table border='1' cellspacing='0' width='70%' style='font-family: Roboto-Regular, arial, serif; font-size: 15px; color: #6f4500'>
<tr>
<th>Itens</th> <th>ID do Produto</th> <th>Código</th> <th>Tipo</th> <th>Produto</th><th>Preço</th><th>Quantidade</th><th>Valor total</th>
</tr>
        ".$idProd = $_POST['arrayidProd']."
        ".$cod = $_POST['arraycod']."
        ".$tipo = $_POST['arraytipo']."
        ".$produto = $_POST['arrayproduto']."
        ".$preco = $_POST['arraypreco']."
        ".$qtd = $_POST['arrayqtd']."
        ".$total = $_POST['arraytotal']."
        
    for($i = 0; $i < count($idProd); $i++) {
    <td></td><td>".$idProd[$i]."</td><td>".$cod[$i]."</td><td>".$tipo[$i]."</td><td>".$produto[$i]."</td><td>".$preco[$i]."</td><td>".$qtd[$i]."</td><td>".$total[$i]."</td></tr>
    }
</table>";

1 answer

0

Test here

  1. First step - recover POST values

    $idProd = $_POST['arrayidProd'];
    $cod = $_POST['arraycod'];
    $tipo = $_POST['arraytipo'];
    $produto = $_POST['arrayproduto'];
    $preco = $_POST['arraypreco'];
    $qtd = $_POST['arrayqtd'];
    $total = $_POST['arraytotal'];
    
  2. Second step - loop the values into a variable

    for($i = 0; $i < count($idProd); $i++) {
    $linhas .= ("<tr><td></td><td>".$idProd[$i]."</td><td>".$cod[$i]."</td><td>".$tipo[$i]."</td><td>".$produto[$i]."</td><td>".$preco[$i]."</td><td>".$qtd[$i]."</td><td>".$total[$i]."</td></tr>");
    }
    
  3. Create the message

    $mensagem = "
    <table border='1' cellspacing='0' width='70%' style='font-family: Roboto-Regular, arial, serif; font-size: 15px; color: #6f4500'>
    <tr>
    <th>Itens</th> <th>ID do Produto</th> <th>Código</th> <th>Tipo</th> <th>Produto</th><th>Preço</th><th>Quantidade</th><th>Valor total</th>
    </tr>".$linhas."
    </table>";
    

Complete code

<?php
        
    $idProd = $_POST['arrayidProd'];
    $cod = $_POST['arraycod'];
    $tipo = $_POST['arraytipo'];
    $produto = $_POST['arrayproduto'];
    $preco = $_POST['arraypreco'];
    $qtd = $_POST['arrayqtd'];
    $total = $_POST['arraytotal'];    
 
    for($i = 0; $i < count($idProd); $i++) {
        $linhas .= ("<tr><td></td><td>".$idProd[$i]."</td><td>".$cod[$i]."</td><td>".$tipo[$i]."</td><td>".$produto[$i]."</td><td>".$preco[$i]."</td><td>".$qtd[$i]."</td><td>".$total[$i]."</td></tr>");
    }
 
    $mensagem = "
    <table border='1' cellspacing='0' width='70%' style='font-family: Roboto-Regular, arial, serif; font-size: 15px; color: #6f4500'>
    <tr>
    <th>Itens</th> <th>ID do Produto</th> <th>Código</th> <th>Tipo</th> <th>Produto</th><th>Preço</th><th>Quantidade</th><th>Valor total</th>
    </tr>".$linhas."
    </table>";
 
?>

Browser other questions tagged

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