How to Resolve Syntax Error

Asked

Viewed 151 times

-3

A form with attachment that misses the shipment, on line 14. 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);
?>
  • 2

    Don’t post your code in image form. [en.so] has code support, just paste your code into the question editor, select it and press the shortcut Ctrl+K. You can also use the button {} editor. Please log in to [Edit] and make the change.

  • I’m sorry Anderson, but I can’t make it, but I’m still gonna try again.

  • So I recommend that you do before anything the [tour] by the site to learn at least the basics of how the site works. In it you will find guides on how to properly format your questions and answers.

2 answers

2

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

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

Note that there is a point where there are double quotation marks and the variable. You can find more examples in the official documentation: http://php.net/manual/en/language.operators.string.php

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

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

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

  • Even so I could not correct Amadeus, help me in this face, I need urgent these corrections.!

  • @Days on line 14, concatenation is done wrong. You have corrected this?

  • no... sorry. how do I?

  • @Days, take a look at my answer. Your solution is in the first example I gave

  • I’m beginner, and I’m studying PHP now, for me it’s very recent yet... but I understand your concern so I can learn, but can’t identify the point.

1

On line 14 missing "escape" the quotation marks inside the "quotation marks":

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

Do this:

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

It is also necessary to escape the quotation marks on line 22:

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

Should stay like this:

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

It is also necessary to escape the quotation marks on line 24:

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

Should stay 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);

Browser other questions tagged

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