2014-10-07 92 views
1

我使用PHP mail()功能與附件使用Content-Type: multipart/mixed頭,但它始終返回noname.txtPHP的mail()帶附件的回報noname.txt

這裏是我的代碼:

$upload_name=$_FILES["upload"]["name"]; 
$upload_type=$_FILES["upload"]["type"]; 
$upload_size=$_FILES["upload"]["size"]; 
$upload_temp=$_FILES["upload"]["tmp_name"]; 
$subject = "Subject"; 
$to="[email protected]"; 
$message="message"; 

    $fp = fopen($upload_temp, "rb"); 
$file = fread($fp, $upload_size); 

$file = chunk_split(base64_encode($file)); 
$num = md5(time()); 

    //Normal headers 

$headers = "From: Info Mail<[email protected]>\r\n"; 
    $headers .= "MIME-Version: 1.0\r\n"; 
    $headers .= "Content-Type: multipart/mixed; "; 
    $headers .= "boundary=".$num."\r\n"; 
    $headers .= "--$num\r\n"; 

    // This two steps to help avoid spam 

$headers .= "Message-ID: <".gettimeofday()." [email protected]".$_SERVER['SERVER_NAME'].">\r\n"; 
$headers .= "X-Mailer: PHP v".phpversion()."\r\n"; 

    // With message 

$headers .= "Content-Type: text/html; charset=iso-8859-1\r\n"; 
    $headers .= "Content-Transfer-Encoding: 8bit\r\n"; 
    $headers .= "".$message."\n"; 
    $headers .= "--".$num."\n"; 

    // Attachment headers 

$headers .= "Content-Type:".$upload_type." "; 
    $headers .= "name=\"".$upload_name."\"r\n"; 
    $headers .= "Content-Transfer-Encoding: base64\r\n"; 
    $headers .= "Content-Disposition: attachment; "; 
    $headers .= "filename=\"".$upload_name."\"\r\n\n"; 
    $headers .= "".$file."\r\n"; 
    $headers .= "--".$num."--"; 
// SEND MAIL 
    [email protected]($to, $subject, $message, $headers); 
fclose($fp); 

In Mail收件箱它返回:

enter image description here

在此先感謝...

+0

我想你是在你的MIME類型的文件,你要在這一行上傳文件的問題$ headers。=「Content-Type:multipart/mixed;」; – Ricky 2014-10-07 11:32:07

+1

@Ricky然後,我可以用什麼來代替_Content-Type:multipart/mixed_? – edCoder 2014-10-07 11:37:13

+0

我建議使用像SwiftMailer這樣的電子郵件庫。然後發送帶有附件和不同編碼的電子郵件真的很容易。 – TiMESPLiNTER 2014-10-07 11:46:58

回答

0
$upload_name=$_FILES["your_file_input_name"]["name"]; 
$upload_type=$_FILES["your_file_input_name"]["type"]; 
$upload_size=$_FILES["your_file_input_name"]["size"]; 
$upload_temp=$_FILES["your_file_input_name"]["tmp_name"]; 

$subject = "Your Subject"; 
$to="[email protected]"; 
$message="Your Message"; 

$fp = fopen($upload_temp, "rb"); 
$file = fread($fp, $upload_size); 

$file = chunk_split(base64_encode($file)); 
$num = md5(time()); 

//Normal headers 

$headers = "From: Info Mail<[email protected]>\r\n"; 
$headers .= "MIME-Version: 1.0\r\n"; 
$headers .= "Content-Type: multipart/mixed; "; 
$headers .= "boundary=".$num."\r\n"; 
$headers .= "--$num\r\n"; 

// This two steps to help avoid spam 

$headers .= "Message-ID: <".gettimeofday()." [email protected]".$_SERVER['SERVER_NAME'].">\r\n"; 
$headers .= "X-Mailer: PHP v".phpversion()."\r\n"; 

// With message 

$headers .= "Content-Type: text/html; charset=iso-8859-1\r\n"; 
$headers .= "Content-Transfer-Encoding: 8bit\r\n"; 
$headers .= "".$message."\n"; 
$headers .= "--".$num."\n"; 

// Attachment headers 

$headers .= "Content-Type: application/".$upload_type." "; 
$headers .= "name=\"".$upload_name."\"r\n"; 
$headers .= "Content-Transfer-Encoding: base64\r\n"; 
$headers .= "Content-Disposition: attachment; "; 
$headers .= "filename=\"".$upload_name."\"\r\n\n"; 
$headers .= "".$file."\r\n"; 
$headers .= "--".$num."--"; 

// SEND MAIL 
[email protected]($to, $subject, $message, $headers); 
fclose($fp); 

if($flgchk) 
{ 
    echo 'Sent Successfully'; 
} 
else 
{ 
    echo 'Unexpected error occurred!!! Please try again.'; 
} 
0
$header = "From: Info Mail<[email protected]>\r\n"; 
      $header .= "MIME-Version: 1.0\r\n"; 
      $header .= "Content-Type: multipart/mixed; boundary=\"".$num."\"\r\n\r\n"; 
      $header .= "This is a multi-part message in MIME format.\r\n"; 
      $header .= "--".$num."\r\n"; 
      $header .= "Content-type:text/html; charset=UTF-8\r\n"; 
      $header .= $message."\r\n\r\n"; 
      $header .= "--".$num."\r\n"; 
      $header .= "Content-Type: application/octet-stream; name=\"".$upload_name."\"\r\n"; // use different content types here 
      $header .= "Content-Transfer-Encoding: base64\r\n"; 
      $header .= "Content-Disposition: attachment; filename=\"".$fname."\"\r\n\r\n"; 
      $header .= $file."\r\n\r\n"; 
      $header .= "--".$num."--"; 

我用上面的標頭這似乎工作郵件。

+1

謝謝,但不工作 – edCoder 2014-10-08 03:35:02

0

您的代碼是完美的

在此處更改代碼。

在附件頭

更換

$headers .= "Content-Type:".$upload_type." "; 

$headers .= "Content-Type: multipart/mixed; "; 
+1

謝謝,但再次檢查_noname.txt_ – edCoder 2014-10-08 03:32:31

+0

檢查一次'$ upload_name'是否有值。 – Gowri 2014-10-08 03:45:01

0

刪除最後一個邊界,只有最後一個附件串完成郵件,這消除在Gmail中noname.txt 。