I am using PHP Mailer to basically send a interactive PDF to an email address. This works locally calling the script from the PDF to the server, but does not work when the PDF is completed on the server.
Code is below:
<?php
if(!isset($HTTP_RAW_POST_DATA)) {
echo "The Application could not be sent. Please save the PDF and email it manually.";
exit;
}
echo "<html><head></head><body><img src='loading.gif'>";
//Create PDF file with data
$semi_rand = md5(time());
$pdf = $HTTP_RAW_POST_DATA;
$file = $semi_rand . ".pdf";
$handle = fopen($file, 'w+');
fwrite($handle, $pdf);
fclose($handle);
//
require_once('class/class.phpmailer.php');
include("class/class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
$mail = new PHPMailer(false); // the true param means it will throw exceptions on errors, which we need to catch
$mail -> CharSet = "UTF-8";
$mail->IsSMTP(); // telling the class to use SMTP
try {
$mail->Host = "HOST"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "HOST";
$mail->Port = 465;
$mail->Username = "USERNAME";
$mail->Password = "PASSWORD";
$mail->AddAddress('TO ADDRESS');
$mail->SetFrom('FROM ADDRESS');
$mail->Subject = 'SUBJECT';
$mail->Body = 'Please see attachment';
$mail->IsHTML(true);
$mail->AddAttachment($file); // attachment
$mail->Send();
//Delete the temp pdf file then redirect to the success page
// unlink($file);
echo '<META HTTP-EQUIV="Refresh" Content="0; URL="1.1.1.1">';
exit;
} catch (phpmailerException $e) {
//you can either report the errors here or redirect them to an error page
//using the above META tag
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
//Verify the temporary pdf file got deleted
unlink($file);
?>
Is there something I am missing? All values for the $mail (host, username, password etc) are correct - but when it creates the PDF to send, it only comes through as < 1kb. My PDF calls this PHP file on submit.
答案 0 :(得分:0)
You can't create PDFs like simple txt or csv files with fwrite
. It is a more sophisticated file type.
Look into DomPDF which for your setting may look somewhat like the below (assuming $HTTP_RAW_POST_DATA
is an html document):
require("dompdf_config.inc.php");
$semi_rand = md5(time());
$pdf = file_get_contents('http://www.pdfpage.com/');
$file = $semi_rand . ".pdf";
$dompdf = new DOMPDF();
$dompdf->load_html($pdf);
$dompdf->set_paper('a4', 'portrait');
$dompdf->render();
$dompdf->stream($file, array('Attachment' => '0'));
...
// USE $file as needed in email attachment