How to Send Email In CodeIgniter?

Share This Post

Send Email In CodeIgniter. CodeIgniter Has Built in Function to send email. In Our Routine. For Website Or Web Application We Need To Send Email For Verifications, Reset Password, Forgot Password, To Send Invoice any many more email we have to send through our website or web-application. As I said Before Codeigniter have inbuilt library to send email. Its Too Much Easy To use Inbuilt Library With minimall Code and Easy To Understand. So Without wasting Time lets Start This Article.

In This Article, You will learn,

1. CodeIgniter Setup Email Configuration.
2. CodeIgniter Controller.
3. CodeIgniter View.

CodeIgniter Setup Email Configuration To Send Email In CodeIgniter.

In CodeIgniter we have to make Configuration File For Email setup, CodeIgniter not come with existing config for email. So You have to Define Your Google SMTP, Or Whatever email setup in one file.

Make New File email.php and Add This Code In Your Application/Config Folder.

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

$config = array(
    'protocol' => 'smtp', // 'mail', 'sendmail', or 'smtp'
    'smtp_host' => 'smtp.example.com', 
    'smtp_port' => 465,
    'smtp_user' => '[email protected]', //here is your email
    'smtp_pass' => '12345!', // here is your password
    'smtp_crypto' => 'ssl', //can be 'ssl' or 'tls' for example
    'mailtype' => 'text', //plaintext 'text' mails or 'html'
    'smtp_timeout' => '4', //in seconds
    'charset' => 'iso-8859-1',
    'wordwrap' => TRUE
);

Here is,
smtp_user is Your email address and this will show as send from. this must be valid email address. you can use your google mail address here.

smtp_pass is your SMTP email password, also you can use google mail if you Enable Third-Party Mail Clients. Dont Know How ? Click Here to Enable Third-Party Mail Clients

Rest You can Customize If You have proper knowledge, else you can leave as it is.

CodeIgniter Controller.

To Send Email In CodeIgniter, First We have To make Controller. Follow Below Code To Send Email.
Add Following Code To Your Controller Or Create New File EmailController.php in your Application/Controller Directory.

<?php

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

class EmailController extends CI_Controller {

    public function __construct() {
        parent:: __construct();

        $this->load->helper('url');
    }

    public function index() {
        $this->load->view('email/contact');
    }

    function send() {
        $this->load->config('email');
        $this->load->library('email');
        
        $from = $this->config->item('smtp_user');
        $to = $this->input->post('to');
        $subject = $this->input->post('subject');
        $message = $this->input->post('message');

        $this->email->set_newline("\r\n");
        $this->email->from($from);
        $this->email->to($to);
        $this->email->subject($subject);
        $this->email->message($message);

        if ($this->email->send()) {
            echo 'Your Email has successfully been sent.';
        } else {
            show_error($this->email->print_debugger());
        }
    }
}

Here is,

  • $this->load->config(’email’); loads the email configuration settings
  • $this->load->library(’email’); loads the email library
  • $from = $this->config->item(‘smtp_user’); gets the sender id from the email configuration file that we defined.
  • $to = $this->input->post(‘to’); gets the to value from the submitted form
  • $subject = $this->input->post(‘subject’); sets the email subjected from the form
  • $message = $this->input->post(‘message’); sets the email message from the form
  • $this->email->set_newline(“\r\n”); defines the new line characters for emails
  • $this->email->from($from); sets the sender email address
  • $this->email->to($to); sets the recipient email address
  • $this->email->subject($subject); sets the email subject
  • $this->email->message($message); sets the email message
  • if ($this->email->send()) {…} attempts to send the email. If the email is sent successfully, then the message Your Email has successfully been sent else debug information is printed on what might have gone wrong.

Now, Lets Create Simple View File To Send mail from browser, else you can also try with postman

Also Read How to Upload Image And File in CodeIgniter.

CodeIgniter View.

Create a new file contact.php application/views/email

Add the following code to application/views/email/contact.php

<!DOCTYPE html>
<html>
    <head>
        <title>CodeIgniter Send Email</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <div>
            <h3>Use the form below to send email</h3>
            <form method="post" action="<?=base_url('email')?>" enctype="multipart/form-data">
                <input type="email" id="to" name="to" placeholder="Receiver Email">
                <br><br>
                <input type="text" id="subject" name="subject" placeholder="Subject">
                <br><br>
                <textarea rows="6" id="message" name="message" placeholder="Type your message here"></textarea>
                <br><br>
                <input type="submit" value="Send Email" />
            </form>
        </div>
    </body>
</html>

HERE,

  • We have a basic HTML form that accepts the email, subject and message then passes the parameters to email route.

Now You Can try This code with http://localhost:3000/YourController/send-email

Still Not Understand ? Watch Out This Video, this may help you.

send email in codeigniter

So This Article is about How to Send Email In CodeIgniter? Comment Below If You have Queries or Error and share this article.

Leave a Reply

Your email address will not be published. Required fields are marked *

close