HTML code reading error in Mail.php function

Asked

Viewed 66 times

0

So I created a contact form that I would need to have it formatted in the recipient email. That’s why I left HTML a little more structured and used CSS Inline for the settings, as dictated by good practices for sending HTML by email.

I stored all the code below within a variable (from which I use it later in the mail function for the message body):

<!DOCTYPE html>
<style>
table {
    border-collapse: collapse;
}

td {
    padding: 5px 10px;
    font-family: arial;
    font-size: 14px;
}
h2 {
    padding: 0;
    margin: 30px 0 5px 0;
    font-family: arial;
    font-size: 16px;
    text-transform: uppercase;
}
</style>

<body>
<table width="790" align="center">
    <tbody align="center" width="790">
            <caption><h2 align="left">1. Dados Pessoais</h2></caption>
            <tr><td colspan="3"><strong>Nome:</strong> $nome </td></tr>
            <tr><td><strong>Estado Civil:</strong> $estadoCivil</td><td><strong>Resid.:</strong> $telefoneFixo</td><td><strong>Cel.:</strong> $telefoneCelular</td></tr>
            <tr><td><strong>Sexo:</strong> $sexo </td><td><strong>Nascimento:</strong> $datanascimentoPessoal <td><strong>Naturalidade:</strong> $naturalidade </td>
            <tr><td colspan="3"><strong>Endereço:</strong> $endereco</td></tr></tr>
            <tr><td><strong>Bairro:</strong> $bairro </td><td><strong>CEP:</strong> $cep </td><td><strong>Cidade:</strong> $cidade </td></tr>
            <tr><td><strong>Nome do Pai:</strong> $nomePai </td><td><strong>Nome da Mãe:</strong> $nomeMae </td></tr>
            <tr><td><strong>Gru de Instrução:</strong> $grauInstrucao </td><td><strong>Completo?</strong> $grauCompleto </td><td><strong>Raça/Cor:</strong> $racaCor</td></tr>
    </tbody>
</table>

<table width="790" align="center">
    <tbody align="center" width="790">
            <caption><h2 align="left">2. Beneficiários</h2></caption>
            <tr><td><strong>Cônjuge:</strong> $conjuge </td><td><strong>Nascimento:</strong> $datanascimentoConjuge </td><td><strong>IRRF:</strong> $irrfConjuge </td></tr>
            <tr><td><strong>Dependente:</strong> $dep1 </td><td><strong>Nascimento:</strong> $datanascimentoDep1 </td><td><strong>IRRF:</strong> $irrfDep1 </td></tr>
            <tr><td><strong>Dependente:</strong> $dep2 </td><td><strong>Nascimento:</strong> $datanascimentoDep2 </td><td><strong>IRRF:</strong> $irrfDep2 </td></tr>
            <tr><td><strong>Dependente:</strong> $dep3 </td><td><strong>Nascimento:</strong> $datanascimentoDep3 </td><td><strong>IRRF:</strong> $irrfDep3 </td></tr>
            <tr><td><strong>Dependente:</strong> $dep4 </td><td><strong>Nascimento:</strong> $datanascimentoDep4 </td><td><strong>IRRF:</strong> $irrfDep3 </td></tr>
    </tbody>
</table>

<table width="790" align="center">
    <tbody align="center" width="790">
            <caption><h2 align="left">3. Documentação</h2></caption>
            <tr><td><strong>RG:</strong> $rg </td><td><strong>Orgão Exp.:</strong> $orgaoExpedidor </td><td><strong>Data de Exp.:</strong> $dataexpedicaoRG </td><td><strong>CPF:</strong> $cpf </td></tr>
            <tr><td><strong>Título de Eleitor:</strong> $tituloEleitor </td><td><strong>Zona:</strong> $zonaEleitor </td><td><strong>Seção:</strong> $secaoEleitor</td></tr>
            <tr><td><strong>CTPS:</strong> $ctps </td><td><strong>Série:</strong> $serieCTPS </td><td><strong>UF:</strong> $ufCTPS </td><td><strong>Data de Exp.:</strong> $dataexpedicaoCTPS </td><td><strong>PIS:</strong> $pis </td></tr>
            <tr><td><strong>Carteira de Hab.:</strong> $carteiraHabilitacao </td><td><strong>Categoria:</strong> $categoriaHab </td><td><strong>Vencimento:</strong> $vencimentoHab</td><td><strong>UF:</strong> $ufHab </td><td><strong>Reservista:</strong> $reservista</td></tr>
    </tbody>
