email with a disfigured table

Asked

Viewed 496 times

0

The code below sends you order confirmation email correctly, however it arrives unnumbered to only 'a single recipient'. In case, the product in the description field appears printed before anything on the screen and some words appear together. Resultados do mesmo código

    /* FORMATAÇÃO HTML DA MENSAGEM */
    $html ="<!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' 'http://www.w3.org/TR/html4/loose.dtd'>
    <html>
        <head>
            <meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
            <meta name='viewport' content='width=device-width, initial-scale=1.0'/>
            <title>Email html</title>
        </head>
        <body style='margin: 0; padding: 0;'>
            <!-- Tabela grande extrena -->
            <table border='0' width='100%' cellpadding='0' cellspacing='0'>
                <tr>
                    <td>
                    <!-- Tabela interna 1 com 600px -->
                        <table border='1' align='center' width='600px' style='border-collapse: collapse;' cellpadding='0' cellspacing='0'>
                            <tr><td align='center' style='padding: 30px 0 30px 0;'><img src='http://XXXXX.com/gui/Imagens/Logo_eMail.png' alt='XXXXX' width='50' height='50' style='display: block;' /></td></tr>
                             <tr>
                                <td style='padding:30px 30px 30px 30px'>
                                    <!-- Tabela mais interna-->
                                    <table border='1' cellpadding='0' cellspacing='0' width='100%'>
                                            <tr><td colspan='2'><p>Olá, ".$nome."!<br>Seu pedido foi realizado com sucesso no portal da XXXXX, conforme detalhes abaixo:<br><br></p></td></tr>
                                            <tr><th colspan='2' bgcolor='#9CC6F7'>DETALHAMENTO DE PEDIDO</th></tr>
                                            <tr><td colspan='2'>PEDIDO n. : ".$numTroca."</td></tr>
                                            <tr><td colspan='2'>DESTINATÁRIO : ".$strDestin."</td></tr>
                                            <tr><td bgcolor='#9CC6F7' align='center' width='15%'><b>QTD</b></td><td bgcolor='#9CC6F7' align='center' width='85%'><b>DESCRIÇÃO</b></td></tr>";
                                            //MONTAGEM DINÂMICA DO MEIO DA TABELA
                                            foreach ($resTroca as $value){
                                                $brindeDesc = $value['bri_Desc'];
                                                $brindeQtd = $value['tro_Qtd'];
                                                $html .= "<tr><td width='15%'>".$brindeQtd."</td><td width='85%'>".$brindeDesc."</td></tr>";
                                            }
                    $html .="</table>
                             </td>
                            </tr>
                            <tr><td><p><br>Gratos pela preferência, logo mais você receberá novo email com instruções e endereço de retirada do brinde.<br>Atenciosamente, <br>Zharium</p></td></tr>
                        </table>
                    <!-- Fim da Tabela 1 -->
                    </td>
                </tr>
            </table>
        <!-- Fim da Tabela grande externa -->
        </body>
    </html>";


    /**FORMATANDO O ENVIO DEEMAIL*/
    $mail = new PHPMailer();
    $mail->SetLanguage('pt');   // Define o Idioma
    $mail->IsHTML(true);        // Enviar como HTML
    $mail->IsSMTP();            // Define que será enviado por SMTP
    $mail->CharSet   = 'utf-8'; // Define a Codificação

    $mail->SMTPDebug = 3;       
    $mail->Host      = 'br337.hostgator.com.br'; // Servidor SMTP
    $mail->SMTPAuth  = true;    // Servidor SMTP precisa de autenticação
    $mail->Username  = '[email protected]';  // E-mail para aut. no SMTP
    $mail->Password  = '*****';       // Senha do E-mail

    $mail->From      = '[email protected]'; // Define o Remetente
    $mail->FromName  = 'XXXXXX';         // Nome do Remetente
    $mail->AddAddress($emailTo,$nome);    // Email e Nome do destinatário
    $mail->AddCC('[email protected]','XXXXXXX'); // Envia Cópia
    $mail->WordWrap = 70;
    $mail->Subject  = $assunto;           // Assunto da mensagem
    $mail->Body = $html;                  // Corpo da mensagem HTML

    $enviado = $mail->Send();             // Envia o e-mail
    $mail->ClearAllRecipients();          // Limpa os destinatários

    if($enviado){     // Testa o envio
      return true;
    }else{
      echo "Não foi possível enviar o e-mail.";
      echo "<b>Informações do erro:</b> " . $mail->ErrorInfo; die();
    }
  • This code has several problems, one of them is not escape special characters, another of them is that you do not clean the variable $meio before the loop (there are more problems, but these two are enough to be a hell debug)

  • Hello @Bacco, thank you for your attention. I modified the code, as you can see, but it still gives problems in a single recipient. The variables are started in an interval that is not visible, so if you can point out the other mistakes.. I would be grateful.

  • vc needs . htmlentities( $brindeQtd ). to take care of special characters (if you are not doing this elsewhere in the code). One thing that can help is to use heredoc. http://answall.com/a/14258/70

3 answers

0

In internal table 1, in the first <td>, is declaring colspan=2 needlessly.

Some renderers can interpret literally and others just ignore.

When interpreted literally, rendering adds cells that do not exist, causing the unwanted visual effect.

Try removing the attribute colspan of cells where unnecessary.

A third cause may be the content being divided. Older systems impose limitations on the number of characters per line in a text file.

See an example of what happens when text content is split: https://ideone.com/37fzHa

This test was played using the PHP function chunk_split().

<!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' '
http://www.w3.org/TR/html4/loose.dtd'>
<html>
    <head>
       
 <meta http-equiv='Content-Type' content='text/html; charset=utf
-8'>
        <meta name='viewport' content='width=device-width, 
initial-scale=1.0'/>
        <title>Email html</title>
    </hea
d>
    <body style='margin: 0; padding: 0;'>
        <!-- Tabela
 grande extrena -->
        <table border='0' width='100%' cellp
adding='0' cellspacing='0'>
            <tr>
                <td
>
                <!-- Tabela interna 1 com 600px -->
          
          <table border='1' align='center' width='600px' style='
border-collapse: collapse;' cellpadding='0' cellspacing='0'>
   
                     <tr><td colspan='2' align='center' style='p
adding: 30px 0 30px 0;'><img src='http://zoiha.com.br/gui/Imagen
s/Logo_eMail.png' alt='Zoiha' width='50' height='50' style='disp
lay: block;' /></td></tr>
                         <tr>
        
                    <td style='padding:30px 30px 30px 30px'>
   
                             <!-- Tabela mais interna-->
       
                         <table border='1' cellpadding='0' cells
pacing='0' width='100%'>
                                       
 <tr><td colspan='2'><p>Olá, foo!<br>Seu pedido foi realizado c
om sucesso no portal da Zoiha, conforme detalhes abaixo:<br><br>
</p></td></tr>
                                        <tr><th c
olspan='2' bgcolor='#9CC6F7'>DETALHAMENTO DE PEDIDO</th></tr>
  
                                      <tr><td colspan='2'>PEDIDO
 n. : foo 1</td></tr>
                                        <t
r><td colspan='2'>DESTINATÁRIO : foo 2</td></tr>
              
                          <tr><td bgcolor='#9CC6F7' align='cente
r' width='15%'><b>QTD</b></td><td bgcolor='#9CC6F7' align='cente
r' width='85%'><b>DESCRIÇÃO</b></td></tr><tr><td width='15%'>1
</td><td width='85%'>test 1</td></tr><tr><td width='15%'>1</td><
td width='85%'>test 2</td></tr><tr><td width='15%'>1</td><td wid
th='85%'>test 2</td></tr></table></td>
                        <
/tr>
                        <tr><td><p><br>Gratos pela preferê
