{"id":1525,"date":"2017-05-24T17:21:16","date_gmt":"2017-05-24T11:51:16","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=1525"},"modified":"2026-09-14T13:47:12","modified_gmt":"2026-09-14T08:17:12","slug":"send-email-using-html-templates-codeigniter","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/send-email-using-html-templates-codeigniter\/","title":{"rendered":"How to send email using HTML templates in Codeigniter?"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this article, we will explain how to <strong>send an email using an HTML template in CodeIgniter<\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Before proceeding, make sure you have:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>A working CodeIgniter application.<\/li>\n\n\n\n<li>Access to the CodeIgniter application files.<\/li>\n\n\n\n<li>A valid email account for sending emails.<\/li>\n\n\n\n<li>Basic knowledge of PHP and CodeIgniter.<\/li>\n\n\n\n<li>A configured mail server or SMTP service.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Implementation<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Create the HTML Email Template<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">First, create an HTML file according to your email template.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Create the following file:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/application\/views\/emails\/test-email.php<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Add the HTML content:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;!DOCTYPE html>\n&lt;html>\n&lt;head>\n    &lt;meta charset=\"utf-8\" \/>\n    &lt;title>CodeIgniter Mail Templates&lt;\/title>\n    &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" \/>\n&lt;\/head>\n&lt;body>\n\n    &lt;p style=\"Margin-top: 0;color: #565656;font-family: Georgia,serif;font-size: 16px;line-height: 25px;Margin-bottom: 25px\">\n        Hey &lt;?php echo $userName; ?>,\n    &lt;\/p>\n\n    &lt;p style=\"Margin-top: 0;color: #565656;font-family: Georgia,serif;font-size: 16px;line-height: 25px;Margin-bottom: 25px\">\n        This is a demo email showing how to send emails using HTML templates in CodeIgniter.\n    &lt;\/p>\n\n&lt;\/body>\n&lt;\/html><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>$userName<\/code> variable will be populated dynamically from the controller.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Create the Controller<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create a controller named <code>sendmails.php<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Path:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/application\/controllers\/sendmails.php<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Add the following code:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php if (!defined('BASEPATH')) exit('No direct script access allowed');\n\nclass Sendmails extends CI_Controller {\n\n    public function htmlmail()\n    {\n        $userEmail = 'email@domain.com';\n        $subject = 'Test';\n\n        $config = array(\n            'mailtype' => 'html',\n            'charset'  => 'utf-8',\n            'priority' => '1'\n        );\n\n        $this->load->library('email', $config);\n        $this->email->set_newline(\"\\r\\n\");\n\n        $this->email->from('your-mail-id', 'Pheonix Solutions');\n\n        $data = array(\n            'userName' => 'Pheonix Solutions'\n        );\n\n        $this->email->to($userEmail);\n        $this->email->subject($subject);\n\n        $body = $this->load->view(\n            'emails\/test-email.php',\n            $data,\n            TRUE\n        );\n\n        $this->email->message($body);\n        $this->email->send();\n    }\n}\n\n?><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In the above code:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>$userEmail<\/code> specifies the recipient&#8217;s email address.<\/li>\n\n\n\n<li><code>$subject<\/code> specifies the email subject.<\/li>\n\n\n\n<li><code>$config<\/code> configures the email as HTML.<\/li>\n\n\n\n<li><code>$data<\/code> contains the dynamic values that will be passed to the HTML template.<\/li>\n\n\n\n<li><code>$this->load->view()<\/code> loads the email template.<\/li>\n\n\n\n<li><code>$this->email->message()<\/code> sets the generated HTML as the email body.<\/li>\n\n\n\n<li><code>$this->email->send()<\/code> sends the email.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Call the Controller Method<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Once the controller and HTML template are configured, call the controller method using the following URL:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">http:\/\/[domain-name]\/Sendmails\/htmlmail<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Replace <code>[domain-name]<\/code> with your application&#8217;s domain name.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If the email configuration is correct, the HTML email will be sent to the configured recipient.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In this article, we explained how to send emails using an <strong>HTML template in CodeIgniter<\/strong>. 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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">FAQs<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. Can I use dynamic values in the HTML email template?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. You can pass dynamic values to the template using an array:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$data = array(<br>    'userName' => 'Pheonix Solutions'<br>);<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The value can then be accessed in the view using:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. Where should the HTML email template be stored?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The template can be stored inside the CodeIgniter views directory, for example:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/application\/views\/emails\/test-email.php<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. How do I enable HTML emails in CodeIgniter?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Set the <code>mailtype<\/code> configuration to <code>html<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">'mailtype' => 'html'<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Related Article<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/how-to-add-google-recaptcha-in-php-codeigniter\/\">How to add Google ReCAPTCHA in PHP Codeigniter &#8211; Pheonix Solutions<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/upload-file-using-codeigniter-framework\/\">UPLOAD FILE USING CODEIGNITER FRAMEWORK &#8211; Pheonix Solutions<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Talk to our experts<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Have a technology challenge or looking for the right solution for your business? Our team can help you with&nbsp;<strong>cloud, DevOps, development, infrastructure, design, and more<\/strong>. Feel free to reach out to our experts&nbsp;<a href=\"https:\/\/pheonixsolutions.com\/contact\">here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Sending emails using an HTML template allows you to create professional and reusable email layouts for your CodeIgniter application. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1022],"tags":[341,273],"class_list":["post-1525","post","type-post","status-publish","format-standard","hentry","category-web-architecture","tag-codeigniter","tag-php","psol-cat-web-architecture"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-oB","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/1525","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=1525"}],"version-history":[{"count":1,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/1525\/revisions"}],"predecessor-version":[{"id":11749,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/1525\/revisions\/11749"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=1525"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=1525"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=1525"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}