简体   繁体   English

Codeigniter多个文件归档发送电子邮件

[英]codeigniter multiple file filed send in email

How to send multiple file filed in email attachment. 如何发送电子邮件附件中归档的多个文件。

in views file 在视图文件中

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

in my controller 在我的控制器中

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

here is i am able to upload file in server successfully but not able to send attachment in email, i received last file 3 time in my inbox. 这是我能够成功上传服务器中的文件,但无法通过电子邮件发送附件,我在收件箱中收到了3次最新文件。

suggest me how to send all files in mail inbox 建议我如何发送收件箱中的所有文件

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

and your html should look like this 和您的html应该看起来像这样

File one 档案一

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

File two 档案二

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

File three 文件三

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

Can you make the comparison of these files? 您可以对这些文件进行比较吗?

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

By this way: 通过这种方式:

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

If these are the same paths, maybe you have wrong when getting files of the submitted form. 如果这些路径相同,则获取提交表单的文件时可能出错。

use this code 使用此代码

first we need to upload data on server with name then we append name of files then we make create loop then send email 首先,我们需要使用名称将数据上传到服务器上,然后附加文件名,然后进行创建循环,然后发送电子邮件

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM