This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

📣 Join our live AMA on the Future of Page Builders with Brizy's CEO! Register Now →

How to Send Email in CodeIgniter With SMTP

Updated on October 17, 2022

2 Min Read
codeigniter send email

These days, almost every PHP framework supports a well-developed email management library. CodeIgniter is no different, as it has a great email-sending class that ensures that CodeIgniter projects can send emails without difficulty.

This article will describe how to send emails in a CodeIgniter application using SMTP. I will use the well-known and maintained email-sending class.

Load CodeIgniter’s Email Class

First, load the CodeIgniter’s email library through the following code snippet:

$this->load->library('email');

Set Email Parameters

The next step is to set up the required fields for the custom email. these fields could be set up through several functions, including from() function takes two parameters – the email address of the sender and the name. to() function takes the email address of the recipient. The next two functions, subject() and message(), round up the requirements for sending emails in CodeIgniter. Here is how these functions are used in the code:

$this->email->from('[email protected]', 'Identification');
$this->email->to('[email protected]');
$this->email->subject('Send Email Codeigniter');
$this->email->message('The email send using codeigniter library');

Stop Wasting Time on Servers

Cloudways handle server management for you so you can focus on creating great apps and keeping your clients happy.

Once these functions are filled, the final step is to send the email by using the send() function.

$this->email->send();

Create the Controller

Create a controller file Sendingemail_controller.php and save it in the application/controller/. Add the following code to this file:

<?php
class Sendingemail_Controller extends CI_Controller {
    function __construct() {
        parent::__construct();
        $this->load->library('session');
        $this->load->helper('form');
    }
    public function index() {
        $this->load->helper('form');
        $this->load->view('contact_email_form');
    }
    public function send_mail() {
        $from_email = "[email protected]";
        $to_email = $this->input->post('email');
        //Load email library
        $this->load->library('email');
        $this->email->from($from_email, 'Identification');
        $this->email->to($to_email);
        $this->email->subject('Send Email Codeigniter');
        $this->email->message('The email send using codeigniter library');
        //Send mail
        if($this->email->send())
            $this->session->set_flashdata("email_sent","Congragulation Email Send Successfully.");
        else
            $this->session->set_flashdata("email_sent","You have encountered an error");
        $this->load->view('contact_email_form');
    }
}
?>

Create the View

Create a view file called contact_email_form.php and save it in application/views/. Add the following code to it:

<html>
<head>    
    <title> Send Email Codeigniter </title>
</head>
<body>
<?php
echo $this->session->flashdata('email_sent');
echo form_open('/Sendingemail_Controller/send_mail');
?>
<input type = "email" name = "email" required />
<input type = "submit" value = "SEND MAIL">
<?php
echo form_close();
?>
</body>
</html>

Make the changes in the routes.php file in application/config/routes.php and add the following line at the end of the file:

$route['email'] = 'Sendingemail_Controller';

Access the Email Application

Finally, Hit the following URL to access the application:

http://your-domain.com/index.php/email

Setting SMTP Configuration

As mentioned earlier, CodeIgniter fully supports different email protocols, including SMTP, through simple configuration options.

As you can see from the following code snippet, selecting the email protocol is a matter of setting a single configuration variable. In this code snippet, I have set the $config[‘protocol’] to  SMTP for using the SMTP protocol.

<?php
$this->load->library('email');

$config = array();
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'xxx';
$config['smtp_user'] = 'xxx';
$config['smtp_pass'] = 'xxx';
$config['smtp_port'] = 25;
$this->email->initialize($config);

$this->email->set_newline("\r\n");

Summary

Adding email functionality to CodeIgniter applications deployed on any Web Hosting for PHP is simple to use in the email library. All you have to do is to set a few variables, and the email is set up. If you need any help sending emails in your CodeIgniter applications, leave a comment below, and I will get back to you.

Share your opinion in the comment section. COMMENT NOW

Share This Article

Owais Alam

is the WordPress Community Manager at Cloudways - A Managed WooCommerce Hosting Platform and a seasoned PHP developer. He loves to develop all sorts of websites on WordPress and is in love with WooCommerce in particular. You can email him at [email protected]

×

Get Our Newsletter
Be the first to get the latest updates and tutorials.

Thankyou for Subscribing Us!

×

Webinar: How to Get 100% Scores on Core Web Vitals

Join Joe Williams & Aleksandar Savkovic on 29th of March, 2021.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Want to Experience the Cloudways Platform in Its Full Glory?

Take a FREE guided tour of Cloudways and see for yourself how easily you can manage your server & apps on the leading cloud-hosting platform.

Start my tour

CYBER WEEK SAVINGS

  • 0

    Days

  • 0

    Hours

  • 0

    Mints

  • 0

    Sec

GET OFFER

For 4 Months &
40 Free Migrations

For 4 Months &
40 Free Migrations

Upgrade Now