codeigniter 多个文件提交在电子邮件中发送

codeigniter multiple file filed send in email

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

在视图文件中

  <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 次。

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

// 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" /> 

你能对这些文件进行比较吗?

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

通过这种方式:

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

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

使用此代码

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

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