2012-07-02 99 views
0

嘗試使用PHP的PEAR Mail擴展發送帶附件的電子郵件時,我的頁面仍然出錯(錯誤324 - Chrome)。儘管頁面錯誤消失 - 我確實收到了大約800封電子郵件中的一封。這裏就是我的工作:使用PEAR發送帶附件的郵件時發生錯誤

function email_statements($type) { 
switch($type) { 
    // Determine the type of statement to be e-mailed 
    case 'monthly': 
     // Array holding all unique AP e-mail addresses 
     $ap_email_addresses = array(); 
     include('../path/to/db/connection/params.php'); 
     // Grab the unique AP e-mail address set to receive statements 
     $stmt = $mysqli->prepare("SELECT email FROM statements GROUP BY email ORDER BY email ASC"); 
     $stmt->execute(); 
     $stmt->bind_result($ap_email_address); 
     // Add unique e-mail address to AP E-mail Addresses array 
     while($row = $stmt->fetch()) $ap_email_addresses[] = $ap_email_address; 
     $stmt->close(); 
     $mysqli->close(); 
     // Verify we grabbed the e-mail addresses 
     if(count($ap_email_addresses) == 0) { 
      // Exit and return error code if unable to grab e-mail addresses 
      $return_message = 1; 
      exit; 
     } 
     // E-mail addresses grabbed - proceed 
     else { 
      // PDF formatted date 
      date_default_timezone_set('America/New_York'); 
      $formatted_date = date('m_d_Y'); 
      // Now we have the unique e-mail addresses - associate those with the account numbers 
      include('../path/to/db/connection/params.php'); 
      foreach($ap_email_addresses as $email_address) { 
       $file_names = array(); 
       $stmt = $mysqli->prepare("SELECT customer_number FROM statements WHERE email = ?"); 
       $stmt->bind_param("s", $email_address); 
       $stmt->execute(); 
       $stmt->bind_result($ap_account_number); 
       // Constructs the name of the statement (PDF) file to be sent 
       while($output = $stmt->fetch()) $file_names[] = $ap_account_number . '_' . $formatted_date . '.pdf'; 
       // Send e-mails 
       include('Mail.php'); 
       include('Mail/mime.php'); 
       // Include SMTP authentication parameters 
       include('../path/to/email/info.php'); 
       // Set the e-mail recipients 
       $recipients = '[email protected]'; 
       // Set the e-mail subject 
       $subject = 'Monthly Account Statement'; 
       // Create the e-mail body 
       $html = 
       '<html> 
        <body> 
         <p>Test E-mail</p> 
        </body> 
       </html>'; 
       // Construct the message headers 
       $headers = array(
        'From'   => $from, 
        'Subject'  => $subject, 
        'Content-Type' => 'text/html; charset=UTF-8', 
        'MIME-Version' => '1.0' 
       ); 
       $mimeparams = array(); 
       $mimeparams['text_encoding'] = '8bit'; 
       $mimeparams['text_charset'] = 'UTF-8'; 
       $mimeparams['html_charset'] = 'UTF-8'; 
       $mimeparams['head_charset'] = 'UTF-8'; 
       // Create a new instance 
       $mime = new Mail_mime(); 
       // Specify the e-mail body 
       $mime->setHTMLBody($html); 
       // Attach the statements (PDF) 
       foreach($file_names as $attached_file) { 
        $file = '../path/to/the/pdf/file/' . $attached_file; 
        $file_name = $attached_file; 
        $content_type = "Application/pdf"; 
        $mime->addAttachment ($file, $content_type, $file_name, 1); 
       } 
       // Create the body 
       $body = $mime->get($mimeparams); 
       // Add the headers 
       $headers = $mime->headers($headers); 
       // Create SMTP connect array to be passed 
       $smtp = Mail::factory('smtp', array ('host' => $host, 'port' => $port, 'auth' => true, 'username' => $username, 'password' => $password)); 
       // Send the message 
       $mail = $smtp->send($recipients, $headers, $body); 
       if(PEAR::isError($mail)) echo 'Error'; 
       else echo 'Sent'; 
       // Close this account and cycle through the rest 
       $stmt->close(); 
      } 
      $mysqli->close(); 
     } 
     break; 
} 

}

現在,我想也許我沒有給腳本足夠的時間來執行 - 所以我將它高得離譜set_time_limit(99999999),以確保它有足夠的時間儘管它通常在10-15秒內超時。所以基本上它是這樣工作的,我有一個內部DB存儲客戶帳號和電子郵件地址。帳戶可以具有相同的電子郵件地址(將其發送到其公司的AP部門) - 也就是一個電子郵件地址接收多個分支機構的報表。從它們的每個語句已經構建,格式爲ACCOUNTNUMBER_MM_DD_YYYY.pdf。

所以我只是試圖在正文中留言,並附上月報表。再次,不知道爲什麼它失敗了,即使腳本停止,我確實收到其中一封電子郵件(我現在將它們全部發給自己,以便測試)。任何人都可以看到我可能有問題嗎?

回答

0

我發現了這個問題。從PHP 5.0開始,你不能擁有同一個包含文件的多個實例 - 也就是說Mail.php被包含在循環中。一旦它被移出循環之外,問題就解決了。