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

时间:2012-07-02 17:31:56

标签: php email pear attachment

尝试使用PHP的PEAR Mail扩展程序发送带附件的电子邮件时,我的页面继续出错(错误324 - Chrome)。虽然页面错误消失了 - 但我确实收到了大约800封电子邮件中的一封。以下是我正在使用的内容:

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;
}

}

现在我想也许我没有给脚本足够的时间来执行 - 所以我设置它可笑的高set_time_limit(99999999)以确保它有足够的时间,虽然它通常在10-15秒内超时。所以基本上这样工作,我有一个内部数据库,存储客户帐号和电子邮件地址。帐户可以具有相同的电子邮件地址(它被发送到他们公司的AP部门) - 也就是说,一个电子邮件地址接收多个分支的语句。从他们的每个语句中已经构建了格式为ACCOUNTNUMBER_MM_DD_YYYY.pdf。

所以我只是想在身体中发短信,并附上月结单。再一次,不知道为什么它会失败,即使脚本停止,我也会收到一封电子邮件(我现在只是为了测试而将它们全部发送给自己)。任何人都可以看到我可能有问题吗?

1 个答案:

答案 0 :(得分:0)

我发现了这个问题。从PHP 5.0+开始,你不能拥有同一个包含文件的多个实例 - 在循环时也包含了Mail.php。一旦它移出循环,问题就解决了。