How to attech images in mail in codeigniter
In CodeIgniter, you can attach images to an email using the email
library. Here's an example of how to do it:
Step 1: Load the email library
In your controller, load the email library:
$this->load->library('email');
Step 2: Set the email configuration
Set the email configuration, including the sender's email address, recipient's email address, and the subject of the email:
$this->email->from('[email protected]', 'Your Name');
$this->email->to('[email protected]');
$this->email->subject('Email with attachment');
Step 3: Set the attachment
Set the attachment using the attach()
method. You can pass the file path and the file name as arguments:
$this->email->attach('/path/to/image.jpg', 'image.jpg');
Step 4: Send the email
Send the email using the send()
method:
$this->email->send();
Here's the complete code example:
<?php
class MyController extends CI_Controller {
public function send_email_with_attachment()
{
$this->load->library('email');
$this->email->from('[email protected]', 'Your Name');
$this->email->to('[email protected]');
$this->email->subject('Email with attachment');
$this->email->attach('/path/to/image.jpg', 'image.jpg');
$this->email->send();
}
}
Tips:
- Make sure the file path is correct and the file exists.
- You can attach multiple files by calling the
attach()
method multiple times. - You can also set the attachment type (e.g.,
image/jpeg
) using the third argument of theattach()
method. - If you're using a CI 3.x, you need to use
CI_Email
instead ofemail
.