通过PHP发送的PDF附件并非始终有效

通过PHP发送的PDF附件并非始终有效,php,pdf,encoding,base64,attachment,Php,Pdf,Encoding,Base64,Attachment,我制作了一个带有验证md5散列的简单电子邮件注册系统。当验证链接被跟踪并且帐户变为“活动”时,我将尝试向用户发送pdf。这有时是有效的,而不是对其他人。我正在使用以下代码: $subject = 'Your free PDF'; $random_hash = md5(date('r', time())); $headers = "From: info@mysite.com\r\nReply-To: info@mysite.com"; $headers .= "\r\nContent-Typ

我制作了一个带有验证md5散列的简单电子邮件注册系统。当验证链接被跟踪并且帐户变为“活动”时,我将尝试向用户发送pdf。这有时是有效的,而不是对其他人。我正在使用以下代码:

$subject = 'Your free PDF'; 
$random_hash = md5(date('r', time())); 
$headers = "From: info@mysite.com\r\nReply-To: info@mysite.com"; 
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\""; 
$attachment = chunk_split(base64_encode(file_get_contents('../../assets/Free PDF.pdf'))); 
ob_start(); //Turn on output buffering 
?> 
--PHP-mixed-<?php echo $random_hash; ?>  
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>" 

--PHP-alt-<?php echo $random_hash; ?>  
Content-Type: text/plain; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit

Hi,

Please find attached your free PDF.

~ The Team

--PHP-alt-<?php echo $random_hash; ?>  
Content-Type: text/html; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit

Hi,<br>
<br>
Please find attached your free <strong>PDF</strong>.
<br>
~ The Team

--PHP-alt-<?php echo $random_hash; ?>-- 

--PHP-mixed-<?php echo $random_hash; ?>  
Content-Type: application/pdf; name="Free PDF.pdf"  
Content-Transfer-Encoding: base64  
Content-Disposition: attachment  

<?php echo $attachment; ?> 
--PHP-mixed-<?php echo $random_hash; ?>-- 

<?php 
//copy current buffer contents into $message variable and delete current output buffer 
$message = ob_get_clean(); 
//send the email 
$mail_sent = @mail( $email, $subject, $message, $headers ); 
$subject='您的免费PDF';
$random_hash=md5(日期('r',时间());
$headers=“From:info@mysite.com\r\n适用于:info@mysite.com"; 
$headers.=“\r\n内容类型:多部分/混合;边界=\”PHP混合-“$random\u散列。”\”;
$attachment=chunk_split(base64_encode(file_get_contents('../../assets/Free PDF.PDF'));
ob_start()//打开输出缓冲
?> 
--PHP混合-
内容类型:多部分/备选;boundary=“PHP alt-”
--PHP alt-
内容类型:文本/纯文本;charset=“iso-8859-1”
内容传输编码:7bit
你好
请查看附件中的免费PDF。
~z~团队
--PHP alt-
内容类型:text/html;charset=“iso-8859-1”
内容传输编码:7bit
嗨,

请查看附件中的免费PDF
~z~团队 --PHP alt-- --PHP混合- 内容类型:application/pdf ;;name=“免费PDF.PDF” 内容传输编码:base64 内容配置:附件 --PHP混合--
这很简单:不要构建自己的mime电子邮件。Swiftmailer和PHPMailer都可以在几行代码中为您完成所有这些,而且做得更好。