email with unconfigured table

0

The code below sends order confirmation emails correctly, however it arrives unconfigured for just 'a single recipient'. In case, the product in the description field appears printed before anything on the screen and some words appear together.

/*FORMATAÇÃOHTMLDAMENSAGEM*/$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();
    }
    
asked by anonymous 07.07.2016 / 23:38

3 answers

0

I once had a similar problem.

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

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

Try to concatenate a "\ r \ n" at the end of the line at 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";
}
    
11.07.2016 / 23:17
0

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

Note: I removed the comment, it was giving error here in the time to post 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();
}
    
08.07.2016 / 19:38
0

In internal table 1, in the first <td> , you are declaring colspan=2 unnecessarily.

Some renderers can interpret literally and others just ignore.

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

Try removing the colspan attribute from the cells where it becomes unnecessary.

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

See an example of what happens when text content is divided: link

This test has been reproduced 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 any doubts about chunk split, if possible, take the original email and read the original code received by the server, (in your case UOL). You need to open and read the original message code. But I can not say if the webmail of the provider has such an option.

Besides bo chunk_split, other formats may 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 result tin in the letters as =0A=0A =0A =0A = =0A =0A =0A =0A =0A =0A = =0A =0A =0A =0A =0A =0A = but in an email client it is displayed correctly because the decode is done automatically. On here important that I want to show is how the formatting of your table also breaks with that format. See the visual result in the snippet.

Summarizing

In the PHPMailer instance settings, set the Encoding parameter to either base64 or quoted-printable. Example:

$mail->Encoding = "base64"

or

$mail->Encoding = "quoted-printable"

Also try reducing the WordWrap parameter. Typically above 64 causes formatting problems. Therefore a more secure value is 50.

$mail->WordWrap = 50;

Correct HTML semantics

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

Remove all colspan attributes from the code and keep only in this section

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

All others can be deleted.

    
11.07.2016 / 22:30