簡體   English   中英

如何使用TCPDF與PHP郵件功能

[英]How to use TCPDF with PHP mail function

$to = 'my@email.ca';
$subject = 'Receipt';
$repEmail = 'rep@sales.ca';

$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文件發送到我的電子郵件帳戶。

電子郵件實際上是使用此編碼並附加PDF。 問題是它只是粗糙的100字節大小的空白PDF。 這當然不是有效的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('reply@to.ca', 'Reply To');
$mailer->SetFrom('sent@from.ca', 'Sent From');
$mailer->AddReplyTo('reply@to.ca', 'Reply To');
$mailer->AddAddress('send@to.ca', '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();
}

你有兩個選擇。 您可以將PDF保存到文件並附加文件,或者將其作為字符串輸出。 我發現字符串輸出更可取:

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

文件名被忽略,因為它只返回編碼的字符串。 現在,您可以在電子郵件中包含該字符串。 在使用像這樣的附件時,我更喜歡使用PHPMailer。 使用PHPMailer的AddStringAttachment方法來完成此任務:

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

我嘗試了幾種替代品。 唯一有效的方法是將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@yahoo.com");
$mail->AddReplyTo("email@gmail.com", "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';

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM