duandian8110 2012-07-02 17:31
浏览 39
已采纳

使用PEAR发送带附件的邮件时出错

My page continues to error out (Error 324 - Chrome) when attempting to send e-mails with attachments using PHP's PEAR Mail extension. Although the page error's out - I do receive one of the approximately 800 e-mails. Here's what I'm working with:

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 = 'example@example.com';
                // 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;
}

}

Now I thought maybe I wasn't giving the script enough time to execute - so I set it ridiculously high set_time_limit(99999999) to ensure it had plenty of time, although it normally times out within 10-15 seconds. So basically it works like this, I have an internal DB that stores customer account numbers and e-mail addresses. Accounts can have the same e-mail address (It's sent to their company's AP department) - aka one e-mail address receives statements for multiple branches. From their each statement is already constructed with the format being ACCOUNTNUMBER_MM_DD_YYYY.pdf.

So I'm simply trying to have a short message in the body, and attach the monthly statements. Again, not sure why it's failing, and even though the script halts, I do receive ONE of the e-mails (I'm sending them all to myself at the moment just to test). Can anyone see where I may have an issue?

  • 写回答

1条回答 默认 最新

  • dongzhuo0895 2012-07-02 18:38
    关注

    I discovered the issue. As of PHP 5.0+ you cannot have multiple instances of the same include file - aka Mail.php was included as it looped. Once it was moved outside of the loop, the issue was resolved.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于温度改变石墨烯介电性能(关键词-介电常数)
  • ¥150 HDMI分路器LT86102 的输出在890MHz频点处EMC超标8DB
  • ¥15 druid(相关搜索:数据库|防火墙)
  • ¥15 大一python作业
  • ¥15 preLaunchTask"C/C++: aarch64- apple-darwin22-g++-14 生成活动 文件”已终止,退出代码为-1。
  • ¥60 如何鉴定微信小程序数据被篡改过
  • ¥18 关于#贝叶斯概率#的问题:这篇文章中利用em算法求出了对数似然值作为概率表参数,然后进行概率表计算,这个概率表是怎样计算的呀
  • ¥20 C#上传XML格式数据
  • ¥15 elementui上传结合oss接口断点续传,现在只差停止上传和继续上传,各大精英看下
  • ¥20 手机截图相片分辨率降低一半