ncia, logo mais você receberá novo email com instruções e en
dereço de retirada do brinde.<br>Atenciosamente, <br>Zoiha</p><
/td></tr>
                    </table>
                <!-- Fim 
da Tabela 1 -->
                </td>
            </tr>
        
</table>
    <!-- Fim da Tabela grande externa -->
    </body>
<
/html>

To resolve the doubt regarding the divided content (Chunk split), if possible take the original email and read the original code received by the server, (in your case the UOL). You need to open and read the original code of the message. But I can’t tell if the Webmail of said provider has such option.

In addition to bo chunk_split, other formats can also corrupt rendering if it is not well formatted.

It is also common for emails to be encoded as quoted-rpintable

<!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' 'http://www.=
w3.org/TR/html4/loose.dtd'>=0A<html>=0A    <head>=0A        <meta http-equi=
v=3D'Content-Type' content=3D'text/html; charset=3Dutf-8'>=0A        <meta =
name=3D'viewport' content=3D'width=3Ddevice-width, initial-scale=3D1.0'/>=
=0A        <title>Email html</title>=0A    </head>=0A    <body style=3D'mar=
gin: 0; padding: 0;'>=0A        <!-- Tabela grande extrena -->=0A        <t=
able border=3D'0' width=3D'100%' cellpadding=3D'0' cellspacing=3D'0'>=0A   =
         <tr>=0A                <td>=0A                <!-- Tabela interna =
1 com 600px -->=0A                    <table border=3D'1' align=3D'center' =
width=3D'600px' style=3D'border-collapse: collapse;' cellpadding=3D'0' cell=
spacing=3D'0'>=0A                        <tr><td colspan=3D'2' align=3D'cen=
ter' style=3D'padding: 30px 0 30px 0;'><img src=3D'http://zoiha.com.br/gui/=
Imagens/Logo_eMail.png' alt=3D'Zoiha' width=3D'50' height=3D'50' style=3D'd=
isplay: block;' /></td></tr>=0A                         <tr>=0A            =
                <td style=3D'padding:30px 30px 30px 30px'>=0A              =
                  <!-- Tabela mais interna-->=0A                           =
     <table border=3D'1' cellpadding=3D'0' cellspacing=3D'0' width=3D'100%'=
>=0A                                        <tr><td colspan=3D'2'><p>Ol=
=C3=A1, foo!<br>Seu pedido foi realizado com sucesso no portal da Zoiha, co=
nforme detalhes abaixo:<br><br></p></td></tr>=0A                           =
             <tr><th colspan=3D'2' bgcolor=3D'#9CC6F7'>DETALHAMENTO DE PEDI=
DO</th></tr>=0A                                        <tr><td colspan=3D'2=
'>PEDIDO n. : foo 1</td></tr>=0A                                        <tr=
><td colspan=3D'2'>DESTINAT=C3=81RIO : foo 2</td></tr>=0A                  =
                      <tr><td bgcolor=3D'#9CC6F7' align=3D'center' width=3D=
'15%'><b>QTD</b></td><td bgcolor=3D'#9CC6F7' align=3D'center' width=3D'85%'=
><b>DESCRI=C3=87=C3=83O</b></td></tr><tr><td width=3D'15%'>1</td><td width=
=3D'85%'>test 1</td></tr><tr><td width=3D'15%'>1</td><td width=3D'85%'>test=
 2</td></tr><tr><td width=3D'15%'>1</td><td width=3D'85%'>test 2</td></tr><=
/table></td>=0A                        </tr>=0A                        <tr>=
<td><p><br>Gratos pela prefer=C3=AAncia, logo mais voc=C3=AA receber=C3=
=A1 novo email com instru=C3=A7=C3=B5es e endere=C3=A7o de retirada do brin=
de.<br>Atenciosamente, <br>Zoiha</p></td></tr>=0A                    </tabl=
e>=0A                <!-- Fim da Tabela 1 -->=0A                </td>=0A   =
         </tr>=0A        </table>=0A    <!-- Fim da Tabela grande externa -=
->=0A    </body>=0A</html>

