Send email with attachment and default title in FORM

Asked

Viewed 76 times

0

I took on the internet a PHP form to attach files but the email arrives without subject.

I would like to leave a pre-defined and hidden Subject and after sending back to the site (today it ends on the white screen).

<?php
//pego os dados enviados pelo formulario
$nome = $_POST["nome"];
$email = "[email protected]";
$mensagem = $_POST["mensagem"];
$assunto = $_POST["Contato Pelo Site"];
$email_from = $_POST["email_from"];
$cidade = $_POST["cidade"];
//formato o campo da mensagem
$mensagem = wordwrap( $mensagem, 50, "
", 1);
//valido os emails
if (!ereg("^([0-9,a-z,A-Z]+)([.,_]([0-9,a-z,A-Z]+))*[@]([0-9,a-z,A-Z]+)([.,_,-]([0-9,a-z,A-Z]+))*[.]([0-9,a-z,A-Z]){2}([0-9,a-z,A-Z])?$", $email)){
echo"<center>Digite um email valido</center>";
echo "<center><a href=\"java script:history.go(-1)\">Voltar</center></a>";
exit;
}
if (!ereg("^([0-9,a-z,A-Z]+)([.,_]([0-9,a-z,A-Z]+))*[@]([0-9,a-z,A-Z]+)([.,_,-]([0-9,a-z,A-Z]+))*[.]([0-9,a-z,A-Z]){2}([0-9,a-z,A-Z])?$", $email_from)){
echo "<center>Digite um email valido</center>";
echo "<center><a href=\"java script:history.go(-1)\"><center>Voltar</center></a>";
exit;
}
$arquivo = isset($_FILES["arquivo"]) ? $_FILES["arquivo"] : FALSE;
if(file_exists($arquivo["tmp_name"]) and !empty($arquivo)){
$fp = fopen($_FILES["arquivo"]["tmp_name"],"rb");
$anexo = fread($fp,filesize($_FILES["arquivo"]["tmp_name"]));
$anexo = base64_encode($anexo);
fclose($fp);
$anexo = chunk_split($anexo);
$boundary = "XYZ-" . date("dmYis") . "-ZYX";
$mens = "--$boundary\n";
$mens .= "Content-Transfer-Encoding: 8bits\n";
$mens .= "Content-Type: text/html; charset=\"ISO-8859-1\"\n\n"; //plain
$mens .= "$mensagem\n";
$mens .= "--$boundary\n";
$mens .= "Content-Type: ".$arquivo["type"]."\n";
$mens .= "Content-Disposition: attachment; filename=\"".$arquivo["name"]."\"\n";
$mens .= "Content-Transfer-Encoding: base64\n\n";
$mens .= "$anexo\n";
$mens .= "--$boundary--\r\n";
$headers = "MIME-Version: 1.0\n";
$headers .= "From: \"$nome\" <$email_from>\r\n";
$headers .= "Content-type: multipart/mixed; boundary=\"$boundary\"\r\n";
$headers .= "$boundary\n";
//envio o email com o anexo
mail($email,$assunto,$cidade,$mens,$headers);
echo"Email enviado. Breve entraremos em Contato!";
}
//se não tiver anexo
else{
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: \"$nome\" <$email_from>\r\n";
//envia o email sem anexo
mail($email,$assunto,$mensagem, $headers);
echo"Email enviado. Breve entraremos em Contato!";
}
?>

HTML PART

<div id="contact" class="page"> 
  <div class="container"> 
    <!-- Title Page -->
    <div class="row"> 
      <div class="span12"> 
        <div class="title-page"> 
          <h2 class="title">Entre em contato conosco</h2>
          <h3 class="title-description">Teremos o prazer de atender Você</h3>
        </div>
      </div>
    </div>
    <div class="row"> 
      <div class="span9"> 
        <script language="javascript" type="text/javascript">
function checa_formulario(email){
if (email.nome.value == ""){
alert("Por Favor não deixe o seu nome em branco!!!");
email.nome.focus();
return (false);
}
if (email.email_from.value == ""){
alert("Por Favor não deixe o seu email em branco!!!");
email.email_from.focus();
return (false);
}
if (email.email.value == ""){
alert("não deixe o email destinatario em branco!!!");
email.email.focus();
return (false);
}
if (email.telefone.value == ""){
alert("não deixe o telefone em branco!!!");
email.telefone.focus();
return (false); 
}   
}
</script>
        <title>Anexar Fotos e Descrever sua Necessidade.</title>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        <style type="text/css">
</style></head>
        <body onLoad="document.email.nome.focus();"> 
        <form onSubmit="return checa_formulario(this)" action="contact.php" method="post" enctype="multipart/form-data" name="email">
          <h3 align="center" class="style1">Anexar Fotos e Descrever sua Necessidade abaixo</h3>
          <table width="32%" border="0" align="center">
            <tr> 
              <td><div align="right"><span class="texto">Nome</span></div></td>
              <td><input name="nome" type="text" id="nome"></td>
            </tr>
            <tr> 
              <td width="33%"><div align="right" class="texto">Email:</div></td>
              <td width="67%"><input name="email_from" type="text" class="email"></td>
            </tr>
            <tr> 
              <td><div align="right" class="texto"></div></td>
            </tr>
            <tr> 
              <td><div align="center" class="texto">Telefone, Cidade e Mensagem</div></td>
              <td><textarea name="mensagem" cols="50" rows="10" id="mensagem"></textarea></td>
            </tr>
            <tr> 
              <td><div align="right" class="texto">Anexo</div></td>
              <td><input name="arquivo" type="file"></td>
            </tr>
            <tr> 
              <td> </td>
              <td><input type="submit" name="Submit" href="Contato Via Site#">
                Enviar mensagem</td>
            </tr>
          </table>
        </form>
  • Hello @Paulo, I see that a solution proposed by the community helped you, consider accepting it, accept an answer is the best way to thank those who helped you, it will also help those who have the same problem and still keep the site healthy because your question ceases to be an unresolved question. -- It’s only worth you take a look at our [Tour] =D

1 answer

0

Correct the subject to:

$assunto ="Contato Pelo Site";

In its variable, the POST you won’t find any request coming with the given name.
When you want to use preset, just leave as a normal string.

Read: https://php.net/manual/en/tutorial.forms.php

  • show. I did it. The page ends on a white screen with a thank you message. How to return? I thought of a 1-second timer.......

Browser other questions tagged

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