2016-03-23 61 views
0

我有一個簡單的函數發送PDF文件時有一些麻煩 - 電子郵件發送但附件已損壞,當我嘗試打開它,所以顯然我的功能在做有問題。PHP如何發送PDF附件,而不會損壞

任何幫助將不勝感激。

function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) { 

$file = $path.$filename; 

$file_size = filesize($file); 

$handle = fopen($file, "r"); 

$content = fread($handle, $file_size); 

fclose($handle); 

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

$uid = md5(uniqid(time())); 

$header = "From: ".$from_name." <".$from_mail.">\r\n"; 

$header .= "Reply-To: ".$replyto."\r\n"; 

$header .= "MIME-Version: 1.0\r\n"; 

$header .= "This is a multi-part message in MIME format.\r\n"; 

$header .= "--".$uid."\r\n"; 

$header .= "Content-Type: application/octet-stream; name=\"attachment.pdf\"\r\n"; 

$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n"; 

$header .= $message."\r\n\r\n"; 

$header .= "--".$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 .= "--".$uid."--"; 






wp_mail($mailto, $subject, $message, $header); 


} 
+0

不建立你自己的啞劇電子郵件。使用一個適當的郵件包,如phpmailer和swiftmailer。這會將幾乎所有的代碼都減少到一個'$ mail-> AddAttachment(...)'類型的調用。因爲你已經用phpmailer標記了這個問題 - 你爲什麼不使用它? –

回答

1

您的電子郵件標題已關閉很多。一個適當的多部分消息會是這個樣子:

Content-Transfer-Encoding: binary 
Content-Type: multipart/mixed; boundary="_----------=_1458761739257530" 
MIME-Version: 1.0 
Date: xxxxx 
From: xxxxx 
To: xxxxx 
Reply-To: xxxx 
Subject: xxxx 

This is a multi-part message in MIME format. 

--_----------=_1458761739257530 
Content-Disposition: inline 
Content-Transfer-Encoding: 8bit 
Content-Type: text/plain 

This is the plain-text part of my message 
That's all. 

--_----------=_1458761739257530 
Content-Disposition: attachment; filename="myfile.pdf" 
Content-Transfer-Encoding: base64 
Content-Type: application/octet-stream; name="myfile.pdf" 

<base64 content here> 

--_----------=_1458761739257530-- 

但是,像一個真正的,你不應該產生標題你自己 - 有很多方法來搞砸了。相反,請使用任何數量的現有PHP庫,這些庫將生成MIME頭文件,進行編碼併爲您處理郵件。

例如,你的發行版很可能包括PEAR模塊「郵件」和「Mail_Mime」,否則你可以用pear install Mail Mail_Mime

然後做這樣的事情很容易安裝:

function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) { 

    require_once('Mail.php'); 
    require_once('Mail/mime.php'); 

    $file = $path.$filename; 

    $headers = array (
     'From'  => $from_mail, 
     'To'  => $mailto, 
     'Reply-To' => $replyto, 
     'Subject' => $subject, 
    ); 

    $mime = new Mail_mime(array('eol' => "\n")); 

    $mime->SetTXTBody($message); 
    $mime->addAttachment($file, 'application/octet-stream'); 

    $mime_body = $mime->get(); 
    $mime_headers = $mime->headers($headers); 

    $mail =& Mail::factory('mail'); 
    $mail->send($mailto, $mime_headers, $mime_body); 

    if (PEAR::isError($mail)) { 
     echo("<p>ERROR:" . $mail->getMessage() . "</p>\n"); 
    } else { 
     echo("<p>Message successfully sent!</p>\n"); 
    } 
}