【发布时间】:2015-11-18 03:17:48
【问题描述】:
我正在尝试发送一封电子邮件,其中包含自动/动态生成的 PDF 作为 PHP 中的附件。我正在尝试使用下面的代码来实现这一点,但它不起作用。
<?php
require('fpdf.php');
class PDF extends FPDF
{
// Page header
function Header()
{
// Logo
//$this->Image('logo.jpg',10,6,30);
// Arial bold 15
$this->SetFont('Arial','B',15);
// Move to the right
$this->Cell(80);
// Title
$this->Cell(60,10,'Convert HTML TO PDF',1,0,'C');
// Line break
$this->Ln(20);
}
// Page footer
function Footer()
{
// Position at 1.5 cm from bottom
$this->SetY(-15);
// Arial italic 8
$this->SetFont('Arial','I',8);
// Page number
$this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
}
}
// Instanciation of inherited class
$pdf = new PDF();
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Times','',12);
$pdf->Cell(0,10,'Name : ',0,1);
$pdf->Cell(0,10,'Email : ',0,1);
$pdf->Cell(0,10,'Mobile : ',0,1);
$pdf->Cell(0,10,'Comment : ',0,1);
$pdf->Output("filename.pdf","F");
$pdfdoc = $pdf->Output("", "S");
//$attachment = chunk_split(base64_encode($pdfdoc));
$email_to = "xxx@gmail.com"; // The email you are sending to (example)
$email_from = "xxx@outlook.com"; // The email you are sending from (example)
$email_subject = "subject line"; // The Subject of the email
$email_txt = "text body of message"; // Message that the email has in it
$fileatt_type = "application/pdf"; // File Type
$fileatt_name = "filename.pdf"; // Filename that will be used for the file as the attachment
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers = "From: NameHere"; // Who the email is from (example)
$headers .= "\nMIME-Version: 1.0\n" ."Content-Type: multipart/mixed;\n" ." boundary=\"{$mime_boundary}\"";
$email_message.= "This is a multi-part message in MIME format.\n\n" ."--{$mime_boundary}\n" ."Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" . $email_txt;
$email_message .= "\n\n";
$data = chunk_split(base64_encode($pdfdoc));
$email_message .= "--{$mime_boundary}\n" ."Content-Type: {$fileatt_type};\n" ." name=\"{$fileatt_name}\"\n" ."Content-Transfer-Encoding: base64\n\n" .$data . "\n\n" . "--{$mime_boundary}--\n";
;
if(mail($email_to,$email_subject,$email_message,$headers))
{
echo "File Sent Successfully.";
//unlink($attachment); // delete a file after attachment sent.
}
else
{
die("Sorry but the email could not be sent. Please go back and try again!");
echo "File Not Sent .";
}
}
?>
我的方法有什么错误吗?
【问题讨论】:
-
您是否注意到该示例代码中也存在语法错误。此外,在 die() 之后回显也没有任何好处。
-
您能告诉我们您保存/写入 PDF 文件的代码吗?首先要检查的是文件是否创建成功。如果我们可以确认文件写入正确,那么问题就变成了电子邮件是否附加成功。
标签: php email pdf attachment