php邮件中的自动附加文件

php邮件中的自动附加文件,php,email,Php,Email,我想在php邮件中发送一个pdf文件。我知道文件的名称和位置(一张打印在cup pdf中的发票),并希望在我点击网站上的按钮时,用php自动发送此文件。如何做到这一点?谢谢例如,您应该使用为此而构建的类之一。 为了得到更多的解释,假设我们在PDF文件中只使用纯文本UTF8内容 uniqboundary应替换为某种唯一的字符串 $fileType是文件的MIME类型 代码如下: $headers = 'MIME-Version: 1.0'."\r\n" .'Content-Type:

我想在php邮件中发送一个pdf文件。我知道文件的名称和位置(一张打印在cup pdf中的发票),并希望在我点击网站上的按钮时,用php自动发送此文件。如何做到这一点?谢谢

例如,您应该使用为此而构建的类之一。 为了得到更多的解释,假设我们在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);

它应该可以工作。

phpmailer是一个很好的选择,但是需要一个smtp帐户来实现它