【问题标题】:How to use TCPDF with PHP mail function如何将 TCPDF 与 PHP 邮件功能一起使用
【发布时间】:2012-07-19 07:04:51
【问题描述】:
$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();
}

【问题讨论】:

    标签: php email pdf phpmailer tcpdf


    【解决方案1】:

    你有两个选择。您可以将 PDF 保存到文件并附加文件,或者将其输出为字符串。我发现字符串输出更可取:

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

    文件名被忽略,因为它只返回编码字符串。现在您可以在电子邮件中包含该字符串。在处理此类附件时,我更喜欢使用 PHPMailer。使用 PHPMailer 的 AddStringAttachment 方法来完成:

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

    【讨论】:

    • 是的,看来我必须使用外部邮件库。感谢您的提示。
    • 我也想避免使用外部邮件库,但这是完美的解决方案
    • 大卫向你脱帽致敬
    【解决方案2】:

    我尝试了几种选择。唯一可行的方法是将 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';
    

    【讨论】:

    • 你的服务器有能力使用mail()函数吗?如果没有,您将无法从TCPDF发送邮件
    猜你喜欢
    • 2013-03-17
    • 2011-01-22
    • 1970-01-01
    • 2017-01-16
    • 2020-10-01
    • 2014-05-19
    • 2011-04-07
    • 2012-07-12
    相关资源
    最近更新 更多