2015-06-24 62 views
0

我正在使用下面的腳本來發送帶有郵件的附件。它發送郵件,但附件是空的。我已經閱讀了腳本,但似乎沒有找到錯誤來自哪裏。帶有郵件附件的空文件 - PHP

PHP的:

$file_name = $_FILES['attachment']['name']; 
    $temp_name = $_FILES['attachment']['tmp_name']; 
    $file_type = $_FILES['attachment']['type']; 

    // get file extension 

    $base = basename($file_name); 
    $extension = substr($base, strlen($base)-4, strlen($base)); 

    // allowed file types 

    $allowed_extensions = array(".doc", ".docx", ".pdf"); 

    $from = $_POST['email']; 
    $to = "[email protected]"; 
    $subject = "CV Upload"; 
    $message = "My CV"; 

    $file_name = $temp_name; 
    $content = chunk_split(base64_encode(file_get_contents($file))); 
    $uid = md5(uniqid(time())); 

    $header = "From: ".$from."\r\n"; 
    $header .= "Reply-To: ".$replyto."\r\n"; 
    $header .= "MIME-Version: 1.0\r\n"; 

    $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n"; 
    $header .= "This is a multi-part message in MIME format.\r\n"; 

    $header .= "--".$uid."\r\n"; 
    $header .= "Content-type:text/plain; charset-iso-8859-1\r\n"; 
    $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n"; 
    $header .= $message."\r\n\r\n"; 

    $header .= "--".$uid."\r\n"; 
    $header .= "Content-type: ".$file_type."; name=\"".$file_name."\"\r\n"; 
    $header .= "Content-Transfer-Encoding: base64\r\n"; 
    $header .= "Content-Disposition: attachment; filename=\"".$file_name."\"\r\n\r\n"; 
    $header .= $content."\r\n\r\n"; 
+0

不建立自己的,開始使用的PHPMailer或swiftmailer –

+0

由於目前尚未確定沒有'$ file'您使用它在你的代碼。 '$ content = chunk_split(base64_encode(file_get_contents($ file)));' –

回答

2

檢查你的代碼

$content = chunk_split(base64_encode(file_get_contents($file))); 

Technicaly,$file不存在。使用$file_name

$content = chunk_split(base64_encode(file_get_contents($file_name))); 
+0

我將.docx指定爲允許的文件類型之一,但它不允許。 – uikrosoft