How to send email using HTML templates in Codeigniter?

Introduction

Sending emails using an HTML template allows you to create professional and reusable email layouts for your CodeIgniter application. Instead of writing the complete HTML content directly inside the controller, you can create a separate view file and load it while sending the email.

In this article, we will explain how to send an email using an HTML template in CodeIgniter.

Prerequisites

Before proceeding, make sure you have:

  1. A working CodeIgniter application.
  2. Access to the CodeIgniter application files.
  3. A valid email account for sending emails.
  4. Basic knowledge of PHP and CodeIgniter.
  5. A configured mail server or SMTP service.

Implementation

Step 1: Create the HTML Email Template

First, create an HTML file according to your email template.

Create the following file:

/application/views/emails/test-email.php

Add the HTML content:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>CodeIgniter Mail Templates</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
</head>
<body>

    <p style="Margin-top: 0;color: #565656;font-family: Georgia,serif;font-size: 16px;line-height: 25px;Margin-bottom: 25px">
        Hey <?php echo $userName; ?>,
    </p>

    <p style="Margin-top: 0;color: #565656;font-family: Georgia,serif;font-size: 16px;line-height: 25px;Margin-bottom: 25px">
        This is a demo email showing how to send emails using HTML templates in CodeIgniter.
    </p>

</body>
</html>

The $userName variable will be populated dynamically from the controller.

Step 2: Create the Controller

Create a controller named sendmails.php.

Path:

/application/controllers/sendmails.php

Add the following code:

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

class Sendmails extends CI_Controller {

    public function htmlmail()
    {
        $userEmail = 'email@domain.com';
        $subject = 'Test';

        $config = array(
            'mailtype' => 'html',
            'charset'  => 'utf-8',
            'priority' => '1'
        );

        $this->load->library('email', $config);
        $this->email->set_newline("\r\n");

        $this->email->from('your-mail-id', 'Pheonix Solutions');

        $data = array(
            'userName' => 'Pheonix Solutions'
        );

        $this->email->to($userEmail);
        $this->email->subject($subject);

        $body = $this->load->view(
            'emails/test-email.php',
            $data,
            TRUE
        );

        $this->email->message($body);
        $this->email->send();
    }
}

?>

In the above code:

  • $userEmail specifies the recipient’s email address.
  • $subject specifies the email subject.
  • $config configures the email as HTML.
  • $data contains the dynamic values that will be passed to the HTML template.
  • $this->load->view() loads the email template.
  • $this->email->message() sets the generated HTML as the email body.
  • $this->email->send() sends the email.

Step 3: Call the Controller Method

Once the controller and HTML template are configured, call the controller method using the following URL:

http://[domain-name]/Sendmails/htmlmail

Replace [domain-name] with your application’s domain name.

If the email configuration is correct, the HTML email will be sent to the configured recipient.

Conclusion

In this article, we explained how to send emails using an HTML template in CodeIgniter. By keeping the email HTML in a separate view file, you can easily maintain and reuse email templates while passing dynamic data from the controller.

FAQs

1. Can I use dynamic values in the HTML email template?

Yes. You can pass dynamic values to the template using an array:

$data = array(
'userName' => 'Pheonix Solutions'
);

The value can then be accessed in the view using:





2. Where should the HTML email template be stored?

The template can be stored inside the CodeIgniter views directory, for example:

/application/views/emails/test-email.php

3. How do I enable HTML emails in CodeIgniter?

Set the mailtype configuration to html:

'mailtype' => 'html'

Related Article

How to add Google ReCAPTCHA in PHP Codeigniter – Pheonix Solutions

UPLOAD FILE USING CODEIGNITER FRAMEWORK – Pheonix Solutions

Talk to our experts

Have a technology challenge or looking for the right solution for your business? Our team can help you with cloud, DevOps, development, infrastructure, design, and more. Feel free to reach out to our experts here.

admin

Our team has expertise across software and web development, WordPress, e-commerce, mobile applications, UI/UX, cloud and infrastructure, DevOps, CI/CD, API integration, security, testing, automation, and technical support. The team also works with AI-based software solutions, LLMs, AI workflows, AI agents, and intelligent application development to help businesses automate processes and build smarter digital solutions. We focus on developing, deploying, maintaining, and optimising secure, scalable, and reliable technology solutions while helping businesses adopt modern technologies and drive digital transformation.

2 thoughts on “How to send email using HTML templates in Codeigniter?”

  1. Thank you! it’s worked well.
    but no styling.

    I used a simple Bootstrap page template. but not styles or color applied.

    anyway, thank you

Leave a Reply

Scroll to Top