使用Codeigniter中的PHPMailer将mpdf输出作为附件电子邮件文件发送

时间:2017-04-28 05:30:26

标签: codeigniter phpmailer mpdf

我有用mpdf库创建的pdf文件,我想用PHPMailer将输出文件作为附件发送到电子邮件中。我试过这段代码但仍然无法正常工作。附件丢失,但电子邮件已成功发送。

这是我在控制器中的代码:

    public function pdf_file()
    {
        $this->load->library('m_pdf');

        $this->data['title']="MY PDF TITLE 1.";
        $this->data['description']="Test PDF File";

        $html=$this->load->view('frontend/pdf_output',$this->data, true);

        $pdfFilePath = "the_pdf_output.pdf";

        $pdf = $this->m_pdf->load();

        $pdf->SetProtection(array('copy'), '123456', '123456', 128);
        $pdf->SetVisibility('screenonly');

        $pdf->WriteHTML($html,2);

        //$pdf->Output($pdfFilePath, "D");
        $content = $pdf->Output('', 'S');
        $content = chunk_split(base64_encode($content));

        $subject = "PDF File";
        $message = $this->load->view('frontend/pdf_output',$this->data,TRUE);
        $from = "aaa@yahoo.com";
        $to = 'xxx@gmail.com';
        $cc = null;
        $attachment = $content;
        $sender = "Administrator";

        $site_url = site_url();
        if ($site_url == 'http://localhost/mysite/') 
        {
            $this->load->view('frontend/email',$data);
        }
        else
        {
            $this->booking_model->send_email($subject,$message,$from,$to,$cc,$attachment,$sender);
        }
    }

这是我的PHPMailer配置:

    function send_email($subject,$message,$from,$to,$cc,$attachment,$sender)
    {
        $this->load->library('PHPMailerAutoload');
        $mail = new PHPMailer;
            $mail->isSMTP();

            $mail->SMTPDebug = 0;
            $mail->Debugoutput = 'html';

            $mail->SMTPOptions = array(
            'ssl' => array(
                'verify_peer' => false,
                'verify_peer_name' => false,
                'allow_self_signed' => true
            )
        );

            $mail->Host = "mail.host.com";
            $mail->IsHTML(true);
            $mail->Port = 26;
            $mail->SMTPAuth = true;
            //$mail->SMTPSecure = 'ssl';
            $mail->Username = "aaa@domain.com";
            $mail->Password = "123456";

            $mail->setFrom($from, $sender);
            if (count($cc) > 1) 
        {
            for ($i=1; $i <= count($cc); $i++) 
            {
                $mail->addCC($cc[$i],  $sender);
            }
        }


        if (!empty($attachment)) 
        {
            $mail->addAttachment($attachment);
        }

            $mail->addAddress($to);
            $mail->Subject = $subject;

            $mail->msgHTML($message);

            //send the message, check for errors
            if (!$mail->send()) {
                // failed
            } else {
                // success
            }
    }

有人知道在电子邮件中发送pdf附件吗?请帮忙。

0 个答案:

没有答案