2016-02-20 58 views
1

我有這個問題的PHP郵件功能: 我想發送一個HTML電子郵件附件 PDF文件,該文件存儲在mywebsite文件夾中,並從我創建與mpdf,但是當我發送它收到的郵件有一個附件大小0bPHP的HTML郵件附件清空

這是代碼:

<? 
$attachment = "path_to_file_pdf.file.pdf"; 
if(file_exists($attachment)){ 
    // File Exists 
    $size = filesize($attachment); 
    if($size > 0){ 
     //Alternative 1 
      $file = fopen($attachment,'rb'); 
      $content = fread($file, $size); 
      fclose($file); 
      $content = chunk_split(base64_encode($content)); 
     //Alternative 1 
      //$content = chunk_split(base64_encode(file_get_contents($attachment))); 
     $mailto = "[email protected]"; 
     $from_name = "MyDomainName"; 
     $from_mail = "[email protected]"; 
     $replyto = "[email protected]"; 
     $uid = md5(uniqid(time())); 
     $subject = "e-mail subject here"; 
     $message = "HTML MESSAGE HERE" ; 
     $filename = "file.pdf"; 
     $header = "From: ".$from_name." <".$from_mail.">\r\n"; 
     $header .= "Reply-To: ".$replyto."\r\n"; 
     $header .= "Content-Type: multipart/mixed; boundary=\"PHP-alt-".$uid."\"\r\n\r\n"; 
     $header .= "This is a multi-part message in MIME format.\r\n"; 
     $header .= "--PHP-alt-".$uid."\r\n"; 
     $header .= "Content-Type: text/html; charset=UTF-8\r\n"; 
     $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n"; 
     $header .= $message."\r\n\r\n"; 
     $header .= "--PHP-alt-".$uid."\r\n"; 
     $header .= "Content-Type: application/pdf; name=\"".$filename."\"\r\n"; 
     $header .= "Content-Transfer-Encoding: base64\r\n"; 
     $header .= "Content-Disposition: attachment; \r\n\r\n"; 
     $header .= $content."\r\n\r\n"; 
     $header .= "--PHP-alt-".$uid."--"; 
     if(@mail($mailto, $subject, "", $header)){ 
      echo "Mail SENT"; 
     }else{ 
      echo "ERROR - Mail error"; 
     } 
    }else{ 
     echo "ERROR- File size = 0"; 
    } 
}else{ 
    echo "ERROR - File doesn't exist"; 
} 
?> 

郵件被正確傳送,所以文件都存在,它的尺寸比0B更大。 但是,當我收到我的電子郵箱中的電子郵件時,它是完全正確的,而不是存在的附件,但它是空的。 我嘗試了兩種提取插入代碼中的文件內容的替代方法,但結果是一樣的。 有人可以幫助我嗎?

回答

0

我改變了一些標題配置。試試這個:

<? 
$attachment = "path_to_file_pdf.file.pdf"; 
if(file_exists($attachment)){ 
    // File Exists 
    $size = filesize($attachment); 
    if($size > 0){ 
     //Alternative 1 
      $file = fopen($attachment,'rb'); 
      $content = fread($file, $size); 
      fclose($file); 
      $content = chunk_split(base64_encode($content)); 
     //Alternative 1 
      //$content = chunk_split(base64_encode(file_get_contents($attachment))); 
     $mailto = "[email protected]"; 
     $from_name = "MyDomainName"; 
     $from_mail = "[email protected]"; 
     $replyto = "[email protected]"; 
     $uid = md5(uniqid(time())); 
     $subject = "e-mail subject here"; 
     $message = "HTML MESSAGE HERE" ; 
     $filename = "file.pdf"; 
     $header = "From: ".$from_name." <".$from_mail.">\r\n"; 
     $header .= "Reply-To: ".$replyto."\r\n"; 
     $header .= "Content-Type: multipart/mixed; boundary=\"PHP-alt-".$uid."\"\r\n\r\n"; 
     $header .= "This is a multi-part message in MIME format.\r\n"; 
     $header .= "--PHP-alt-".$uid."\r\n"; 
     $header .= "Content-Type: text/html; charset=UTF-8\r\n"; 
     $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n"; 
     $header .= $message."\r\n\r\n"; 
     $header .= "--PHP-alt-".$uid."\r\n"; 
     $header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; 
     $header .= "Content-Transfer-Encoding: base64\r\n"; 
     $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n"; 
     $header .= $content."\r\n\r\n"; 
     $header .= "--PHP-alt-".$uid."--"; 
     if(@mail($mailto, $subject, "", $header)){ 
      echo "Mail SENT"; 
     }else{ 
      echo "ERROR - Mail error"; 
     } 
    }else{ 
     echo "ERROR- File size = 0"; 
    } 
}else{ 
    echo "ERROR - File doesn't exist"; 
} 
?> 
+0

嗨,thanx的回覆,但它沒有解決問題.. – pablinho