【问题标题】:codeigniter multiple file filed send in emailcodeigniter 多个文件归档在电子邮件中发送
【发布时间】:2018-03-10 00:53:06
【问题描述】:

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

在视图文件中

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

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

【问题讨论】:

    标签: php codeigniter email sendmail


    【解决方案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" /> 
    

    【讨论】:

    • 这是你的输入文件名,比如
    • 使用上面的代码你只需要改变你的html如下
    • 检查您的上传路径是否正确上传文件?
    • 在这一行修改你的文件上传配置 $config['upload_path'] = '/usr/local/var/www/Test/ci/uploads/'; (更改应用程序的路径,例如“/uploads”,并确保该目录存在。
    • 我更改了所有内容,但我的服务器中没有文件上传我使用 $config['upload_path'] = $_SERVER['DOCUMENT_ROOT']."/uploads/";我用这个但没有文件上传我想请提出其他建议
    【解决方案2】:

    使用此代码

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

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

    【讨论】:

      【解决方案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);
      

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

      【讨论】:

      • var_dump 这些变量。确保它们是不同的路径。
      • 请举个例子
      • 更新了我的评论。
      猜你喜欢
      • 2018-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-13
      • 2013-01-07
      • 2019-08-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多