使用 PHPMailer 库发送附件

Sending attachments with PHPMailer library

目前我有一个 table 显示我所有记录的地方。现在我可以将 table 数据导出到 excel sheet。现在,当我单击导出时,excel sheet 会转到我的 C:\Xammp\htdocs 文件夹。以下是我用来保存 excel sheet.

的代码
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。那么在这种情况下如何获得相同的文件名。我放的方法好像不行

出现问题是因为您生成了两次文件名,而且是 time-dependent,所以每次都可能不同。在变量中定义一次,然后将其用于两件事:

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