CodeIgniter Email Sending

Sending emails is a very common feature in web applications such as contact forms, registration confirmation, password reset, and notifications.

CodeIgniter provides a powerful Email Class that makes sending emails simple using different protocols like Mail, SMTP, and Sendmail.

🔹 Step 1: Load the Email Library

Before sending emails, load the email library in your controller:

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


🔹 Step 2: Configure Email Settings

You can configure email settings in two ways:

Option 1: Load config inside Controller

$config['protocol']    = 'smtp';
$config['smtp_host']   = 'ssl://smtp.googlemail.com';
$config['smtp_port']   = '465';
$config['smtp_user']   = 'your_email@gmail.com';
$config['smtp_pass']   = 'your_password';
$config['mailtype']    = 'html'; 
$config['charset']     = 'utf-8';
$config['newline']     = "\r\n";  

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

Option 2: Store config in application/config/email.php

$config = array(
    'protocol'  => 'smtp',
    'smtp_host' => 'ssl://smtp.googlemail.com',
    'smtp_port' => 465,
    'smtp_user' => 'your_email@gmail.com',
    'smtp_pass' => 'your_password',
    'mailtype'  => 'html',
    'charset'   => 'utf-8',
    'newline'   => "\r\n"
);

Then load it with:

$this->load->config('email');
$this->email->initialize($this->config->item());


🔹 Step 3: Create Controller for Sending Email

Create a file application/controllers/EmailController.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class EmailController extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->library('email');
    }

    public function send_email() {
        // Email configuration
        $config['protocol']    = 'smtp';
        $config['smtp_host']   = 'ssl://smtp.googlemail.com';
        $config['smtp_port']   = '465';
        $config['smtp_user']   = 'your_email@gmail.com';
        $config['smtp_pass']   = 'your_password';
        $config['mailtype']    = 'html';
        $config['charset']     = 'utf-8';
        $config['newline']     = "\r\n";

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

        // Sender email
        $this->email->from('your_email@gmail.com', 'Your Website Name');
        // Receiver email
        $this->email->to('receiver@example.com');
        // CC & BCC
        $this->email->cc('cc@example.com');
        $this->email->bcc('bcc@example.com');

        // Email subject & message
        $this->email->subject('CodeIgniter Email Test');
        $this->email->message('<h3>This is a test email sent from CodeIgniter.</h3>');

        // Send email
        if ($this->email->send()) {
            echo "Email sent successfully!";
        } else {
            show_error($this->email->print_debugger());
        }
    }
}


🔹 Step 4: Send Email with Attachment

You can also attach files before sending:

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


🔹 Step 5: Sending Plain Text Email

$this->email->mailtype = 'text';
$this->email->message("This is a plain text email.");



Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.