How send mail in codeignitor

In CodeIgniter, you can send mail using the email library, which is a built-in library in CodeIgniter. Here's a step-by-step guide on how to send mail in CodeIgniter:

Step 1: Load the Email Library

In your controller, load the email library by adding the following line of code:

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

Step 2: Set the Mail Configuration

Set the mail configuration by creating a config/email.php file and adding the following code:

<?php
$config['protocol'] = 'smtp'; // or 'sendmail' or 'mail'
$config['smtp_host'] = 'your_smtp_host';
$config['smtp_user'] = 'your_smtp_username';
$config['smtp_pass'] = 'your_smtp_password';
$config['smtp_port'] = 587; // or 25

Replace the placeholders with your actual SMTP settings.

Step 3: Set the Mail Message

Create a new instance of the CI_Email class and set the mail message properties:

$email = &get_instance()->email;
$email->from('your_from_email', 'Your Name');
$email->to('recipient_email');
$email->subject('Subject of the email');
$email->message('Body of the email');

Step 4: Send the Mail

Send the mail using the send() method:

$email->send();

Example Code

Here's an example code snippet that demonstrates how to send a mail in CodeIgniter:

<?php

class Mail extends CI_Controller {

    public function send_email()
    {
        $this->load->library('email');

        $config['protocol'] = 'smtp';
        $config['smtp_host'] = 'your_smtp_host';
        $config['smtp_user'] = 'your_smtp_username';
        $config['smtp_pass'] = 'your_smtp_password';
        $config['smtp_port'] = 587;

        $this->email->initialize($config);

        $this->email->from('your_from_email', 'Your Name');
        $this->email->to('recipient_email');
        $this->email->subject('Subject of the email');
        $this->email->message('Body of the email');

        if ($this->email->send()) {
            echo 'Email sent successfully!';
        } else {
            echo 'Error sending email!';
        }
    }
}

Tips and Variations