-1

I want to send email with info from form but i dont want that the action of my form send data to external php file, i want to have my php code inside html to only have one file. this is my structure:

<?php
function form_mail($sPara, $sAsunto, $sTexto, $sDe)
{
$bHayFicheros = 0;
$sCabeceraTexto = "";
$sAdjuntos = "";

if ($sDe)$sCabeceras = "From:".$sDe."\n";
else $sCabeceras = "";
$sCabeceras .= "MIME-version: 1.0\n";
foreach ($_POST as $sNombre => $sValor)
$sTexto = $sTexto."\n".$sNombre." = ".$sValor;

foreach ($_FILES as $vAdjunto)
{
if ($bHayFicheros == 0)
{
$bHayFicheros = 1;
$sCabeceras .= "Content-type: multipart/mixed;";
$sCabeceras .= "boundary=\"--_Separador-de-mensajes_--\"\n";

$sCabeceraTexto = "----_Separador-de-mensajes_--\n";
$sCabeceraTexto .= "Content-type: text/plain;charset=iso-8859-1\n";
$sCabeceraTexto .= "Content-transfer-encoding: 7BIT\n";

$sTexto = $sCabeceraTexto.$sTexto;
}
if ($vAdjunto["size"] > 0)
{
$sAdjuntos .= "\n\n----_Separador-de-mensajes_--\n";
$sAdjuntos .= "Content-type: ".$vAdjunto["type"].";name=\"".$vAdjunto["name"]."\"\n";;
$sAdjuntos .= "Content-Transfer-Encoding: BASE64\n";
$sAdjuntos .= "Content-disposition: attachment;filename=\"".$vAdjunto["name"]."\"\n\n";

$oFichero = fopen($vAdjunto["tmp_name"], 'r');
$sContenido = fread($oFichero, filesize($vAdjunto["tmp_name"]));
$sAdjuntos .= chunk_split(base64_encode($sContenido));
fclose($oFichero);
}
}

if ($bHayFicheros)
$sTexto .= $sAdjuntos."\n\n----_Separador-de-mensajes_----\n";
return(mail($sPara, $sAsunto, $sTexto, $sCabeceras));
}

//cambiar aqui el email
if (form_mail("victor@99minutos.com", $_POST[asunto],
"Los datos introducidos en el formulario son:\n\n", $_POST[email]))
echo "Su formulario ha sido enviado con exito";
?>
<html>
<body>
<form name='formulario' id='formulario' method='post' target='_self' enctype="multipart/form-data">
<p>Nombre <input type='text' name='Nombre' id='Nombre'></p>
<p>E-mail
<input type='text' name='email' id='email'>
</p>
<p>Asunto
<input type='text' name='asunto' id='asunto' />
</p>
<p>Mensaje
<textarea name="mensaje" cols="50" rows="10" id="mensaje"></textarea>
</p>
<p>Adjuntar archivo: <input type='file' name='archivo1' id='archivo1'></p>
<p>
<input type='submit' value='Enviar'>
</p>
</form>
</body>
</html>

But i dont know why the php code is not correctly reading, this the example of how is showing

enter image description here

victor
  • 507
  • 1
  • 7
  • 20

3 Answers3

0

Ok $_POST is an array. It looks like this:

  Array $_POST
     [0]  => Array Won
     [1]  => Array Lost

maybe try something like this:

foreach ($_POST as $sValor) {
    $sTexto = $sTexto."\n".$sValor." = ".$sValor[0];
}
kuchar
  • 607
  • 1
  • 8
  • 23
0

Firstly, add tag to submit button (name="enviar" ), with out it, when page is loaded it will try read empty inputs and send mail.

<input type='submit' name="enviar" value='Enviar'>

then add

if (isset($_POST["enviar"])) {
  // if (form_mail( ..... goes here
}

change foreach and add braces ( {} ) to first foreach, also write "asunto" and "mail" in $_POST with inverted commas

$_POST["asunto"];
$_POST["mail"];
0

Your php code is not executing at all, and instead is just rendering itself as text.

I reproduced the exact same issue you are having by copying your code to a "test.html" file and then simply opening it with a browser (Firefox in my case).

There is nothing "wrong" with your php code. Probably your problem is just that you need to change your file extension from .html to .php. That way whatever server you have deployed will probably recognize the file as php and it will execute the php code inside it instead of just printing it.

As a note, html code can be (and usually is) embedded into a .php file.

Alfro
  • 458
  • 9
  • 22
  • Well i change the extension of the file, but now if i refresh the page immediately send me a mail, how can stop this and only fire when i click in submit button? – victor Aug 10 '16 at 23:22
  • @victor I think you should edit the question (or close this one and open another one) with that particular issue. Notice this question was marked as duplicated. For making php doing something when a form is submitted, check out this question: http://stackoverflow.com/a/9752226/4257747 – Alfro Aug 11 '16 at 07:45
  • This answer here seems mostly right as well: http://stackoverflow.com/a/38884837/4257747 – Alfro Aug 11 '16 at 07:46