Nisha haridas
Nisha haridas

Reputation: 332

email sending in codeigniter

Is it possible to send an attachment from temporary directory? When I send the mail I am getting the print_r($_FILES) as

Array ( [file] => Array ( [name] => test.doc 
        [type] => application/msword 
        [tmp_name] => /tmp/php2UaLKE 
        [error] => 0 [size] => 681472 ) 
      ) 

and my error is Unable to locate the following email attachment: /tmp/php2UaLKE/EasyToEat.doc

and my statement like:

$attachment=$_FILES['file']['tmp_name'].'/'.$_FILES['file']['name'];        
$this->email->attach($attachment);

I want to know is it possible to attach an document to mail without uploading that to specified location on the server in codeigniter?

Upvotes: 2

Views: 2634

Answers (3)

saad
saad

Reputation: 1364

You cannot directly attach a file from input file field. Please refer to following answer

https://stackoverflow.com/a/3628203/3377733

Upvotes: 1

hoferm
hoferm

Reputation: 209

You forgot one thing:

$attachment=$_FILES['file']['tmp_name'].'/'.$_FILES['file']['name'];        
$this->email->attach($attachment);

The file path is the "tmp_name" of your array (not with "name").
Try this:

$this->email->attach($_FILES['file']['tmp_name']);

Hope this helps...

Upvotes: 1

Peter
Peter

Reputation: 2306

I actually just did this... Its got a lil more than you need, but it does just that. Writes a file to a temp directory, then emails it.


function send_weekly_report()
    {
        $server_ip = $_SERVER['REMOTE_ADDR'];
        if($server_ip != '127.1.1.1')
        {
            $this->load->model('admin_model');
            $this->load->helper('csv_helper');
            $this->load->helper('file');
            //create CSV Array
            $header = array("Sales Rep", "Client", "Action Taken", "Won or Lost", "Action Why", "Current Vendor", "Comp. Cal Program", "Comp. Cal Date", "Notes", "Time");  
            $data = $this->admin_model->load_week();          
            $output = array_merge(array($header), $data);

            $csv = array_to_csv($output);
            $filename = '/tmp/'.time().".csv";
            if ( ! write_file($filename, $csv))
            {
                 $this->load->library('email');
                $this->email->from('donotreply@email', 'Admin');
                $this->email->to('[email protected]'); 

                $this->email->subject('Weekly Sales Test FAIL!!!!!');
                $this->email->message('Weekly Report Failed!');   
                $this->email->send();
            }
            else
            {
                 //send email
                 $this->load->library('email');
                $this->email->from('donotreply@email', 'Admin');
                $this->email->to('[email protected]'); 

                $this->email->subject('Weekly Sales Test');
                $this->email->message('Please find the attached report.');    
                $this->email->attach($filename);
                $this->email->send();

                //echo $this->email->print_debugger();                 
            }

        }
    }

Upvotes: 1

Related Questions