将生成的zip文件附加到电子邮件

时间:2016-05-31 13:07:43

标签: php email swiftmailer

我正在尝试创建一个系统来为我们的客户发布报告。为此,我创建了一个类,将库存结果上传到云端(用户也可以在线访问他们的报告)。但是,我遇到了一些我无法解决的问题。

所以,问题编号1.我正在使用一个简单的函数生成一个zip文件:

public function generateReports($emailAddress)
{
    ?>
    <script>
    var url = '<?php echo Navigation::gUrl('/users/admin/reportspack.php', array('stocktake_id' => $this->stocktake_id));?>';
        $.ajax({
            url: url,
            dataType: 'json',
            type: 'post',
            timeout: 5000,
            {}

        });
        </script>
        <?
        echo "<h2>Reports generated and saved in a ZIP folder.</h2>";
}

该功能的下一部分是将文件附加到电子邮件:

require_once "/swiftmailer/swiftmailer/lib/swift_required.php";
    //require_once "mime.php";
    $query = "SELECT s.store_name AS store_name, s.supervisor_name AS supervisor_name ,DATE_FORMAT(s.stocktake_date, '%d/%l/%Y') AS stocktake_date, u.username AS username, u.password AS password
                    FROM stocktakes s 
                    INNER JOIN users u ON u.company_id = s.sms_customer_id
                    WHERE stocktake_id = '{$this->stocktake_id}'
                      ";
    //echo $query;
    //die();
    $result = $this->dbClient->query($query);;
    $row = $result->fetch(PDO::FETCH_ASSOC);
    $file = 'C:/Reports/Reports'.$this->stocktake_id.'.zip';
    $transport = Swift_SmtpTransport::newInstance('lh233.dnsireland.com', 465, 'ssl')
        ->setUsername('from@email.address')
        ->setPassword('notsharingmypassword');

    $from = 'from@email.address';
    $to = $emailAddress;
    //echo $to;
    //die();
    //$subject ='Test stocktake summary reports' ;
    $body = "Hello,\n\nPlease find attached reports for your most recent stocktake carried out at " . $row['store_name'] ."\n\n These reports have also been uploaded to the customer portal and can be accessed by logging in to http://reports.stocktaking.ie/ using the following credentials:\n\nusername:".$row['username']." \n\npassword:".$row['password']."\n\nShould you have any queries, please do not hesitate to contact me.\n\nKind Regards,\n\n ". $row['supervisor_name'] ."\n\n <<HOLDER FOR STOCKTAKING.IE SIGNATURE>>";

    $mailer = Swift_Mailer::newInstance($transport);
    $message = Swift_Message::newInstance("".$row['store_name'].",Stocktake Summary Reports, ".$row['stocktake_date']."")
    ->setFrom(array($from => $row['supervisor_name']))
  ->setTo(array($to, $to => $row['store_name']))
  ->setBody($body)
  ;
    $message->attach(Swift_Attachment::fromPath($file));
    //echo $message;
    $result = $mailer->send($message);
    echo $result;
    if ($result != 1) {
         echo('<p>Error when sending the e-mail.</p>');
     } else {
         echo('<h2>Message successfully sent!</h2>');
     }

我第一次尝试发布报告时收到以下错误消息:

  

致命错误:未捕获的异常&#39; Swift_IoException&#39;有消息&#39;无法打开文件阅读[C:/Reports/Reports4074.zip]&#39;在C:\ Apache \ htdocs \ swiftmailer \ swiftmailer \ lib \ classes \ Swift \ ByteStream \ FileByteStream.php:144堆栈跟踪:#0 C:\ Apache \ htdocs \ swiftmailer \ swiftmailer \ lib \ classes \ Swift \ ByteStream \ FileByteStream .php(84):Swift_ByteStream_FileByteStream-&gt; _getReadHandle()#1 C:\ Apache \ htdocs \ swiftmailer \ swiftmailer \ lib \ classes \ Swift \ Mime \ ContentEncoder \ Base64ContentEncoder.php(43):Swift_ByteStream_FileByteStream-&gt; read(8192 )#2 C:\ Apache \ htdocs \ swiftmailer \ swiftmailer \ lib \ classes \ Swift \ Mime \ SimpleMimeEntity.php(547):Swift_Mime_ContentEncoder_Base64ContentEncoder-&gt; encodeByteStream(Object(Swift_ByteStream_FileByteStream),Object(Swift_Transport_StreamBuffer),0,78)# 3 C:\ Apache \ htdocs \ swiftmailer \ swiftmailer \ lib \ classes \ Swift \ Mime \ SimpleMimeEntity.php(522):Swift_Mime_SimpleMimeEntity-&gt; _bodyToByteStream(Object(Swift_Transport_StreamBuffer))#4 C:\ Apache \ htdocs \ swiftmailer \ swiftmailer \ lib \ classes \ Swift \ Mime \ Simple in C:\ Apache \ htdocs \ swiftmailer \ swiftmailer \ lib \ classes \ Swift \ ByteStream \ FileB第144行的yteStream.php

这种情况表明,在尝试将zip文件附加到电子邮件时,zip文件仍在生成,这不应该是这种情况吗?代码应该等待代完成?我甚至尝试将电子邮件部分作为单独的函数并在生成zip文件后调用它,但得到了相同的最终结果。当我立即刷新页面时,会附上zip文件夹并发送电子邮件。

感谢您的任何建议!

0 个答案:

没有答案