简体   繁体   中英

Send 2 files by email using a form HTML and PHP

Hi everybody I'm searching to solve a problem (looks easy but I don't find a solution).

I have a form HTML and I'm trying to send datas and files to a specific email. The email arrives correctly but the 2 file attached have a strange name, it look like a temporary name file. It like I lost the name of the file and the extension.

Somebody could help me to understand what I'm doing wrong? I'm not really an expert on PHP.

Thanks!

HTML file

<form id="form1" name="form1" method="post" action="email2.php" enctype="multipart/form-data">
<fieldset>
<legend>Canditatura</legend>
<label>Nome:</label> <input type="text" placeholder="Inserisci il tuo nome" name="nome"  size="30px" required="true"><br>
<label>Cognome:</label> <input type="text" placeholder="Inserisci il tuo cognome" name="cognome" size="30px" required="true"><br>
<label>Email:</label> <input type="email" name="email" placeholder="example@example.it" size="30px" required="true"><br>
<label>Telefono:</label> <input type="tel" name="telefono" placeholder="+39" size="30px"><br>
<label>Allegato 1:</label> <input type="file" id="allegato" name="allegato" required="true"><br>
<label>Allegato 2:</label> <input type="file" id="allegato2" name="allegato2" required="true"><br><br><br>
<label class="lprivacy"><input type="checkbox" required="true">Accetto normativa sulla Privacy</label><input type="submit" value="Invia Canditatura" name="submit">
</fieldset>
</form>

PHP File

    <?php
$allegato = $_FILES['allegato']['tmp_name'];
$allegato_type = $_FILES['allegato']['type'];
$allegato_name = $_FILES['allegato']['name'];

$allegato2 = $_FILES['allegato2']['tmp_name'];
$allegato2_type = $_FILES['allegato2']['type'];
$allegato2_name = $_FILES['allegato2']['name'];

$email = $_POST['email'];
$nome = $_POST['nome'];
$cognome = $_POST['cognome'];
$telefono = $_POST['telefono'];

  mail_attachment("test@test.com","Subject","Nuova canditatuda da <b>$nome $cognome</b>. <br> Telefono: $telefono <br> Email: $email ",array("$allegato","$allegato2"));
  function mail_attachment($to, $subject, $message, $files) {
      $headers = "From: no-reply@mail.com";
      $semi_rand = md5(time());
      $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
      $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";

      $message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
      $message .= "--{$mime_boundary}\n";

      foreach ($files as $file) {

        $filename = end(explode("/",$file));

        $data = file_get_contents($file);

        $data = chunk_split(base64_encode($data));

        $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$file\"\n" .
          "Content-Disposition: attachment;\n" . " filename=\"$file\"\n" .
          "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
        $message .= "--{$mime_boundary}\n";
      }
        echo (@mail($to, $subject, $message, $headers)) ? "<p>Messaggio spedito correttamente a $to!</p>" : "<p>ERRORE! Messaggio non spedito a $to!</p>";
  } // mail-attachment ?>

According to your code, your sending temp file but your not sending the original file name, so that's the received file name has some random file name.

Check bellow code

<?php    
$email = $_POST['email'];
$nome = $_POST['nome'];
$cognome = $_POST['cognome'];
$telefono = $_POST['telefono'];

  mail_attachment("test@test.com","Subject","Nuova canditatuda da <b>$nome $cognome</b>. <br> Telefono: $telefono <br> Email: $email ",array("allegato","allegato2"));
  function mail_attachment($to, $subject, $message, $files) {
      $headers = "From: no-reply@mail.com";
      $semi_rand = md5(time());
      $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
      $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";

      $message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
      $message .= "--{$mime_boundary}\n";

      foreach ($files as $f) {

        $file = $_FILES[$f]['tmp_name'];

        $filename = $_FILES[$f]['name'];;

        $data = file_get_contents($file);

        $data = chunk_split(base64_encode($data));

        $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$file\"\n" .
          "Content-Disposition: attachment;\n" . " filename=\"$file\"\n" .
          "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
        $message .= "--{$mime_boundary}\n";
      }
        echo (@mail($to, $subject, $message, $headers)) ? "<p>Messaggio spedito correttamente a $to!</p>" : "<p>ERRORE! Messaggio non spedito a $to!</p>";
  } // mail-attachment ?>

Instead of passing file uploaded paths, I passing HTML elements to the function, so that it will fetch necessary Information and attach file(s) and it will send mail.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM