Posted On:

Posted In:

A website may need to send an email to interact with its user, either for confirming their authenticity, helping them to retrieve their password, informing them about any terms or policy change, or plainly for marketing purposes.

CodeIgniter offers a built-in email library that equips your web application for sending emails. Here we will teach you how to send emails using the SMTP protocol. 

 

Step 1: 

Create SMTP configuration file email.php inside application/config folder. 

 

<?php 

$config = array( 

   ‘protocol’ => ‘smtp’, 

   smtp_host => ‘smtp.example.com’, 

   smtp_port => 465, 

   smtp_user => smtp.user@example.com, 

   smtp_pass => ‘******’, 

   smtp_crypto => ssl, 

   mailtype => ‘text’, 

   smtp_timeout => ‘4’, 

   ‘charset’ => ‘iso-8859-1’, 

   wordwrap => TRUE 

); 

?> 

 

Step 2: 

Create EmailController.php file inside the controller directory and add the following code. 

 

<?php 

class EmailController extends CI_Controller 

{ 

    function send() { 

       $this->load->config(’email’); 

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

       

       $from = $this->config->item(‘smtp_user‘); 

       $to = ‘receiver@email.com‘; 

       $subject = ‘subject’; 

       $message = ‘message’; 

  

       $this->email->from($from); 

       $this->email->to($to); 

       $this->email->subject($subject); 

       $this->email->message($message); 

  

       if ($this->email->send()) { 

           echo ‘Email has been sent successfully’; 

       } else { 

           show_error($this->email->print_debugger()); 

       } 

   } 

} 

?> 

 

Step 3: 

Now your work is done. To check your code, send a test mail: 

Run http://localhost/EmailController/send in your browser. 

Conclusion

After reading this post, implementing emailing functionality should be super easy for you. Let me know if you have any questions regarding the article in the comment section below. Share this post with your friends and social media followers.

If you are looking for a software firm to create your business website or mobile app, your search ends here. We build affordable and robust software solutions for businesses to digitally thrive on. Contact us to get a quote. You can mail us at info@qrolic.com or WhatsApp us on +91 95 37 84 38 39.

[/vc_column_text][/vc_column][/vc_row]

Latest Posts

Block Editor vs Divi vs Elementor: Choosing Best WordPress Builder

April 10, 2024

A few years back website creation demanded a lot of technical knowledge and proficiency in HTML, CSS, and JavaScript. The scenario has changed, building a user-friendly website with no code is possible. Today, a variety…

Maximize Your ROI: Smart Website Budget for 2024

January 24, 2024

It’s a new year, a perfect time to make plans that will shape your website goals for 2024. This is crucial and will make sure that you have a smooth run for the year, avoid…

Leave a Reply

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

  1. Qrolic Technologies