</table>

<table width="790" align="center">
    <tbody align="center" width="790">
            <caption><h2 align="left">4. Dados Bancários</h2></caption>
            <tr><td><strong>Banco:</strong> $nomeBanco </td> <td><strong>Agência:</strong> $agenciaBanco </td><td><strong>Conta:</strong> $contaBanco </td></tr>            
    </tbody>
</table>

<table width="790" align="center">
    <tbody align="center" width="790">
            <caption><h2 align="left">5. Benefícios</h2></caption>
            <tr><td><strong>Vale Transporte:</strong> $valeTransporte </td><td><strong>Qtde por dia:</strong> $qtdeVT </td><td><strong>Valor da Tarifa:</strong> $valorVT </td><td><strong>Empresa:</strong> $empres </td></tr>
            <tr><td colspan="4"><strong>Número Cartão Transporte/SIC:</strong> $numeroTransporte </td></tr>     
            <tr><td><strong>Vale Alimentação:</strong> $valeAlimentacao </td><td><strong>Vale Refeição:</strong> $valeRefeicao </td><td><strong>Valor por Dia:</strong> $valorDia </td></tr>
    </tbody>
</table>

<table width="790" align="center">
    <tbody align="center" width="790">
            <caption><h2 align="left">6. Informações Adicionais</h2></caption>
            <tr><td colspan="2"><strong>Centro de Custo:</strong> $centroCusto</td></tr>
            <tr><td><strong>Já trabalhou nesta empresa:</strong> $jaTrabalhou</td> <td><strong>Contrato de Experiência:</strong> $contratoExp</td></tr>
            <tr><td colspan="2"><strong>Período de Experiência:</strong> $periodoExp</td></tr>
            <tr><td colspan="2"><strong>Sindicato:</strong> $sindicato</td></tr>
            <tr><td colspan="2"><strong>Forma de Contratação:</strong> $formaContratacao</td></tr>
            <tr><td><strong>Cargo e Funação: </strong>$cargoFuncao </td> <td><strong>Data de Início: </strong>$dataInicio</td></tr>
            <tr><td><strong>Horário de Trabalho: </strong>$horarioTrabalho </td> <td><strong>Intervalo: </strong>$intervalo</td></tr>
            <tr><td><strong>Sábado: </strong>$sabado </td> <td><strong>Horário: </strong>$horarioSabado </td> <td><strong>Qtde Sábados Mês: </strong>$qtdeSabado</td></tr>
            <tr><td colspan="2"><strong>Salário: </strong>$salario </td></tr>
            <tr><td colspan="2"><strong>Já realizou contribuição sindical este ano? </strong>$contrSindical</td></tr>
            <tr><td colspan="2"><strong>Insalubridade: </strong>$insalubridade</td></tr>
            <tr><td colspan="2"><strong>Crachá: </strong>$cracha</td></tr>
    </tbody>
</table>

<table align="center" width="790">
    <tfooter align="center" width="790">        
    <tr><td align="center">$data</td></tr>
    </tfooter>
</table>
</body>
</html>

Set the headers correctly to indicate the message body settings:

   $headers = "xxxxxxxxxxxx";
   $headers .= 'MIME-Version: 1.0' . "\r\n";
   $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

However, when submitting the form, it is not recognizing the HTML settings.

It keeps accusing errors like (in the case of witdh="790" table):

Parse error: syntax error, unexpected '790' (T_LNUMBER)

And consequently it accuses error in all other inline settings such as width value, align, colspan, etc.

Does anyone know why this is happening?

  • 1

    You can not know exactly pq has no variable assignment in the question but I suspect that has a double quote before and width=" is interpreted as the closure.

  • Probably what @rray commented, when I have to send this email I leave the html file .html and upload the file with file_get_contents and give a replace variables. Type the $data would be like this <tr><td align="center">@data@</td></tr>.

  • When will you store the HTML in the variable, as you are doing?

  • It was exactly that @rray ended up not paying attention to me. The error occurs because it is being interpreted the closure of the same variable. What would be the viable alternatives to this situation? I didn’t understand very well the suggestion of fernandoandrade (I’m still beginner).

  • @caiocabale $message = "my html";

  • You can use heredoc to build a template see in that reply

Show 1 more comment

1 answer

0


Assign the variable like this:

$var = '<!DOCTYPE html>
<style>
table {
    border-collapse: collapse;
}