Here in the snippet you will see a tin result in the letters like =0A=0A =0A =0A = =0A =0A =0A =0A =0A =0A = =0A =0A =0A =0A =0A =0A = but in an email client is displayed correctly because it is done automatically. On important here that I want to show is how the formatting of your table also breaks with this format. See the visual result in the snippet.

In short

In the Phpmailer instance settings, set the Encoding parameter to Base64 or quoted-printable. Example:

$mail->Encoding = "base64"

or

$mail->Encoding = "quoted-printable"

Also try reducing the Wordwrap parameter. Normally above 64 causes formatting problems. Therefore a safer value is 50.

$mail->WordWrap = 50;

Fix the HTML semantics

As explained above, there is an unnecessary use of colspan.

Remove all colspan attributes from the code and keep only this snippet

<tr><th colspan='2' bgcolor='#9CC6F7'>DETALHAMENTO DE PEDIDO</th></tr>

Everyone else, you can erase.

  • Hi @Daniel Omine, thank you for your attention. I fixed this, but still continues with the same error (remembering that this error is on a single email server.. UOL). I redid all the code and the variables are initialized before this part visible there in the code, but... unsuccessfully.

  • I believe that the biggest problem is the HTML semantics with the erroneous use of colspan and also the number of characters per line. It is set to 70 the limit but usually 64 is recommended. I added a few more infections to the response. If you want to read only the final section "Summarizing" and "Fix the HTML semantics". I believe that following these instructions can solve the problem.

  • The problem has been solved, however I will follow your recommendations for best practices.

0


I once had a similar problem.

When html has many characters on the same line (before r n), some email servers break these lines when they reach the string buffer limit (insert n) and can invalidate html. For example, making a <td> turn <t\nd> and "messing up" the table.

It is possible that the problem is the concatenated line inside the loop. Maybe it is too long at the end.

Try to concatenate a " r n" at the end of the line in each iteration.

foreach ($resTroca as $value){
    $brindeDesc = $value['bri_Desc'];
    $brindeQtd = $value['tro_Qtd'];
    $html .= "<tr><td width='15%'>".$brindeQtd."</td><td width='85%'>".$brindeDesc."</td></tr>\r\n";
}
  • Very grateful, @Enhardned . I will try to add " n" since my hosting is linux.

  • It is possible that only n will not work. As can be observed, the problem is not found on your server, but on the target server. Thus, r n would be a more comprehensive solution.

  • Yes, @Enhardned, it worked. It worked perfectly. The rest is to match the text with heredoc. Very grateful!

  • If possible, please mark the answer as correct, so we help other people with the same problem.

  • Sorry @Enhardned, how do I mark this answer as correct? It’s the first time I use the stack.. and I don’t see this option... I clicked on the up arrow as helpful answer, would that be?

  • According to this link http://meta.stackexchange.com/questions/23138/how-to-accept-the-answer-on-stack-overflow is below the arrow "This answer is useful" and "this answer is not useful" on the left of the answer (a checkbox)

Show 1 more comment

0

I made the changes in the code below, created a single variable that concatenates the HTML called $html, with each loop loop incrementing.

Note: I withdrew the comment, I was giving error here when posting the code again.

