foo*_*how 4 php pdf email phpmailer tcpdf
$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);
}
Run Code Online (Sandbox Code Playgroud)
我一直在使用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();
}
Run Code Online (Sandbox Code Playgroud)
dav*_*ell 13
你有两个选择.您可以将PDF保存到文件并附加文件,或者将其作为字符串输出.我发现字符串输出更可取:
$pdfString = $pdf->Output('dummy.pdf', 'S');
Run Code Online (Sandbox Code Playgroud)
文件名被忽略,因为它只返回编码的字符串.现在,您可以在电子邮件中包含该字符串.在使用像这样的附件时,我更喜欢使用PHPMailer.使用PHPMailer的AddStringAttachment方法来完成此任务:
$mailer->AddStringAttachment($pdfString, 'some_filename.pdf');
Run Code Online (Sandbox Code Playgroud)