How to resolve syntax error [closed]

-3

A form with an attachment that fails to send on line 14. Someone help

<?php
$nome = $_POST['nome'];
$arquivo = $_FILES["arquivo"];
// Para quem vai ser enviado o email
$para = "eadamaral[@]gmail.com";
$boundary = "XYZ-".date("dmYis")."-ZYX";
$fp = fopen($arquivo["tmp_name"], "rb"); // abre o arquivo enviado
$anexo = fread($fp, filesize($arquivo["tmp_name"])); // calcula o tamanho
$anexo = base64_encode($anexo); // codifica o anexo em base 64
fclose($fp); // fecha o arquivo
// cabeçalho do email
$headers  = "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/mixed; ";
$headers .= "boundary="$boundary"\r\n";
$headers .= "$boundary\n";
// email
$mensagem  = "--$boundary\n";
$mensagem .= "Content-Type: text/html; charset='utf-8'\n";
$mensagem .= "<strong>Nome: </strong> $nome \r\n";
$mensagem .= "--$boundary \n";
// anexo
$mensagem .= "Content-Type: ".$arquivo["type"]."; name="".$arquivo['name']."" \n";
$mensagem .= "Content-Transfer-Encoding: base64 \n";
$mensagem .= "Content-Disposition: attachment; filename="".$arquivo['name']."" \r\n";
$mensagem .= "$anexo \n";
$mensagem .= "--$boundary \n";
// enviar o email
mail($para, $assunto, $mensagem, $headers);
?>
    
asked by anonymous 12.05.2017 / 02:56

2 answers

2

The way that PHP works with string concatenation is as follows:

$var2 = "concatenado";
$var = "texto" . $var2 . "mais texto";

Notice that there is a point where there is the closing of the double quotes and the variable. You can find more examples in the official documentation: link

An alternative to using '.', which is relatively criticized by programmers in other languages, is to concatenate this way:

$var2 = "concatenado";
$var = "texto {$var2}";

Remembering that the previous example will only work in strings between double quotation marks ('' '). Single quotes (' '') will not concatenate the value of variable $ var2, simply "{$ var2}" will appear at the end of string.

    
12.05.2017 / 03:04
1

In line 14 you have to "escape" the quotes within "the quotation marks":

$headers .= "boundary="$boundary"\r\n";

Do this:

$headers .= "boundary=\"$boundary\"\r\n";

It is also necessary to escape the quotes on line 22:

$mensagem .= "Content-Type: ".$arquivo["type"]."; name="".$arquivo['name']."" \n";

It should look like this:

$mensagem .= "Content-Type: ".$arquivo["type"]."; name=\"".$arquivo['name']."\" \n";

It is also necessary to escape the quotes on line 24:

$mensagem .= "Content-Disposition: attachment; filename="".$arquivo['name']."" \r\n";

It should look like this:

$mensagem .= "Content-Disposition: attachment; filename=\"".$arquivo['name']."\" \r\n";

The corrected code should look like this:

<?php
$nome = $_POST['nome'];
$arquivo = $_FILES["arquivo"];
// Para quem vai ser enviado o email
$para = "eadamaral[@]gmail.com";
$boundary = "XYZ-".date("dmYis")."-ZYX";
$fp = fopen($arquivo["tmp_name"], "rb"); // abre o arquivo enviado
$anexo = fread($fp, filesize($arquivo["tmp_name"])); // calcula o tamanho
$anexo = base64_encode($anexo); // codifica o anexo em base 64
fclose($fp); // fecha o arquivo
// cabeçalho do email
$headers  = "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/mixed; ";
$headers .= "boundary=\"$boundary\"\r\n";
$headers .= "$boundary\n";
// email
$mensagem  = "--$boundary\n";
$mensagem .= "Content-Type: text/html; charset='utf-8'\n";
$mensagem .= "<strong>Nome: </strong> $nome \r\n";
$mensagem .= "--$boundary \n";
// anexo
$mensagem .= "Content-Type: ".$arquivo["type"]."; name=\"".$arquivo['name']."\" \n";
$mensagem .= "Content-Transfer-Encoding: base64 \n";
$mensagem .= "Content-Disposition: attachment; filename=\"".$arquivo['name']."\" \r\n";
$mensagem .= "$anexo \n";
$mensagem .= "--$boundary \n";
// enviar o email
mail($para, $assunto, $mensagem, $headers);
    
12.05.2017 / 09:19