<?PHP
$html ="<!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' 'http://www.w3.org/TR/html4/loose.dtd'>
<html>
    <head>
        <meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
        <meta name='viewport' content='width=device-width, initial-scale=1.0'/>
        <title>Email html</title>
    </head>
    <body style='margin: 0; padding: 0;'>
        <!-- Tabela grande extrena -->
        <table border='0' width='100%' cellpadding='0' cellspacing='0'>
            <tr>
                <td>
                <!-- Tabela interna 1 com 600px -->
                    <table border='1' align='center' width='600px' style='border-collapse: collapse;' cellpadding='0' cellspacing='0'>
                        <tr><td colspan='2' align='center' style='padding: 30px 0 30px 0;'><img src='http://zoiha.com.br/gui/Imagens/Logo_eMail.png' alt='Zoiha' width='50' height='50' style='display: block;' /></td></tr>
                         <tr>
                            <td style='padding:30px 30px 30px 30px'>
                                <!-- Tabela mais interna-->
                                <table border='1' cellpadding='0' cellspacing='0' width='100%'>
                                        <tr><td colspan='2'><p>Olá, ".$nome."!<br>Seu pedido foi realizado com sucesso no portal da Zoiha, conforme detalhes abaixo:<br><br></p></td></tr>
                                        <tr><th colspan='2' bgcolor='#9CC6F7'>DETALHAMENTO DE PEDIDO</th></tr>
                                        <tr><td colspan='2'>PEDIDO n. : ".$numTroca."</td></tr>
                                        <tr><td colspan='2'>DESTINATÁRIO : ".$strDestin."</td></tr>
                                        <tr><td bgcolor='#9CC6F7' align='center' width='15%'><b>QTD</b></td><td bgcolor='#9CC6F7' align='center' width='85%'><b>DESCRIÇÃO</b></td></tr>";
                                        //MONTAGEM DINÂMICA DO MEIO DA TABELA
                                        foreach ($resTroca as $value){
                                            $brindeDesc = $value['bri_Desc'];
                                            $brindeQtd = $value['tro_Qtd'];
                                            $html .= "<tr><td width='15%'>".$brindeQtd."</td><td width='85%'>".$brindeDesc."</td></tr>";
                                        }
                       $html .="</table>";
                $html .="</td>
                        </tr>
                        <tr><td><p><br>Gratos pela preferência, logo mais você receberá novo email com instruções e endereço de retirada do brinde.<br>Atenciosamente, <br>Zoiha</p></td></tr>
                    </table>
                <!-- Fim da Tabela 1 -->
                </td>
            </tr>
        </table>
    <!-- Fim da Tabela grande externa -->
    </body>
</html>";

/**------FORMATANDO O ENVIO DE EMAIL -----*/
$mail = new PHPMailer();
$mail->SetLanguage('pt');          // Define o Idioma
$mail->IsHTML(true);                // Enviar como HTML
$mail->IsSMTP();                       // Define que será enviado por SMTP
$mail->CharSet   = 'utf-8';         // Define a Codificação

$mail->SMTPDebug = 3;            //nível de erro para depuração
$mail->Host      = 'br335.hostgator.com.br'; // Servidor SMTP
$mail->SMTPAuth  = true;               // Caso o servidor SMTP precise de autenticação
$mail->Username  = '[email protected]';  // Usuário ou E-mail para autenticação no SMTP
$mail->Password  = '1234567890';       // Senha do E-mail

$mail->From      = '[email protected]'; // Define o Remetente
$mail->FromName  = 'Zoiha';         // Nome do Remetente
$mail->AddAddress($emailTo,$nome);    // Email e Nome do destinatário
$mail->AddCC('[email protected]','Zoiha'); // Envia Cópia
$mail->WordWrap = 70;
$mail->Subject  = $assunto;           // Assunto da mensagem
$mail->Body = $html;                  // Corpo da mensagem em formato HTML

$enviado = $mail->Send();             // Envia o e-mail
$mail->ClearAllRecipients();          // Limpa os destinatários

if($enviado){ // Testa o envio
            return true;

}else{
            echo "Não foi possível enviar o e-mail.";
            echo "<b>Informações do erro:</b> " . $mail->ErrorInfo; die();
}
  • Thanks @Joker. I made the corrections but it keeps giving the same error to "a certain recipient" (curiously a recipient '.net'). The code is perfect in any email, but in this email the 2nd 'TD' of the dynamic line (the DESCRIPTION) behaves as if it had not been closed. Thus, the description of the products stays outside the cell. I did the test with 2 products, It returned me one product within the QTD cell and the other outside the most internal table. I would like to send an image of your screen here, but I do not know how to do it. I have tried for days a solution without success.

Browser other questions tagged

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