2011-05-26 145 views
1

是否可以從臨時目錄發送附件? 當我送我收到郵件的print_r($_FILES)作爲在codeigniter中發送電子郵件

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

和我的錯誤是無法找到以下電子郵件附件:/tmp/php2UaLKE/EasyToEat.doc

和我的發言,如:

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

我想知道的是,可以附加一個文檔,而無需上傳,要指定位置笨的服務器上的郵件?

回答

1

我其實只是做了這個...它有一個比你需要更多,但它確實是這樣。將文件寫入臨時目錄,然後通過電子郵件發送。


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('[email protected]', '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('[email protected]', '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();     
      } 

     } 
    } 
+0

我找的,這並不需要上傳文件到任何位置的代碼。 – 2011-05-27 03:18:49

0

你忘了一兩件事:

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

文件路徑是你的陣列的 「tmp_name的值」(不是 「名稱」)。
試試這個:

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

希望這有助於...

+0

在這裏,我收到附件名稱爲phpa23mPB的郵件。但是當我保存時,我無法打開該文件。 – 2011-05-27 03:17:51

+1

嘗試將文件移至其他目標。 _move_uploaded_file($ _ FILES [「file」] [「tmp_name」],'path/to/directory /'。$ _FILES [「file」] [「name」]); _。那麼你應該可以使用該文件。 – hoferm 2011-05-27 08:36:04

相關問題