如何使用TCPDF与PHP邮件功能 [英] How to use TCPDF with PHP mail function

查看:123
本文介绍了如何使用TCPDF与PHP邮件功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$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文件发送到我的电子邮件帐户。

I have been using TCPDF for a short amount of time now to generate PDF files from forms. It works quite well and that part of the PHP has not changed. Now I want to send those PDF files to my email account.

电子邮件正在使用此编码并附加PDF。问题是它只是一个大小为100字节的空白PDF。哪个当然不是一个有效的PDF,也没有与表单的回应有任何关系。

The emailing is actually working with this coding and attaching a PDF. The issue is that it is simply a blank PDF at rough 100 bytes in size. Which of course is not a valid PDF nor does it have anything to do with the responses from the form.

我真的不熟悉将文件附加到电子邮件在PHP中,任何帮助解决这个问题将不胜感激。

I am really not familiar with the attaching of files to an email in PHP and any help resolving this issue would be greatly appreciated.

更新

由于似乎有几个人正在看这个仍然我会发布我目前的解决方案。它涉及下载建议下载PHPMailer。我已经在TCPDF的输出行开始了。

Since it seems like several people are looking at this still I will post my current solution. It involves downloading PHPMailer as suggested below. I have started at the output line for 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保存到文件中,并附加该文件,否则将其作为字符串输出。我发现字符串输出是比较好的:

You have two choices. You can save the PDF to a file and attach the file or else output it as a string. I find the string output is preferable:

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

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

The file name is ignored since it just returns the encoded string. Now you can include the string in your email. I prefer to use PHPMailer when working with attachments like this. Use the AddStringAttachment method of PHPMailer to accomplish this:

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

这篇关于如何使用TCPDF与PHP邮件功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