td {
    padding: 5px 10px;
    font-family: arial;
    font-size: 14px;
}
h2 {
    padding: 0;
    margin: 30px 0 5px 0;
    font-family: arial;
    font-size: 16px;
    text-transform: uppercase;
}
</style>

<body>
<table width="790" align="center">
    <tbody align="center" width="790">
            <caption><h2 align="left">1. Dados Pessoais</h2></caption>
            <tr><td colspan="3"><strong>Nome:</strong> $nome </td></tr>
            <tr><td><strong>Estado Civil:</strong> $estadoCivil</td><td><strong>Resid.:</strong> $telefoneFixo</td><td><strong>Cel.:</strong> $telefoneCelular</td></tr>
            <tr><td><strong>Sexo:</strong> $sexo </td><td><strong>Nascimento:</strong> $datanascimentoPessoal <td><strong>Naturalidade:</strong> $naturalidade </td>
            <tr><td colspan="3"><strong>Endereço:</strong> $endereco</td></tr></tr>
            <tr><td><strong>Bairro:</strong> $bairro </td><td><strong>CEP:</strong> $cep </td><td><strong>Cidade:</strong> $cidade </td></tr>
            <tr><td><strong>Nome do Pai:</strong> $nomePai </td><td><strong>Nome da Mãe:</strong> $nomeMae </td></tr>
            <tr><td><strong>Gru de Instrução:</strong> $grauInstrucao </td><td><strong>Completo?</strong> $grauCompleto </td><td><strong>Raça/Cor:</strong> $racaCor</td></tr>
    </tbody>
</table>

<table width="790" align="center">
    <tbody align="center" width="790">
            <caption><h2 align="left">2. Beneficiários</h2></caption>
            <tr><td><strong>Cônjuge:</strong> $conjuge </td><td><strong>Nascimento:</strong> $datanascimentoConjuge </td><td><strong>IRRF:</strong> $irrfConjuge </td></tr>
            <tr><td><strong>Dependente:</strong> $dep1 </td><td><strong>Nascimento:</strong> $datanascimentoDep1 </td><td><strong>IRRF:</strong> $irrfDep1 </td></tr>
            <tr><td><strong>Dependente:</strong> $dep2 </td><td><strong>Nascimento:</strong> $datanascimentoDep2 </td><td><strong>IRRF:</strong> $irrfDep2 </td></tr>
            <tr><td><strong>Dependente:</strong> $dep3 </td><td><strong>Nascimento:</strong> $datanascimentoDep3 </td><td><strong>IRRF:</strong> $irrfDep3 </td></tr>
            <tr><td><strong>Dependente:</strong> $dep4 </td><td><strong>Nascimento:</strong> $datanascimentoDep4 </td><td><strong>IRRF:</strong> $irrfDep3 </td></tr>
    </tbody>
</table>

<table width="790" align="center">
    <tbody align="center" width="790">
            <caption><h2 align="left">3. Documentação</h2></caption>
            <tr><td><strong>RG:</strong> $rg </td><td><strong>Orgão Exp.:</strong> $orgaoExpedidor </td><td><strong>Data de Exp.:</strong> $dataexpedicaoRG </td><td><strong>CPF:</strong> $cpf </td></tr>
            <tr><td><strong>Título de Eleitor:</strong> $tituloEleitor </td><td><strong>Zona:</strong> $zonaEleitor </td><td><strong>Seção:</strong> $secaoEleitor</td></tr>
            <tr><td><strong>CTPS:</strong> $ctps </td><td><strong>Série:</strong> $serieCTPS </td><td><strong>UF:</strong> $ufCTPS </td><td><strong>Data de Exp.:</strong> $dataexpedicaoCTPS </td><td><strong>PIS:</strong> $pis </td></tr>
            <tr><td><strong>Carteira de Hab.:</strong> $carteiraHabilitacao </td><td><strong>Categoria:</strong> $categoriaHab </td><td><strong>Vencimento:</strong> $vencimentoHab</td><td><strong>UF:</strong> $ufHab </td><td><strong>Reservista:</strong> $reservista</td></tr>
    </tbody>
</table>

<table width="790" align="center">
    <tbody align="center" width="790">
            <caption><h2 align="left">4. Dados Bancários</h2></caption>
            <tr><td><strong>Banco:</strong> $nomeBanco </td> <td><strong>Agência:</strong> $agenciaBanco </td><td><strong>Conta:</strong> $contaBanco </td></tr>            
    </tbody>
