PHP 邮件附件未打开或文件出错


PHP Mail attachment not opening or error file

似乎

这段代码有效,但是当我打开邮件上的附件时,它无法打开或出错。除了附件外,一切都很好。有人可以帮忙吗?谢谢。

<?php
if (isset($_POST["send"]))  {  
  $subject = "Applicant";
  $name = $_POST['contact_name'];
  $position = $_POST['position'];
  $tmpName = $_FILES['attachment']['tmp_name']; 
  $fileType = $_FILES['attachment']['type']; 
  $fileName = $_FILES['attachment']['name'];
  # Open a file
  $file = fopen($tmpName, "r" );
  if( $file == false )
  {
     echo "Error in opening file";
     exit();
  }
  # Read the file into a variable
  $size = $_FILES['attachment']['size'];
  $content = fread( $file, $size);
  # encode the data for safe transit
  # and insert 'r'n after every 76 chars.
  $encoded_content = chunk_split(base64_encode($content));
  # Get a random 32 bit number using time() as seed.
  $num = md5( time() );
  # Define the main headers.
  $header = "From: Applicant'r'n";
  $header .= "MIME-Version: 1.0'r'n";
  $header .= "Content-Type: multipart/mixed; ";
  $header .= "boundary=$num'r'n";
  $header .= "--$num'r'n";
  # Define the position section
  $header .= "Content-Type: text/plain'r'n";
  $header .= "Content-Transfer-Encoding:8bit'r'n'n";
  $header .= "$name'r'n";
  $header .= "$position'r'n";
  $header .= "--$num'r'n";
  # Define the attachment section
  $header .= "Content-Type:  multipart/mixed; ";
  $header .= "name='"$fileName'"'r'n";
  $header .= "Content-Transfer-Encoding:base64'r'n";
  $header .= "Content-Disposition:attachment; ";
  $header .= "filename='"$fileName'"'r'n'n";
  $header .= "$encoded_content'r'n";
  $header .= "--$num--";
  $message = "Name: ".$name."'r'n"."Position: ".$position;
  # Send email now
  $retval = mail ('sample@gmail.com', $subject, $message, $header);
  if( $retval == true ){
   echo "<div style='border-style: solid; border: thick double #00386c; margin-bottom: 10px;'><h1 style='text-align: center; color: #00386c; padding: 10px;'>Your resume was sent! Thank you.</h1></div>";
    }else{
      echo "error";exit;
    }
}
?>              

电子邮件正文中缺少附件部分。试试这个:

if (isset($_POST["send"])) {  
    $subject = "Applicant";
    $name = $_POST['contact_name'];
    $position = $_POST['position'];
    $tmpName = $_FILES['attachment']['tmp_name']; 
    $fileType = $_FILES['attachment']['type']; 
    $fileName = $_FILES['attachment']['name'];
    $message = "Name: ".$name."'r'n"."Position: ".$position;
    # Open a file
    # encode the data for safe transit
    # and insert 'r'n after every 76 chars.
    $encoded_content = chunk_split(base64_encode(file_get_contents($tmpName)));
    # Get a random 32 bit number using time() as seed.
    $random_hash = md5(date('r', time())); 
    # Define the main headers.
    $headers = "From: no-reply@example.com'r'n";
    $headers .= "Reply-To: no-reply@example.com'r'n";
    $headers .= "MIME-Version: 1.0'r'n";
    $headers .= "Content-Type: multipart/mixed; boundary='"PHP-mixed-".$random_hash."'"'r'n"; 
    $headers .= "X-Priority: 3'r'n";
    $headers .= "X-MSMail-Priority: Normal'r'n";    
    $headers .= "X-Mailer: PHP/" . phpversion();
    # Plain text section
    $body = "--PHP-mixed-" . $random_hash . "'r'n";
    $body .= "Content-Type: text/plain; charset='"UTF-8'"'r'n";
    $body .= "Content-Transfer-Encoding: 8bit'r'n'r'n";
    $body .= $message;
    $body .= "'r'n'r'n";
    $body .= "'r'n--PHP-mixed-" . $random_hash . "'r'n'r'n";

    # Define the attachment section
    $body .= "--PHP-mixed-" . $random_hash . "'r'n";
    $body .= "Content-Type: " . $fileType . "; name='"" . $fileName . "'"'r'n";
    $body .= "Content-Transfer-Encoding: base64'r'n";
    $body .= "Content-Disposition: attachment; filename='"" . $fileName . "'"'r'n'r'n";
    $body .= $encoded_content;
    $body .= "'r'n--PHP-mixed-" . $random_hash . "'r'n'r'n";

    # Send email now
    $retval = mail('sample@gmail.com', $subject, $body, $headers);
    if( $retval == true ) {
        echo "<div style='border-style: solid; border: thick double #00386c; margin-bottom: 10px;'><h1 style='text-align: center; color: #00386c; padding: 10px;'>Your resume was sent! Thank you.</h1></div>";
    } else{
        echo "error";exit;
    }
}