在 codeigniter 中将文件附加到电子邮件

Attach a file to an email in codeigniter

这是我的代码,我想将多个文件附加到电子邮件中。 此代码正在运行并发送电子邮件,但我无法在电子邮件中附加附件。电子邮件标题和消息是用户输入的,发送电子邮件地址也是用户 input.In 这里我使用复选框 select 发送电子邮件的电子邮件地址。

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Sendemail extends CI_Controller {

  public function __construct()
  {
    /*call CodeIgniter's default Constructor*/
    parent::__construct();
    /*load database libray manually*/
    $this->load->database();
    $this->load->library('session');
    /*load Model*/
    $this->load->helper('url');
    
  }

   public function index() { 
      $this->load->view('Admin/dashboard'); 
   } 


  public function form_validation()  
      {  
           //echo 'OK';  
           $this->load->library('form_validation');  
           $this->form_validation->set_rules("title", "title", 'required');  
           $this->form_validation->set_rules("files[]", "files[]");  
           $this->form_validation->set_rules("message", "message", 'required');  
           $this->form_validation->set_rules("single_select[]", "single_select[]", 'required');  
           if ($this->form_validation->run()){

            if(isset($_POST['submit'])){ 
              $checkbox1=$_POST['single_select'];
                  foreach($checkbox1 as $chk1){ 
                      
                   
                    $this->load->library('email');
                    $this->email->to($chk1);
                    $this->email->from('xxxxxxxxxx@gmail.com');
                    $this->email->subject($this->input->post("title"));
                    $this->email->message($this->input->post("message"));
                    
                    $this->email->send();

                    
                }
                $this->session->set_flashdata('message','Email sent.');
                redirect('Admin/index');
            }
            else{
              $this->session->set_flashdata('message','Something went wrong!');
              redirect('Admin/index');
            }
        }

        else{
          $this->session->set_flashdata('message','Something went wrong!');
          redirect('Admin/index');
        }     
    } 
}```

您可以像这样附加多个文件

$this->email->attach('/path/to/photo1.jpg');
$this->email->attach('/path/to/photo2.jpg');
$this->email->attach('/path/to/photo3.jpg');