I generated a pdf object with jsPDF. I want to send mail with PDF attached by AJAX, but I can't send the file correctly. I try to convert in Blob object to send and later in PHP I try to decode to base64 for can be sent by mail, but when I receive mail I receive blob file without extension.
1.- I create PDF object:
var pdf = new jsPDF(); // new pdf object
pdf.text("Table title", 14, 16); // text line
var elem = $(".tableSample")[0]; // node -> html to pdf
var res = pdf.autoTableHtmlToJson(elem); // lib to transform htmlTables to pdf
pdf.autoTable(res.columns, res.data, {startY: 20}); // lib to transform htmlTables to pdf
var outputBase64 = pdf.output('datauristring');
var blob = new Blob([outputBase64], { type: "application/pdf"});
2.- I Call to my function Ajax:
ajaxAdjunto({
controler : "ctInformes2.php",
method : "enviarInforme",
attached : blob,
paramValid : {
mailText : "This is the mail body",
mailAsunto : "Este es el asunto",
mailDest : "mailTo@mailTo.com"
},
callbackSucces : function (backParam) { },
callbackError : function (err) { }
});
3.- My function Ajax is prepared for send with attachments with FormData object:
function ajaxAdjunto(objParam){
url = "./controller/"+objParam.controler+"?metodo="+objParam.method;
param = new FormData();
//Add to FormData mail text
for (var item in objParam.paramValid){
if (item == ""){
param.append(item , "null");
}else{
param.append(item , objParam.paramValid[item ]);
}
}
//Add to FormData file
param.append("adjunto", objParam.attached);
//Call Ajax
$.ajax({
data: param,
type: "POST",
url: url,
cache: false,
contentType: false,
processData: false,
success: function (backParam) {
objParam.callbackSucces(backParam, objParam);
},
error: function (xhr){
if ( objParam.callbackError ){
objParam.callbackError(xhr);
}else{
alerta(xhr.statusText);
console.log(xhr);
}
}
});
}
PHP CODE - ( I remove other $body content and mail headers for this sample )
// var_dump -> $_FILES['attached']
array (size=5)
'name' => string 'blob' (length=4)
'type' => string 'application/pdf' (length=15)
'tmp_name' => string 'C:\Windows\Temp\php9593.tmp' (length=27)
'error' => int 0
'size' => int 6328
// PHP CODE
if ( count($_FILES) > 0 ){
$nameFile = $_FILES['attached ']['name'];
$sizeFile = $_FILES['attached ']['size'];
$typeFile = $_FILES['attached ']['type'];
$tempFile = $_FILES["attached "]["tmp_name"];
$body .= "--=C=T=E=C=\r\n"; // delimiter
$body .= "Content-Type: application/octet-stream; ";
$body .= "name=" . $nameFile . "\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n";
$body .= "Content-Disposition: attachment; ";
$body .= "filename=" . $nameFile . "\r\n";
$body .= "\r\n"; // empty line
$fp = fopen($tempFile, "rb");
$file = fread($fp, $sizeFile);
$file = chunk_split(base64_encode($file));
$body .= "$file\r\n";
$body .= "\r\n"; // empty line
}
$body .= "--=C=T=E=C=--\r\n"; // delimiter end mail
//Send mail
if(mail($mailTo, $subject, $body, $header)){
echo "mail was sent";
}else{
echo "error when try send mail";
}