ddl
ddl

Reputation: 329

Attach form-file to email, Codeigniter

I need to get file from user and attach it to mail without saving it on server. Is this possible in Codeigniter to store uploaded file in variable and then attach it to email from Codeigniter email library? How to achieve that? In CI I'm only finding classes to store uploaded files on hard drive not in variable.

Upvotes: 0

Views: 3487

Answers (2)

Kuldeep Singh
Kuldeep Singh

Reputation: 471

Try this one

https://www.codeigniter.com/user_guide/libraries/email.html

It works well. I have tried this in my recent project

Upvotes: -1

Nithyanandhan M
Nithyanandhan M

Reputation: 1554

First you have to upload the file to server directory then pass the name and path of file to email attach line i.e.,

    **$this->email->attach('/path/to/file.ext');**

See the below code for upload class and email library.

    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '100';
    $config['max_width']  = '1024';
    $config['max_height']  = '768';

    $this->load->library('upload', $config);

    if ( ! $this->upload->do_upload())
    {
        $error = array('error' => $this->upload->display_errors());

    }
    else
    {
        $data = array('upload_data' => $this->upload->data());

    $this->load->library('email');

    $this->email->from('[email protected]', 'Your Name');
    $this->email->to('[email protected]');
    $this->email->cc('[email protected]');
    $this->email->bcc('[email protected]');

    $this->email->subject('Email Test');
    $this->email->attach('/path/to/file.ext');
    $this->email->message('Testing the email class.');

    $this->email->send();

    }

Upvotes: 3

Related Questions