2013-03-26 63 views
0

我想发送一个PDF生成与TCPDF使用邮件PHP函数。附加pdf生成与TCPDF邮件发送邮件PHP函数

如果我发送这样简单的邮件:mail('[email protected]','My Subject',$ message);

pdf生成的很好,但是当我想在标题中附上pdf不起作用。

这是我的代码:

$pdfdoc=$pdf->Output('file.pdf', 'S'); 

$separator = md5(time()); 

     $eol = PHP_EOL; 
// Send 
     $filename = "_Desiredfilename.pdf"; 

     // encode data (puts attachment in proper format) 

     $attachment = chunk_split(base64_encode($pdfdoc)); 

     ///////////HEADERS INFORMATION//////////// 
     // main header (multipart mandatory) message 
     $headers = "From: Sender_Name<[email protected]>".$eol; 
     $headers .= "Bcc: [email protected]".$eol; 
     $headers .= "MIME-Version: 1.0".$eol; 
     $headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol.$eol; 
     $headers .= "Content-Transfer-Encoding: 7bit".$eol; 
     $headers .= "This is a MIME encoded message.".$eol.$eol; 

     // message 
     $headers .= "--".$separator.$eol; 
     $headers .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol; 
     $headers .= "Content-Transfer-Encoding: 8bit".$eol.$eol; 
     $headers .= $message.$eol.$eol; 

     // attachment 
     $headers .= "--".$separator.$eol; 
     $headers .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol; 
     $headers .= "Content-Transfer-Encoding: base64".$eol; 
     $headers .= "Content-Disposition: attachment".$eol.$eol; 
     $headers .= $attachment.$eol.$eol; 
     $headers .= "--".$separator."--"; 


     //Email message 
     mail('[email protected]', 'prova', 'hola', $headers); 

当是什么问题?

感谢

问候

+0

什么是你的错误?你有什么尝试? – Eernie 2013-03-26 09:17:12

+0

问题是邮件没有发送。 – 2013-03-26 09:20:26

+0

可能是php.ini配置中的问题? – 2013-03-26 09:21:10

回答

1

我希望这会为你工作了。我已经检查过它的作品。请尝试它。在此之前,请检查“php.ini”的邮件,并检查PDF文件是否创建。

$pdfdoc=$pdf->Output('file.pdf', 'F'); 

     // Send 
     $files = "file.pdf"; 



     ///////////HEADERS INFORMATION//////////// 
     // 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/plain; charset=\"iso-8859-1\"\n" . 
     "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; 

     // preparing attachments 
     $message .= "--{$mime_boundary}\n"; 
      $fp  = @fopen($files,"rb"); 
      $data = @fread($fp,filesize($files)); 
        @fclose($fp); 
      $data = chunk_split(base64_encode($data)); 
      $message .= "Content-Type: application/octet-stream; name=\"".basename($files)."\"\n" . 
      "Content-Description: ".basename($files)."\n" . 
      "Content-Disposition: attachment;\n" . " filename=\"".basename($files)."\"; size=".filesize($files).";\n" . 
      "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n"; 
     $message .= "--{$mime_boundary}--"; 


     //Email message 
     mail('[email protected]', 'prova', 'hola', $headers); 

或在代码中加入此功能,使用该功能发送多附件文件。 这一个很好。请试试这个。

/* 
    * Function Name: sentMail 
    * Scope  : Sent Mail Function with CC , BCC and Attachment files.. 
    * Input  : Recipient Name   => $recipientName // (Required) 
    *    Recipient MailId(Many) => $recipientMailId // (Required & Array Format) 
    *    Subject Content   => $subjectContent // (Required) 
    *    Message Content   => $messageContent // (Required) 
    *    Sender MailId    => $senderMailId // (Required) 
    *    Sender Name(Many)   => $senderName  // (Required) 
    *    Recipient CC MailId  => $recipientCCMailId //(Optional & Array Format) 
    *    Recipient BCC MailId  => $recipientBCCMailId //(Optional & Array Format) 
    *    Attachment Files   => $attachmentFiles  //(Optional & Array Format) 
    * Output  : Sent mail to the Recipient. 
    */ 
    public function sentMail($recipientName,$recipientMailId,$subjectContent,$messageContent,$senderName,$senderMailId,$recipientCCMailId,$recipientBCCMailId,$attachmentFiles) 
    { 
     try 
     { 
      /*Get the Mulitple Recipient CC MailId*/ 
      if(isset($recipientCCMailId)){ 
       if(count($recipientCCMailId)>1){ 
        $recipientCCMailId = implode(',',$recipientCCMailId); 
       } 
       else{ 
        $recipientCCMailId = $recipientCCMailId[0]; 
       } 
      } 

      /*Get the Mulitple Recipient BCC MailId*/ 
      if(isset($recipientBCCMailId)){ 
       if(count($recipientBCCMailId)>1){ 
        $recipientBCCMailId = implode(',',$recipientBCCMailId); 
       } 
       else{ 
        $recipientBCCMailId = $recipientBCCMailId[0]; 
       } 
      } 

      /*** Mail Contents Starts ***/ 
       $subj = $subjectContent; 
       $msg =""; 

       /*Get the Mulitple Recipient BCC MailId*/ 
       if(count($recipientMailId)>1){ 
        $recipientMailId = implode(',',$recipientMailId); 
        $msg .="Dear, <b>All</b>\r <br>"; 
       } 
       else{ 
        $recipientMailId = $recipientMailId[0]; 
        $msg .="Dear, <b>".ucwords($user_name)."</b> \r <br>"; 
       } 
       $msg .=$messageContent."\r <br><br>"; 
       $msg .="Thank you"."\r\n\n <br>"; 
       $msg .=$senderName."\r\n\n <br><br>"; 

       $headers =""; 

       /** Get the Mulitple Attachment files and Attachment Process Starts **/ 
       if(isset($attachmentFiles)){ 
        $headers .= "From: ".$senderMailId."\r\n"; 
        $headers .= "Cc: ".$recipientCCMailId."\r\n"; 
        $headers .= "Bcc: ".$recipientBCCMailId."\r\n"; 
        // 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 
        $msg .= "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . 
        "Content-Transfer-Encoding: 7bit\n\n" . $msg . "\n\n"; 

        // preparing attachments 
        for($i=0;$i<count($attachmentFiles);$i++) 
        { 
         if(is_file($attachmentFiles[$i])) 
         { 
          $msg .= "--{$mime_boundary}\n"; 
          $fp  = @fopen($attachmentFiles[$i],"rb"); 
          $data = @fread($fp,filesize($attachmentFiles[$i])); 
             @fclose($fp); 
          $data = chunk_split(base64_encode($data)); 
          $msg .= "Content-Type: application/octet-stream; name=\"".basename($attachmentFiles[$i])."\"\n" . 
          "Content-Description: ".basename($attachmentFiles[$i])."\n" . 
          "Content-Disposition: attachment;\n" . " filename=\"".basename($attachmentFiles[$i])."\"; size=".filesize($attachmentFiles[$i]).";\n" . 
          "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n"; 
         } 
        } 
        $msg .= "--{$mime_boundary}--"; 
       } 
       /** Attachment Process Ends **/ 
       else{ 
        $headers .= "MIME-Version: 1.0" . "\r\n"; 
        $headers .= "Content-type:text/html;charset=utf-8" . "\r\n"; 
        //$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n"; 
        $headers .= "From: ".$senderMailId."\r\n"; 
        $headers .= "Cc: ".$recipientCCMailId."\r\n"; 
        $headers .= "Bcc: ".$recipientBCCMailId."\r\n"; 
       } 
      /*** Mail Contents Ends ***/  
      $sent_mail=mail($recipientMailId,$subj,$msg,$headers); 
      if($sent_mail) 
      { 
       return true; 
      } 
      else 
      { 
       return false; 
      } 
     } 
     catch (Exception $e) 
     { 
      getConnection('close'); 
      echo "Caught Exception:",$e->getMessage(); 
     } 
    } 

并且您在该函数的DTD中通过此参数以指定格式编辑了此参数。

sentMail($recipientName,$recipientMailId,$subjectContent,$messageContent,$senderName,$senderMailId,$recipientCCMailId,$recipientBCCMailId,$attachmentFiles); 

这里,

$recipientName = "sam"; 
$recipientMailId = array("[email protected]"); 
$subjectContent = "this is sample pdf attachment over email"; 
$messageContent = "this is sample pdf attachment over email"; 
$senderMailId = "[email protected]" 
$senderName  = "sender"; 
$recipientCCMailId = array("[email protected]"); 
$recipientBCCMailId = array("[email protected]"); 
$attachmentFiles = array("filename.pdf"); 

sentMail($recipientName,$recipientMailId,$subjectContent,$messageContent,$senderName,$senderMailId,$recipientCCMailId,$recipientBCCMailId,$attachmentFiles); 
+0

谢谢,我去证明它。如果我使用F选项生成pdf,pdf将保存在服务器中?只有时间?谢谢 – 2013-03-26 10:18:41

+0

我不希望在服务器中保存pdf,如果可能,我希望在成功发送带有pdf的邮件后生成字符串 – 2013-03-26 10:29:10

+0

。您已使用“取消链接($文件)”删除该临时文件。 – MKV 2013-03-26 10:39:37