2012-07-20 61 views
4
$to = '[email protected]'; 
$subject = 'Receipt'; 
$repEmail = '[email protected]'; 

$fileName = 'receipt.pdf'; 
$fileatt = $pdf->Output($fileName, 'E'); 
$attachment = chunk_split($fileatt); 
$eol = PHP_EOL; 
$separator = md5(time()); 

$headers = 'From: Sender <'.$repEmail.'>'.$eol; 
$headers .= 'MIME-Version: 1.0' .$eol; 
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\""; 

$message = "--".$separator.$eol; 
$message .= "Content-Transfer-Encoding: 7bit".$eol.$eol; 
$message .= "This is a MIME encoded message.".$eol; 

$message .= "--".$separator.$eol; 
$message .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol; 
$message .= "Content-Transfer-Encoding: 8bit".$eol.$eol; 

$message .= "--".$separator.$eol; 
$message .= "Content-Type: application/pdf; name=\"".$fileName."\"".$eol; 
$message .= "Content-Transfer-Encoding: base64".$eol; 
$message .= "Content-Disposition: attachment".$eol.$eol; 
$message .= $attachment.$eol; 
$message .= "--".$separator."--"; 

if (mail($to, $subject, $message, $headers)){ 
$action = 'action=Receipt%20Sent'; 
header('Location: ../index.php?'.$action); 
} 

else { 
$action = 'action=Send%20Failed'; 
header('Location: ../index.php?'.$action); 
} 

我一直在使用TCPDF很短的時間來從窗體生成PDF文件。它工作得很好,PHP的一部分沒有改變。現在我想將這些PDF文件發送到我的電子郵件帳戶。如何使用帶有PHP郵件功能的TCPDF

電子郵件實際上是使用這種編碼並附加PDF。問題在於,它只是一個空白的PDF,大小爲100個字節。這當然不是一個有效的PDF,也不會與表單的回覆有任何關係。

我真的不熟悉在PHP中附加文件到電子郵件和任何幫助解決這個問題將不勝感激。

更新

由於好像幾個人都在看這還是我將張貼我目前的解決方案。它涉及下載PHPMailer如下所示。我已經開始在TCPDF的輸出行。

$attachment = $makepdf->Output('filename.pdf', 'S'); 
SENDmail($attachment); 

function SENDmail($pdf) { 
require_once('phpmailer/class.phpmailer.php'); 
$mailer = new PHPMailer(); 

$mailer->AddReplyTo('[email protected]', 'Reply To'); 
$mailer->SetFrom('[email protected]', 'Sent From'); 
$mailer->AddReplyTo('[email protected]', 'Reply To'); 
$mailer->AddAddress('[email protected]', 'Send To'); 
$mailer->Subject = 'Message with PDF'; 
$mailer->AltBody = "To view the message, please use an HTML compatible email viewer"; 
$mailer->MsgHTML('<p>Message contents</p>')); 
if ($pdf) {$mailer->AddStringAttachment($pdf, 'filename.pdf');} 

$mailer->Send(); 
} 
+0

extra)on line 14 – dricket 2016-02-05 06:26:26

回答

13

您有兩種選擇。您可以將PDF保存到文件並附加文件,或者將其輸出爲字符串。我發現字符串輸出是可取的:

$pdfString = $pdf->Output('dummy.pdf', 'S'); 

因爲它只是返回編碼的字符串,文件名被忽略。現在你可以在你的電子郵件中包含字符串。處理像這樣的附件時,我更喜歡使用PHPMailer。使用PHPMailer的AddStringAttachment方法來實現這一點:

$mailer->AddStringAttachment($pdfString, 'some_filename.pdf'); 
+0

是的,看起來我必須使用外部郵件庫。謝謝你的提示。 – foochow 2012-07-20 09:48:15

+0

我也想避免使用外部郵件庫,但它是完美的解決方案 – Iannazzi 2014-10-22 16:57:41

+0

我把我的帽子交給你davidethell – anexo 2017-01-04 17:14:17

0

我嘗試了幾種方法。只有這樣,我將PDF保存到一個文件夾然後通過電子郵件發送。

$pdf->Output("folder/filename.pdf", "F"); //save the pdf to a folder 
    require_once('phpmailer/class.phpmailer.php'); //where your phpmailer folder is 
$mail = new PHPMailer();      
$mail->From = "email.com"; 
$mail->FromName = "Your name"; 
$mail->AddAddress("[email protected]"); 
$mail->AddReplyTo("[email protected]", "Your name");    
$mail->AddAttachment("folder/filename.pdf");  // attach pdf that was saved in a folder 
$mail->Subject = "Email Subject";     
$mail->Body = "Email Body"; 
if(!$mail->Send()) 
{ 
     echo "Message could not be sent. <p>"; 
     echo "Mailer Error: " . $mail->ErrorInfo; 
} 
else 
{ 
     echo "Message sent"; 
} 
echo 'sent email and attachment'; 
+0

你的服務器是否有能力使用mail()函數?如果沒有,你將無法從'TCPDF'發送郵件 – foochow 2014-07-28 21:39:00