</table>

<table width="790" align="center">
    <tbody align="center" width="790">
            <caption><h2 align="left">5. Benefícios</h2></caption>
            <tr><td><strong>Vale Transporte:</strong> $valeTransporte </td><td><strong>Qtde por dia:</strong> $qtdeVT </td><td><strong>Valor da Tarifa:</strong> $valorVT </td><td><strong>Empresa:</strong> $empres </td></tr>
            <tr><td colspan="4"><strong>Número Cartão Transporte/SIC:</strong> $numeroTransporte </td></tr>     
            <tr><td><strong>Vale Alimentação:</strong> $valeAlimentacao </td><td><strong>Vale Refeição:</strong> $valeRefeicao </td><td><strong>Valor por Dia:</strong> $valorDia </td></tr>
    </tbody>
</table>

<table width="790" align="center">
    <tbody align="center" width="790">
            <caption><h2 align="left">6. Informações Adicionais</h2></caption>
            <tr><td colspan="2"><strong>Centro de Custo:</strong> $centroCusto</td></tr>
            <tr><td><strong>Já trabalhou nesta empresa:</strong> $jaTrabalhou</td> <td><strong>Contrato de Experiência:</strong> $contratoExp</td></tr>
            <tr><td colspan="2"><strong>Período de Experiência:</strong> $periodoExp</td></tr>
            <tr><td colspan="2"><strong>Sindicato:</strong> $sindicato</td></tr>
            <tr><td colspan="2"><strong>Forma de Contratação:</strong> $formaContratacao</td></tr>
            <tr><td><strong>Cargo e Funação: </strong>$cargoFuncao </td> <td><strong>Data de Início: </strong>$dataInicio</td></tr>
            <tr><td><strong>Horário de Trabalho: </strong>$horarioTrabalho </td> <td><strong>Intervalo: </strong>$intervalo</td></tr>
            <tr><td><strong>Sábado: </strong>$sabado </td> <td><strong>Horário: </strong>$horarioSabado </td> <td><strong>Qtde Sábados Mês: </strong>$qtdeSabado</td></tr>
            <tr><td colspan="2"><strong>Salário: </strong>$salario </td></tr>
            <tr><td colspan="2"><strong>Já realizou contribuição sindical este ano? </strong>$contrSindical</td></tr>
            <tr><td colspan="2"><strong>Insalubridade: </strong>$insalubridade</td></tr>
            <tr><td colspan="2"><strong>Crachá: </strong>$cracha</td></tr>
    </tbody>
</table>

<table align="center" width="790">
    <tfooter align="center" width="790">        
    <tr><td align="center">$data</td></tr>
    </tfooter>
</table>
</body>
</html>';

when we use double quotes in html, php allows using single quotes in the string definition.

  • Interesting @NIZZOLA, did not know of this possibility, solved my problem, the form now gives no more error! However, in the email it is not printing the value of the variable but the variable itself. For example: "Name: $name". I must change something in the syntax by using simple quotes?

  • 1

    When using single quotes variables are not interpreted.

  • Concatenate the variables like this: $name = 'Abde' . $var . 'fghij'; or the inversion of double quotes by single so $name = "Abde $var tags html so <input type='text'>";

  • @NIZZOLA apologizes my ignorance, but I didn’t quite understand how to do this procedure you suggested. Should the concatenation be made at the time of the variables' declaration or at the time of their declaration at the beginning? If you can show with 1 line of code that I’ve assembled how the syntax should look I think it would facilitate my understanding. If you need it, follow how I declared some variables: name = $_POST['name']; $stateCivil = $_POST['marital status']; $sex = $_POST['sex']; $phone Number = $_POST['tel']; $phone Number = $_POST['cel'];

  • @Viniciusgabriel I already had to do this and there are two forms, you put in a notepad your html and replace all double Apas by single so you can use double quotes in php as delimiters. example: $var = " <input type='text'>"; see that simple Apas will be playing the role in normal html and then you can put variables inside. Another way is to concatenate the string you end the string where the variable enters and then continues. example: $var = 'Xyz'. $var2. abdde; without touching your html code.

  • @Viniciusgabriel please check and put the question as answered.

  • @NIZZOLA I’m sorry, I’m new to the portal, I couldn’t find where to put the question as an answer? It was not just to signal acceptance of the answer in your code reply?

Show 2 more comments

Browser other questions tagged

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