codeigniter多个文件归档通过电子邮件发送

时间:2018-03-10 00:53:06

标签: php codeigniter email sendmail

如何在电子邮件附件中发送多个文件。

视图文件中的

  <input type="file" name="attachment" id="file_1" />
  <input type="file" name="attachmenttwo" id="file_2" />
  <input type="file" name="attachmentthree" id="file_3" />

在我的控制器中

 $config['upload_path'] = './uploads/';
 $config['allowed_types'] = 'gif|jpg|png|txt';
 $config['max_size'] = '100000';
 $config['overwrite'] = TRUE;
 $config['encrypt_name'] = TRUE;
 $this->load->library('upload', $config);
 $this->load->library('email');
 $this->load->library('encrypt');
 $this->upload->initialize($config);
 $this->upload->do_upload('attachment');
 $this->upload->do_upload('attachmenttwo');
 $this->upload->do_upload('attachmenthree');
 $ret = $this->upload->data();
 $rettwo = $this->upload->data();
 $retthree = $this->upload->data();
 $pathToUploadedFile = $ret['full_path'];
 $pathToUploadedFiletwo = $rettwo['full_path'];
 $pathToUploadedFilethree = $retthree['full_path'];
 $this->email->from('abc@gmail');
 $this->email->to('bcd@gmail.com');
 $this->email->subject('New query');
 $this->email->message('hi');
 $this->email->attach($pathToUploadedFile);
 $this->email->attach($pathToUploadedFiletwo);
 $this->email->attach($pathToUploadedFilethree);
 $this->email->send();

这里是我能够在服务器上成功上传文件但无法在电子邮件中发送附件,我在收件箱中收到了最后一次文件3次。

建议我如何在邮件收件箱中发送所有文件

3 个答案:

答案 0 :(得分:1)

// Load uploader library

$config['upload_path'] = '/usr/local/var/www/Test/ci/uploads/';
$config['allowed_types'] = 'txt|pdf';
$config['overwrite'] = TRUE;
$config['encrypt_name'] = TRUE;

$this->load->library('upload');
$this->upload->initialize($config);

// load email library
$configmail = Array(
    'protocol' => 'smtp',
    'smtp_host' => 'ssl://smtp.googlemail.com',
    'smtp_port' => 465,
    'smtp_user' => 'abc@gmail.com', 
    'smtp_pass' => 'passwrd', 
    'mailtype' => 'html',
    'charset' => 'iso-8859-1',
    'wordwrap' => TRUE
);

$this->load->library('email', $configmail);
$this->email->set_newline("\r\n");
$this->email->from('abc@gmail.com');
$this->email->to($email);
$this->email->subject($subject);
$this->email->message($message);

foreach ($_FILES as $key => $value) {

    if (!empty($key['userfile'])) {

        if (!$this->upload->do_upload($key)) { 
          // show error or something you want
        } else { 
            // attache file in email
            $upload_data = $this->upload->data();
            $this->email->attach($upload_data['full_path']);
        }
    }
}

// finally send email
$this->email->send();

你的HTML应该是这样的

归档一个

<input type="file" name="userfile[]" id="file_1" /> 

文件二

<input type="file" name="userfile[]" id="file_2" /> 

文件三

<input type="file" name="userfile[]" id="file_3" /> 

答案 1 :(得分:0)

您可以对这些文件进行比较吗?

let rec length l =
  match l with
    [] -> 0
  | _ :: t -> 1 + length t;;

let rec take_element l n =
  match l with
    [] -> raise Not_found
  | h::t -> if n > 1 then take_element t (n-1)
            else h;;

let takemiddle l  = take_element l (length l/2 + 1);;

let rec take n l =
  if n = 0 then [] else
    match l with
      h::t -> h:: take (n-1) t;;

let takefront l = take (length l/2) l;;

let rec drop n l =
  if n = 0 then l else
    match l with
      h::t -> drop (n-1) t;;

let takeback l = drop (length l/2 + 1) l;;

let rec tree_of_list l =
  match l with
  [] -> Lf
| h::t -> Br((takemiddle l), tree_of_list (takefront l), tree_of_list (takeback l));;

通过这种方式:

$pathToUploadedFile = $ret['full_path'];
$pathToUploadedFiletwo = $rettwo['full_path'];
$pathToUploadedFilethree = $retthree['full_path'];

如果这些路径相同,那么在获取提交表单的文件时可能会出错。

答案 2 :(得分:0)

使用此代码

首先我们需要在名称服务器上传数据 然后我们追加文件名 然后我们创建循环然后发送电子邮件

 if($this->input->post('attachment') && !empty($_FILES['userFiles']['name'])){
      $filesCount = count($_FILES['userFiles']['name']);
        for($i = 0; $i < $filesCount; $i++){
            $_FILES['userFile']['name'] = $_FILES['userFiles']['name'][$i];
            $_FILES['userFile']['type'] = $_FILES['userFiles']['type'][$i];
            $_FILES['userFile']['tmp_name'] = $_FILES['userFiles']['tmp_name'][$i];
            $_FILES['userFile']['error'] = $_FILES['userFiles']['error'][$i];
            $_FILES['userFile']['size'] = $_FILES['userFiles']['size'][$i];
            $uploadPath = './uploads/';
            $config['upload_path'] = $uploadPath;
           $config['allowed_types'] = 'gif|jpg|png|txt';

            $this->load->library('upload', $config);
            $this->upload->initialize($config);
            if($this->upload->do_upload('userFile')){
             $fileData = $this->upload->data();
             $this->load->library('email'); 
             $this->email->from('abc@gmail.com');
             $this->email->to('abc@gmail.com');
             $this->email->subject('New query');
             $this->email->message($message);
                $pathToUploadedFile = $fileData['full_path'];
                $this->email->attach($pathToUploadedFile);
            }
        }

      $this->email->send();
    }