使用PHP发送PDF文件附件的电子邮件
好吧,这是我的第一个线程,我在网上搜索但没有运气。 我正在做一个实习,我正在开发一个项目,需要我创建一个网页,当用户提交他/她的信息时生成一个pdf文件。只要客户点击提交按钮,3件事情需要发生:使用PHP发送PDF文件附件的电子邮件
- 存储信息数据库(完成),
- 发送员工一封电子邮件,新客户的信息(已完成),以及
- 向客户发送一封带有pdf文件附件(不工作)的“thank you message”邮件。
我的意思是,客户没有收到一封电子邮件,但是当他/她打开PDF文件时,我收到以下错误信息:
“的Acrobat不能OEN‘FILE_NAME’,因为它是要么是不支持的文件类型,要么是因为文件已损坏(例如,它是作为电子邮件附件发送的,未正确解码)...“
请注意,这是我的最后时间做一个创建PDF文件附件的项目。如果有人能帮我解决这个问题,那就太好了。谢谢!
这里是我的代码:
<?php
// once there are no errors, as soon as the customer hits the submit button, it needs to send an email to the staff with the customer information
$msg = "Name: " .$_POST['name'] . "\n"
."Email: " .$_POST['email'] . "\n"
."Phone: " .$_POST['telephone'] . "\n"
."Number Of Guests: " .$_POST['numberOfGuests'] . "\n"
."Date Of Reunion: " .$_POST['date'];
$staffEmail = "staffinfo";
mail($staffEmail, "You have a new customer", $msg); // using the mail php function to send the email. mail(to, subject line, message)
//once the customer submits his/her information, he/she will receive a thank you message attach with a pdf file.
$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont("Arial", "B", 16);
$pdf->Cell(40, 10, "Hello World!");
// email information
$to = $_POST['email'];
$from = $staffEmail;
$subject = "Thank you for your business";
$message = "Thank you for submitting your information!";
// a random hash will be necessary to send mixed content
$separator = md5(time());
// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;
// attachment name
$filename = "yourinformation.pdf";
// encode data (puts attachment in proper format)
$pdfdoc = $pdf->Output("", "S");
$attachment = chunk_split(base64_encode($pdfdoc));
// encode data (multipart mandatory)
$headers = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol.$eol;
$headers .= "Content-Transfer-Enconding: 7bit".$eol;
$headers .= "This is a MIME encoded message.".$eol.$eol;
// message
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: text/html; charsrt=\"iso-8859-1\"".$eol;
$headers .= $message.$eol.$eol;
// attachment
$headers .= "--".$separator.$eol;
//$headers .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
$headers .= "Content-Type: application/zip; name=\"".$filename."\"".$eol;
$headers .= "Content-Transfer-Encoding: base64".$eol;
$headers .= "Content-Disposition: attachment".$eol.$eol;
$headers .= $attachment.$eol.$eol;
$headers .= "--".$separator."--";
// send message
mail($to, $subject, $message, $headers);
}
}
?>
// once there are no errors, as soon as the customer hits the submit button, it needs to send an email to the staff with the customer information
$msg = "Name: " .$_POST['name'] . "\n"
."Email: " .$_POST['email'] . "\n"
."Phone: " .$_POST['telephone'] . "\n"
."Number Of Guests: " .$_POST['numberOfGuests'] . "\n"
."Date Of Reunion: " .$_POST['date'];
$staffEmail = "staffemail";
mail($staffEmail, "You have a new customer", $msg); // using the mail php function to send the email. mail(to, subject line, message)
//once the customer submits his/her information, he/she will receive a thank you message attach with a pdf file.
// creating a pdf file
$pdf_filename = tempnam(sys_get_temp_dir(), "pdf");
$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont("Arial", "B", 16);
$pdf->Cell(40, 10, "Title");
$pdf->Ln();
$pdf->SetFont("Arial", "", 12);
$pdf->Cell(15, 10, "Name:");
$pdf->SetFont("Arial", "I", 12);
$pdf->Cell(15, 10, $_POST['name']);
$pdf->Ln();
$pdf->SetFont("Arial", "", 12);
$pdf->Cell(15, 10, "Email:");
$pdf->SetFont("Arial", "I", 12);
$pdf->Cell(15, 10, $_POST['email']);
$pdf->Ln();
$pdf->SetFont("Arial", "", 12);
$pdf->Cell(15, 10, "Phone:");
$pdf->SetFont("Arial", "I", 12);
$pdf->Cell(15, 10, $_POST['telephone']);
$pdf->Ln();
$pdf->SetFont("Arial", "", 12);
$pdf->Cell(40, 10, "Number of Guests:");
$pdf->SetFont("Arial", "I", 12);
$pdf->Cell(40, 10, $_POST['numberOfGuests']);
$pdf->Ln();
$pdf->SetFont("Arial", "", 12);
$pdf->Cell(40, 10, "Date Of Reunion:");
$pdf->SetFont("Arial", "I", 12);
$pdf->Cell(40, 10, $_POST['date']);
// if file doesn't exists or if it is writable, create and save the file to a specific place
if(!file_exists($pdf_filename) || is_writable($pdf_filename)){
$pdf->Output($pdf_filename, "F");
} else {
exit("Path Not Writable");
}
// using the phpmailer class
// create a new instance called $mail and use its properties and methods.
$mail = new PHPMailer();
$staffEmail = "staffemail";
$mail->From = $staffEmail;
$mail->FromName = "name";
$mail->AddAddress($_POST['email']);
$mail->AddReplyTo($staffEmail, "name");
$mail->AddAttachment($pdf_filename);
$mail->Subject = "PDF file attachment";
$mail->Body = "message!";
// if mail cannot be sent, diplay error message
//if(!$mail->Send()){
//echo "<div id=\"mailerrors\">Message could not be sent</div>";
//echo "<div id=\"mailerrors\">Mailer Error: " . $mail->ErrorInfo . "</div>";
//} else { // else...if mail is sent, diplay sent message
//echo "<div id=\"mailerrors\">Message sent</div>";
//}
// delete the temp file
unlink($pdf_filename);
}
}
这是我的最终代码,它完美的工作! –
这是一个很晚的评论,但是您可能应该使用'tempnam'来生成一个唯一的文件名,这样您就不会冒险让两个客户一次碰到脚本。 – yakatz
试试这个:
<?php
// once there are no errors, as soon as the customer hits the submit button,
// it needs to send an email to the staff with the customer information
$msg = "Name: " .$_POST['name'] . "\n"
. "Email: " .$_POST['email'] . "\n"
. "Phone: " .$_POST['telephone'] . "\n"
. "Number Of Guests: " .$_POST['numberOfGuests'] . "\n"
. "Date Of Reunion: " .$_POST['date'];
$staffEmail = "staffinfo";
mail($staffEmail, "You have a new customer", $msg);
// once the customer submits his/her information, he/she will receive a thank
// you message attach with a pdf file.
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont("Arial", "B", 16);
$pdf->Cell(40, 10, "Hello World!");
// email information
$to = $_POST['email'];
$from = $staffEmail;
$subject = "Thank you for your business";
$message = "Thank you for submitting your information!";
// a random hash will be necessary to send mixed content
$separator = '-=-=-'.md5(microtime()).'-=-=-';
// attachment name
$filename = "yourinformation.pdf";
// Generate headers
$headers = "From: $from\r\n"
. "MIME-Version: 1.0\r\n"
. "Content-Type: multipart/mixed; boundary=\"$separator\"\r\n"
. "X-Mailer: PHP/" . phpversion();
// Generate body
$body = "This is a multipart message in MIME format\r\n"
. "--$separator\r\n"
. "Content-Type: text/html; charset=\"iso-8859-1\"\r\n"
. "\r\n"
. "$message\r\n"
. "--$separator\r\n"
. "Content-Type: application/pdf\r\n"
. "Content-Transfer-Encoding: base64\r\n"
. "Content-Disposition: attachment; filename=\"$filename\"\r\n"
. "\r\n"
. chunk_split(base64_encode($pdf->Output("", "S")))."\r\n"
. "--$separator--";
// send message
mail($to, $subject, $body, $headers);
好的DaveRandom,我现在就试试吧!谢谢! –
我在等待邮件进来,但似乎php邮件功能花费太长时间来发送邮件。第一封电子邮件总是需要很长时间。第一封电子邮件后,需要几秒钟才能收到电子邮件。只要我收到第一封电子邮件,我会确认它是否有效。谢谢! –
没有为我工作,所以我改变了一切,我最终使用phpmailer类。感谢大家的帮助! –
我注意到,你有应用程序/压缩。尝试应用程序/ pdf。 – Raisen
我也是这样做的,但它不起作用。 –
还没有适当考虑它,但是IIRC MIME头文件的eol始终是“\ r \ n”,“PHP_EOL”中的实际值取决于操作系统;这可能会把一个扳手放在你的$ headers var;虽然根据规范它应该是宽容的:http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html(19.3) – CD001