首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在PHP邮件功能中使用TCPDF

如何在PHP邮件功能中使用TCPDF
EN

Stack Overflow用户
提问于 2012-07-20 02:20:15
回答 2查看 12.2K关注 0票数 4
代码语言:javascript
复制
$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的输出行开始。

代码语言:javascript
复制
$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();
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-07-20 02:27:01

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

代码语言:javascript
复制
$pdfString = $pdf->Output('dummy.pdf', 'S');

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

代码语言:javascript
复制
$mailer->AddStringAttachment($pdfString, 'some_filename.pdf');
票数 13
EN

Stack Overflow用户

发布于 2014-07-25 07:03:37

我尝试了几种选择。唯一的办法是,当我保存PDF到一个文件夹,然后通过电子邮件。

代码语言:javascript
复制
$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';
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11571977

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档