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:
- A working CodeIgniter application.
- Access to the CodeIgniter application files.
- A valid email account for sending emails.
- Basic knowledge of PHP and CodeIgniter.
- 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:
$userEmailspecifies the recipient’s email address.$subjectspecifies the email subject.$configconfigures the email as HTML.$datacontains 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.
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
you need to style it inline