Codeigniter sending email to multiple email ids, f

2019-06-10 22:33发布

问题:

I am trying to send email to multiple users with an attachment through a form, In the form,I am selecting email id's and attaching a single file. Emails are going but without the file attached, while file is uploading in the given directory. I think there is some problem with the path, Please help.

class Email extends MY_Controller
{
    public function __construct() {
        parent::__construct();
        $this->load->model('global_model');
        $this->load->library('email');     
        if(!$this->_is_logged_in('admin_id'))
        {
            _redirect('admin_login');
        }
    }

    public function send_newsletter()
    {
        $config = [
            'upload_path'   =>      './assets/email_documents',
            'allowed_types' =>      'jpg|gif|png|jpeg|doc|docx|pdf|xls|xlsx|ppt|pptx|txt',
            'max_size'      =>      '5120',
        ];
        $this->load->library('upload', $config);
        $this->upload->do_upload('filename');
        $data = $this->upload->data(); // To Upload the image
        $file_name = $data['file_name']."<br/>";
        $file_path = base_url("assets/email_documents/" . $data['raw_name'] . $data['file_ext']);



         $lists = $this->input->post('lists');
         $subject = $this->input->post('subject');
         //$message = $this->input->post('message');

         $join_str1 = "subscribers.subscriber_list_id=lists.list_id";

         $subscribers =  
         $this->global_model
         ->join_2table('subscribers','lists', $join_str1,['subscriber_list_id'=>$lists,'subscriber_status'=>'Active']); 



        foreach($subscribers as $row) {

            $email_lists = $row['subscriber_email'];
            $random_key = $row['random_key'];   
            $message = $this->input->post('message').
            "<a href=\"http://example.com/crm_alazizi/unsubscribe/unsubscribe_me/{$random_key}\">Unsubscribe Here</a>";

            $from_email = 'support@example.com';
            $this->email->from($from_email, 'CRM ALAZIZI'); 
            $this->email->to($email_lists);
            $this->email->subject($subject); 
            $this->email->message($message); 
            $this->email->set_mailtype('html');
            $this->email->attach('/assets/email_documents/'.$file_name);
            $sendmail = $this->email->send();

        }                 
            //Send mail 
            if($sendmail) 
            {
                $this->session->set_flashdata('msg','Email sent successfull.');
                _redirect_pre();
            } 
            else 
            {
                $this->session->set_flashdata('msg','Email sent Unsuccessfull,Please try again');
                _redirect_pre();
            }      
    }

回答1:

long shot:

change

$this->email->attach('/assets/email_documents/'.$file_name);

to

$this->email->attach(base_url .'assets/email_documents/'.$file_name);

As you say the file uploads correctly, maybe the file name is wrong:

from

$file_name = $data['file_name']."<br/>";
$file_path = base_url("assets/email_documents/" . $data['raw_name'] . $data['file_ext']);

to

$file_name = $data['raw_name'] . $data['file_ext'];
$file_path = base_url("assets/email_documents/" . $file_name);

If it gets downvoted I'll delete, I just tried quickly.