Myat Zuckerberg
Myat Zuckerberg

Reputation: 85

CodeIgniter Send Email with image

I'm Beginner for the CI.

Here is my code in welcome.php

class Welcome extends CI_Controller {


public function index()
{
$this->load->helper('url');
    $this->load->view('welcome_message');
}
public function emailSend()
{
$this->load->library('upload');
$this->load->library('email');

$config['upload_path'] = 'uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$this->upload->initialize($config);







$this->email->from($this->input->post('from'), $this->input->post('name'));
$this->email->to('[email protected]');
//$this->email->cc('[email protected]');
//$this->email->bcc('[email protected]');

$this->email->subject($this->input->post('subject'));
$this->email->message($this->input->post('body'));

if($this->upload->do_upload())
{
    $attachdata=$this->upload->data();
$this->email->attach($attachdata['full_path']);
}

if($this->email->send())
    {
        echo 'Your email was sent, successfully.';
    }

    else
    {
        show_error($this->email->print_debugger());
    }

}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */

This code is only work for text and not for the attachment(image). The error is "Unable to send email using PHP mail(). Your server might not be configured to send mail using this method."

how can i fixed that error?? Help me Thz you.

Upvotes: 0

Views: 6946

Answers (3)

user2746367
user2746367

Reputation:

$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|jpeg|png|txt|php|pdf';
$config['max_size']         = '9000';
$config['encrypt_name']     = true;

$image_data = $this->upload->data();
$fname=$image_data['file_name'];
$fpath=$image_data['file_path'].$fname;

$this->email->attach($fpath);

The above code will solve your problem.The same problem was to me also. This is because the name of file you saved in folder is different as you attached the above code will solve it because it take the correct path of your uploads folder. Please note that the uploads folder should be in root.

Upvotes: 0

Muddasir Abbas
Muddasir Abbas

Reputation: 1819

        $config['charset'] = 'iso-8859-1';
        $config['wordwrap'] = TRUE;
        $config['mailtype'] = 'html';

        $this->email->initialize($config);
        $to = $this->input->post('to'); // [email protected]
        $cc = '[email protected]'; // [email protected]
        $subject = $this->input->post('subject');
        $message = $this->input->post('message');

        $this->email->to($to);
        $this->email->cc($cc);
        $this->email->from('[email protected]','Awan Developers');
        $this->email->subject($subject);
        $this->email->message($message);


        $config['upload_path'] = './attachments/'; // or you can use any folder like uploads
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '1000';
        $config['max_width']  = '1024';
        $config['max_height']  = '768'; 
        $config['encrypt_name']     = true;
        $this->load->library('upload', $config);

        if(!$this->upload->do_upload('files')) // files will be input attachment field name
        {
            $this->upload->display_errors();
        }else{
            $image_data = $this->upload->data();
            $fname=$image_data['file_name'];
            $fpath=$image_data['file_path'].$fname;
            $this->email->attach($fpath);

            if ($this->email->send()){
                echo "Mail Sent!";
            }
            else{
                echo "There is error in sending mail!";
            }
        }   

Upvotes: 1

Zabs
Zabs

Reputation: 14142

Try this from the official docs..

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

Upvotes: 0

Related Questions