1

I have pdf file created with mpdf library and I want to send the output file as attachment in email with PHPMailer. I have tried this code but still not working. The attachment is missing but the email is successfully sent.

This is my code in controller :

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

This is my PHPMailer configuration :

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

Anyone know to send the pdf attachment on email ? Please help.

Antonio
  • 755
  • 4
  • 14
  • 35
  • Duplicate of https://stackoverflow.com/questions/15693924/codeigniter-send-pdf-file-as-email-attachment and others. – Finwe Apr 28 '17 at 11:50
  • Possible duplicate of [codeigniter send pdf file as email attachment](http://stackoverflow.com/questions/15693924/codeigniter-send-pdf-file-as-email-attachment) – Finwe Apr 28 '17 at 11:50

0 Answers0