Attachment in mail using php codeigniter

A common task!

To attach a file to an email using PHP and CodeIgniter, you can use the following steps:

Step 1: Prepare the email

Create a new email message using the email library in CodeIgniter. You can do this by creating a new instance of the CI_Email class and setting the necessary properties:

$email = &get_instance()->email;
$email->from('[email protected]', 'Your Name');
$email->to('[email protected]', 'Recipient Name');
$email->subject('Email Subject');

Step 2: Attach the file

Use the attach method to attach the file to the email:

$file_path = 'path/to/your/file.pdf'; // replace with the actual file path
$file_name = 'file.pdf'; // replace with the actual file name
$email->attach($file_path, $file_name, 'application/pdf');

The attach method takes three arguments:

Step 3: Send the email

Finally, send the email using the send method:

$email->send();

Here's the complete code:

<?php

defined('BASEPATH') or exit('No direct script access allowed');

class Email extends CI_Controller {

    public function send_email()
    {
        $email = &get_instance()->email;
        $email->from('[email protected]', 'Your Name');
        $email->to('[email protected]', 'Recipient Name');
        $email->subject('Email Subject');

        $file_path = 'path/to/your/file.pdf'; // replace with the actual file path
        $file_name = 'file.pdf'; // replace with the actual file name
        $email->attach($file_path, $file_name, 'application/pdf');

        $email->send();
    }
}

Make sure to replace the placeholders ([email protected], [email protected], path/to/your/file.pdf, and file.pdf) with the actual values.

That's it! With this code, you should be able to attach a file to an email using PHP and CodeIgniter.