2010-06-01 39 views
3

我想发送一个PDF文件在PHP邮件。我知道文件的名称和位置(打印在cup-pdf中的发票),并且希望在我点击我网站上的按钮时自动以php的形式发送该文件。如何才能做到这一点?谢谢在php邮件中自动附加文件

回答

2

您应该使用为此构建的类之一,例如PEAR Mail。 有关更多解释,假设我们只使用PDF文件的纯文本UTF8内容。

  • uniqboundary应该是通过某种唯一的字符串
  • $fileType的更换是MIME类型文件

下面的代码:

$headers = 'MIME-Version: 1.0'."\r\n" 
    .'Content-Type: multipart/related; boundary="--uniqboundary"'."\r\n"; 

$body = '--uniqboundary."\r\n". 
    .'Content-Type: text/plain; charset=utf-8'."\r\n" 
    .'Content-Transfer-Encoding: 8bit'."\r\n\r\n" 
    .$text 
    .'--uniqboundary'."\r\n" 
    .'Content-Type: '.$fileType.'; name="'.basename($filename).'"'."\r\n" 
    .'Content-Transfer-Encoding: base64'."\r\n" 
    .'Content-Disposition: attachment; filename="'.basename($filename).'"'."\r\n\r\n"; 

$lineSize = filesize($filename) + 1; 
$f = fopen($filename, 'r'); 
$chunks[] = chunk_split(base64_encode(fread($f, $lineSize))); 
fclose($f); 

$body .= implode("\r\n", $chunks) 
    .'--uniqboundary--'."\r\n"; 

mail($to, $subject, $body, $headers); 

它应该工作。

0

phpmailer是一个很好的选择,但需要一个smtp帐户,使其