For any queries you can reach us at / WhatsApp us: +919158876092

Send Email using PHPMailer in CodeIgniter


In this example, I am going to show you how to send Email using PHPMailer in CodeIgniter.

For sending Email in CodeIgniter, we use an external library PHPMailer. By using composer you can download this library into your project.

You need to run following command in your project's root directory.


composer require phpmailer/phpmailer

Here, we are using 3 files for generating PDF file:

Controller: 	Email.php           application\controllers\Email.php
View:		compose.php         application\views\compose.php


$config['composer_autoload'] = FALSE;
// to
$config['composer_autoload'] = TRUE;


defined('BASEPATH') OR exit('No direct script access allowed');
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

class Email extends CI_Controller {
    public function __construct() {
    public function compose() {
    public function send_email() {
        $email          = $this->input->post('email');
        $subject        = $this->input->post('subject');
        $message        = $this->input->post('message');
        $mail = new PHPMailer(true);  
		try {
		    $mail->Host         = 'host'; //
		    $mail->SMTPAuth     = true;     
		    $mail->Username     = 'username';  
		    $mail->Password     = 'password';
			$mail->SMTPSecure   = 'tls';  
			$mail->Port         = 587;  
			$mail->Subject      = $subject;
			$mail->Body         = $message;
			$mail->setFrom('username', 'display_name');
			if(!$mail->send()) {
			    echo "Something went wrong. Please try again.";
		    else {
			    echo "Email sent successfully.";
		} catch (Exception $e) {
		    echo "Something went wrong. Please try again.";


<form action="<?php echo base_url('email/send_email') ?>" class="form-horizontal" id="add_email_form" autocomplete="off" method="post" accept-charset="utf-8">
    <div class="form-group">
        <label for="inputName" class="col-sm-2 control-label">Recipient Email</label>
        <div class="col-sm-10">
            <input type="hidden" name="id" value="9" id="id" />
            <input type="hidden" name="source_id" value="2" id="source_id" />
            <input type="text" name="email" value="" id="email" placeholder="Recipient Email Address" class="form-control" required />
    <div class="form-group">
        <label for="inputName" class="col-sm-2 control-label">Subject</label>
        <div class="col-sm-10">
            <input type="text" name="subject" value="" id="subject" placeholder="Subject" class="form-control" required />
    <div class="form-group">
        <label for="inputName" class="col-sm-2 control-label">Message</label>
        <div class="col-sm-10">
            <textarea class="form-control" name="message" id="message"></textarea>
    <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
            <button type="submit" class="btn btn-info">Send Email</button>

How to run?

Open your browser and refer following link: