Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Attach file in email in Cakephp 2

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.56k
    Comment on it

    Attach images/pdf/doc to email using Cakephp 2

     

    Hello friends, I am writing this blog, which will help you to attach files to email using Cakephp 2.x. So lets begin with creating function in UsersController or any other controller and write the following lines of code below:

     

    $from='amukmca@gmail.com';
    $to='amuk.saxena@evontech.com';
    $data=array('message'=>'Hello');
    
    $attachments = array(
    'invoice.pdf' 	=> array(
    'file' 		=> '/var/www/cakeproject/app/webroot/files/pdf/invoice.pdf',
    'mimetype' 	=> 'pdf',
    'contentId' 	=> 'invoice_id'
    ));                                                  
    
    $this->_sendemail($from,$to, 'Subject Title',$data,'invoice',$attachments);


    Now create a function in AppController  _sendemail() as shown below:

     

    public function _sendemail($from, $to, $subject, $data = array(), $template = '',$attachments=array()) {
            $this->layout = false;
            $this->render(false);
            App::uses('CakeEmail', 'Network/Email');
            $Email = new CakeEmail();
            $Email->from(array($from => 'Amuk'));
            $Email->to($to);
            $Email->subject($subject);
            $Email->template($template, null);
            $Email->emailFormat('html');
            if(!empty($attachments)){
                $Email->attachments($attachments);
            }
            $Email->viewVars($data);
            $Email->send();
        }

     

    There are many ways to attach files in Cakephp. You can also use the following code to attach the file

     

    1) String Method:  $Email->attachments('/var/www/cakeproject/app/webroot/files/pdf/invoice.pdf') will attach this file with the name invoice.pdf.

     

    2) Array Method: $Email->attachments(array('/var/www/cakeproject/app/webroot/files/pdf/invoice.pdf') will have the same behavior as using a string.

     

    3) Passing Key in Array: $Email->attachments(array('invoice.pdf' => '/var/www/cakeproject/app/webroot/files/pdf/invoice.pdf')) will attach invoice.pdf with the name invoice.pdf.

     

    4) Nested Arrays:

    $Email->attachments = array('invoice.pdf'     => array(
    'file'         => '/var/www/cakeproject/app/webroot/files/pdf/invoice.pdf',
    'mimetype'     => 'pdf',
    'contentId'     => 'invoice_id'
    ));

     

    For more details you can click on the link : http://book.cakephp.org/2.0/en/core-utility-libraries/email.html#sending-attachments

     

    Thanks for reading

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: