JSPDF创建并附加到电子邮件

时间:2016-02-24 15:51:02

标签: php ajax email pdf jspdf

发送带附件的邮件时遇到一些问题。附件文件是JSPDF库生成的pdf。首先,我开始生成pdf并使用Ajax调用脚本PHP,如下面的代码:

var pdf = doc.output(); 
$.post(
     "/smt/mail.php", 
     { 
          data: pdf 
      }, 
     function(data) {
         console.log(data.resultado); 
     }
 , 'json');

我也尝试使用代码行pdf = btoa(doc.output()); 但错误似乎是一样的。 在PHP脚本中我有:

  $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()

使用这段代码,我可以发送邮件,但没有附件,经过一点调试后,我可以看到条件if(is_file[$i])返回false。

任何人都可以帮助我?

谢谢

0 个答案:

没有答案