PHP - 邮件不会发送附件,只有没有附件

Mine script won't send attachment (this is for more people, but the single give the same problem).

There is the code:

    if ($_POST) {

$to = mysql_real_escape_string($_POST['to']);
$subject = mysql_real_escape_string($_POST['subject']);
$message = mysql_real_escape_string($_POST['message']);


if (empty($to) || empty($subject) || empty($message) ) {

    header("location: nation.php?error=empty");
} else {

    $email = $b['email'];

    $my_file = $_FILES['my_file']['tmp_name'];
    $my_file_type = $_FILES['my_file']['type'];
    $my_file_name = $_FILES['my_file']['name'];
    $body = "";
    //upload
if (is_uploaded_file($my_file))
{
    $headers = "MIME-Version: 1.0" . "
";
    $headers .= 'From: <'.$email.'>' . "
";
    $headers .= "Content-Type: multipart/mixed;
";
 // Apro e leggo il file my_file
  $file = fopen($my_file,'rb');
  $data = fread($file, filesize($my_file));
  fclose($file);

  // Adatto il file al formato MIME base64 usando base64_encode
  $data = chunk_split(base64_encode($data));

  // Genero il "separatore"
  // Serve per dividere, appunto, le varie parti del messaggio.
  // Nel nostro caso separerà la parte testuale dall'my_file
  $semi_rand = md5(time());
  $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

  // Aggiungo le intestazioni necessarie per l'my_file
  $headers .= "Content-Type: multipart/mixed;
";
  $headers .= " boundary=\"{$mime_boundary}\"";

  // Definisco il tipo di messaggio (MIME/multi-part)
  $body .= "This is a multi-part message in MIME format.

";

  // Metto il separatore
  $body .= "--{$mime_boundary}
";

  // Questa è la parte "testuale" del messaggio
  $body .= "Content-Type: text/plain; charset=\"iso-8859-1\"
";
  $body .= "Content-Transfer-Encoding: 7bit

";
  $body .= $message . "

";

  // Metto il separatore
  $body .= "--{$mime_boundary}
";

  // Aggiungo l'my_file al messaggio
  $body .= "Content-Disposition: attachment; filename=\"{$my_file_name}\"
";
  $body .= "Content-Transfer-Encoding: base64

";
  $body .= $data . "

";

  // chiudo con il separatore
  $body .= "--{$mime_boundary}--
";


    if ($privilegi == '1') {

    $nap = mysql_query("SELECT * FROM leads WHERE country ='$to' AND staff='$username'");
    while($abc = mysql_fetch_array($nap)) {
    $em = $abc['email'];
    $a = mail($em,$subject,$body,$headers); 
    }

    } else if ($privilegi == '2') {
    $nap = mysql_query("SELECT * FROM contacts WHERE country ='$to' AND staff='$username'");
    while($abc = mysql_fetch_array($nap)) {
    $em = $abc['email'];
    $a = mail($em,$subject,$body,$headers); 
    }

    } else if ($privilegi == '3') {

    $nap = mysql_query("SELECT * FROM agent WHERE country ='$to' AND staff='$username'");
    while($abc = mysql_fetch_array($nap)) {
    $em = $abc['email'];
    $a = mail($em,$subject,$body,$headers); 
    }

    } else {

    $name = 'error';
    }



    } else {

    $headers = "MIME-Version: 1.0" . "
";
    $headers .= "Content-type:text/html;charset=UTF-8" . "
";


    if ($privilegi == '1') {

    $nap = mysql_query("SELECT * FROM leads WHERE country ='$to' AND staff='$username'");
    while($abc = mysql_fetch_array($nap)) {
    $em = $abc['email'];
    $a = mail($em,$subject,$message,$headers); 
    }

    } else if ($privilegi == '2') {
    $nap = mysql_query("SELECT * FROM contacts WHERE country ='$to' AND staff='$username'");
    while($abc = mysql_fetch_array($nap)) {
    $em = $abc['email'];
    $a = mail($em,$subject,$message,$headers); 
    }

    } else if ($privilegi == '3') {

    $nap = mysql_query("SELECT * FROM agent WHERE country ='$to' AND staff='$username'");
    while($abc = mysql_fetch_array($nap)) {
    $em = $abc['email'];
    $a = mail($em,$subject,$message,$headers); 
    }

    } else {

    $name = 'error';
    }

Worked for 5/6 minutes... Sorry for the bad code, i'am not an expert :\ It send email without attachment, i'm so confused, where is the problem?

If you want to send mail with attachments you might want to take a look on some external libraries like PHPMailer for example. There you can add an attachment simply $mail->addAtachment('path/To/File.extension').

When sending email with mail() function there are big chance that mail will end up in spam folder. With PHPmailer you can use SMTP authentication and trusted SMTP server and this will reduce chances that your mail will be considered as spam.