Codeigniter向多个电子邮件ID发送电子邮件,文件附件不与电子邮件一起发送

时间:2017-08-29 12:28:19

标签: php codeigniter email

我正在尝试通过表单向多个用户发送带有附件的电子邮件,在表单中,我选择了电子邮件ID并附加了单个文件。电子邮件正在运行但没有附加文件,而文件正在上传给定目录。我认为路径有问题,请帮忙。

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 个答案:

答案 0 :(得分:1)

远射:

变化

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

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

正如您所说文件正确上传,可能文件名错误:

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

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

如果它被删除,我会删除,我只是快速尝试。