Reputation: 744
I am writing some code to take a page, turn it into a pdf, and email the pdf. I am using html2canvas and jsPDF to generate the pdf. I then send the pdf to a php script to process it and mail it, but this is failing. I know the pdf generation works, as I can save the pdf locally. Here is my code so far:
javascript:
$(document).ready(function() {
$('#download').click(function() {
html2canvas($('#wrapper'), {
onrendered: function(canvas) {
var imgData = canvas.toDataURL('image/png');
var doc = new jsPDF('p','mm');
doc.addImage(imgData, 'PNG', 10,10);
var pdfMail = btoa(doc.output());
$.post("../mailPdf.php",
{
data:pdfMail
},function (response,status) {
console.log(response);
});
}
});});});
mailPdf.php
function MailWithAttachment($to, $subject, $message, $senderMail, $senderName, $files){
$from = $senderName." <".$senderMail.">";
$headers = "From: $from";
// boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// headers for attachment
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
// multipart boundary
$message = "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"UTF-8\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
// preparing attachments
if(count($files) > 0){
for($i=0;$i<count($files);$i++){
if(is_file($files[$i])){
$message .= "--{$mime_boundary}\n";
$fp = @fopen($files[$i],"rb");
$data = @fread($fp,filesize($files[$i]));
@fclose($fp);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: application/octet-stream; name=\"".basename($files[$i])."\"\n" .
"Content-Description: ".basename($files[$i])."\n" .
"Content-Disposition: attachment;\n" . " filename=\"".basename($files[$i])."\"; size=".filesize($files[$i]).";\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
}
}
}
$message .= "--{$mime_boundary}--";
$returnpath = "-f" . $senderMail;
//send email
$mail = mail($to, $subject, $message, $headers, $returnpath);
//function return true, if email sent, otherwise return fasle
if($mail){ return TRUE; } else { return FALSE; }
}
if(!empty($_POST['data'])){
//email variables
$to = '[email protected]';
$from = '[email protected]';
$from_name = 'From Name';
//attachment files path array
$file = base64_decode($_POST['data']);
$subject = 'PHP Email with attachment';
$html_content = '<h1>PHP Email with attachment</h1>';
//call MailWithAttachment() function and pass the required arguments
$send_email = MailWithAttachment($to,$subject,$html_content,$from,$from_name,$file);
//print message after email sent
echo $send_email?"<h1> Mail Sent</h1>":"<h1> Mail not SEND</h1>";
} else {
echo "No Data Found";
}
The script works up until the if(is_file($files[$i])){
loop. is_file
always returns false and this loop fails to run. I can't figure out why its failing to recognize and read the file.
Upvotes: 0
Views: 847
Reputation: 3797
The problem with php side is that in function MailWithAttachment
you are expecting a little bit different type of data for files
parameter.
$file = $_POST['data'];
is actually not a file, but a base64 encoded string(btoa(doc.output())
). So what you actually want to do is something like this(following the signature of MailWithAttachment
function).
change this:
//attachment files path array
$file = $_POST['data'];
to something like this:
//attachment file encoded
$files = array();
$file = $_POST['data'];
$filePath = '/tmp/foo.pdf';
// actually here you can do whatever you want. you just need to save file on disk, at least for request context. I think creating file in memory also should work.
file_put_contents($filePath, base64_decode($file));
$files[] = $filePath;
...
// pay attention $file is changed to $files here
MailWithAttachment(...,$files);
...
Upvotes: 3