【问题标题】:Sending attachments with PHPMailer library使用 PHPMailer 库发送附件
【发布时间】:2022-01-26 06:58:43
【问题描述】:

目前我有一个表格,我在其中显示我的所有记录。现在我有一个将表格数据导出到 Excel 工作表的功能。现在,当我单击导出时,excel 表会转到我的 C:\Xammp\htdocs 文件夹。以下是我用来保存 Excel 工作表的代码。

function listing_export_allxls()
    {
        $spreadsheet = new Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet();
        $arrlistings = $this->input->post('listing');
        $lists = str_replace('-', ',', $arrlistings);
        $arrLists = $this->listings_model->exportallxls($lists = '');
        $sheet->setCellValue('A1', 'Listing Sales');
        $sheet->getStyle("A1")->getFont()->setSize(16);
        $i = 0;
        $row = 3;
        $Column = array('A', 'B', 'C', 'D', 'E', 'F');
        // Header
        $column_title = array('Ref No', 'Name', 'For', 'Unit No.', 'Unit Type', 'Development');
        for ($i = 0; $i <= count($column_title) - 1; $i++) {
            $index = $Column[$i] . $row;
            $sheet->setCellValue($index, $column_title[$i]);
        }
        // Rows
        for ($j = 0; $j < count($arrLists); $j++) {
            $row++;
            $sheet->setCellValue('A' . $row, $arrLists[$j]['refno']);
            $sheet->setCellValue('B' . $row, $arrLists[$j]['proptitle']);
            $sheet->setCellValue('C' . $row, $arrLists[$j]['property_for']);
            $sheet->setCellValue('D' . $row, $arrLists[$j]['unitno']);
            $sheet->setCellValue('E' . $row, $arrLists[$j]['ctitle']);
        }
        $sheet->getStyle('A3:M3')->getFill()
            ->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
            ->getStartColor()->setARGB('FFA0A0A0');
        $writer = new Xlsx($spreadsheet);
        $writer->save('all_listings_' . date('dmyhis') . '.xls');
        $attachment = ($_SERVER['DOCUMENT_ROOT'] .'all_listings_' . date('dmyhis') . '.xls');
        $this->send_mail_excel('test@email.com', 'test', 'test',$attachment, '', '');
    }

现在我将文件名保存到$attachment,并在我的 send_email 函数中编写以下内容:

function send_mail_excel($emailto, $subject, $message, $attachment, $agentemail = "", $agentname = "")
{
    require_once($_SERVER['DOCUMENT_ROOT'] . '/application/libraries/PHPMailer/src/PHPMailer.php');
    require_once($_SERVER['DOCUMENT_ROOT'] . '/application/libraries/PHPMailer/src/SMTP.php');
    require_once($_SERVER['DOCUMENT_ROOT'] . '/application/libraries/PHPMailer/src/Exception.php');

    $mail = new PHPMailer\PHPMailer\PHPMailer();

    try {
        //Server settings                   
        $mail->isSMTP();                                            //Send using SMTP
        $mail->Host = "smtp.gmail.com";                             //Set the SMTP server to send through
        $mail->SMTPAuth   = true;                                   //Enable SMTP authentication
        $mail->SMTPSecure = 'ssl';
        $mail->Port       = 465;

        $mail->Username = "xxx";
        $mail->Password = 'xxx';
        $mail->SetFrom("xxx");
        //multiple recepients
        if (strrpos($emailto, ',') > 0) {
            $recipients = explode(',', $emailto);
            $i = 0;
            foreach ($recipients as $email) {
                if ($i == 0) {

                    $mail->addAddress($email);
                    $mail->AddCC($email);
                    if($agentemail){
                        $mail->addReplyTo($agentemail, $agentname);
                    }
                }
                ++$i;
            }
        } else {
            $mail->addAddress($emailto);
            $mail->AddCC($emailto);
            if($agentemail){
                $mail->addReplyTo($agentemail, $agentname);
            }
        }

        //Content
        $mail->isHTML(true);                                  //Set email format to HTML
        $mail->Subject = $subject;
        $mail->Body = $message;
        $mail->addAttachment($attachment);
        $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

        $mail->send();
        $msg = 'Message has been sent';
    } catch (Exception $e) {
        $msg = "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
    }
    return $msg;
}

现在我可以在这里成功获取我的电子邮件,但它只显示主题和消息,但没有附加任何附件。那么在这里添加附件到底哪里出错了?

编辑: 所以我发现了问题所在。我在 $attachment 上进行了打印,它显示了C:/Xammp/htdocs/all_listings_260122084136.xls,同时我的实际文件夹中的文件名是C:/Xammp/htdocs/all_listings_260122084135.xls。那么在这种情况下如何获得相同的文件名。我放的方法好像不行

【问题讨论】:

  • 首先检查 $attachment 中的确切内容,它指向的文件确实存在并且可以由运行的用户读取,然后检查 addAttachment 的返回值,它将返回如果它无法读取文件,则为 false。
  • @Synchro 好的,看来我遇到了问题。 $attachment 显示C:/Xammp/htdocs/all_listings_260122084136.xls,同时我的实际文件夹中的文件名是C:/Xammp/htdocs/all_listings_260122084135.xls。那么在这种情况下如何获得相同的文件名。我放的方法好像不行

标签: php excel codeigniter phpmailer


【解决方案1】:

出现问题是因为您生成了两次文件名,并且它是时间相关的,因此每次都可能不同。在变量中定义一次,然后将其用于两件事:

$attachment = $_SERVER['DOCUMENT_ROOT'] .'all_listings_' . date('dmyhis') . '.xls';
$writer->save($attachment);
$this->send_mail_excel('test@email.com', 'test', 'test', $attachment, '', '');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多