问题
I have some problems in send a mail with attachments. The attachment file is a pdf generated by JSPDF library. Firstly I start to generate the pdf and call the script PHP with Ajax, like the code below:
var pdf = doc.output();
$.post(
"/smt/mail.php",
{
data: pdf
},
function(data) {
console.log(data.resultado);
}
, 'json');
Also i tried with the line of code pdf = btoa(doc.output());
but the error seems the same.
In the script PHP I have:
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// 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}--";
$mail = new PHPMailer(true);
try{
$mail->IsSMTP();
$mail->SMTPAuth = true; // enable SMTP authentication
//$mail->SMTPDebug = 1;
$mail->CharSet = 'UTF-8';
//$mail->Host = 'smtp.office365.com';
$mail->Host = 'mail2.mailbox.pt';
$mail->Username = "****"; // SMTP account username example
$mail->Password = "****"; // SMTP account password example
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;
$mail->From = "***";
$mail->FromName = "******";
$mail->AddAddress($to, "Test");
$mail->AddReplyTo("****", '*****');
$mail->IsHTML(true);
$mail->Subject = $subject;
$mail->Body = $message;
$mail->Send()
With this code I can sent the mail, but without attachement, after a little debug I can see that the condition if(is_file[$i])
returns false.
Anyone can help me?
Thank You
来源:https://stackoverflow.com/questions/35606696/jspdf-create-and-attach-to-email