在codeigniter中发送电子邮件

时间:2011-05-26 06:58:03

标签: php codeigniter

是否可以从临时目录发送附件? 当我发送邮件时,我将print_r($_FILES)作为

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

我想知道是否可以将文档附加到邮件而无需将其上传到codeigniter中服务器上的指定位置?

3 个答案:

答案 0 :(得分: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('donotreply@email', 'Admin');
                $this->email->to('peter@email.com'); 

                $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('peter@email.com'); 

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

        }
    }

答案 1 :(得分:0)

你忘了一件事:

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

文件路径是数组的“tmp_name”(不是“name”) 试试这个:

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

希望这会有所帮助......

答案 2 :(得分:0)

您无法直接从输入文件字段附加文件。请参考以下答案

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