Send Email in CodeIgniter With SMTP

Send Email in CodeIgniter With SMTP – Vkprogramming

Introduction

PHP is the most used programming language in the development of web applications. And, sending an email is one of the important modules used in any web application. Web applications send different types of system-generated emails like signup confirmation, forgot-password, statements and many more.

Nowadays, almost every PHP framework provides well managed and developed email functionality. And, CodeIgniter is no new to it and has a built-in email library to send emails to.

Sending email in CodeIgniter is much easier. You have to configure SMTP properties in the configuration file, load it, and start sending emails using SMTP. That’s it.

 

How to Send Email using CodeIgniter

Sending email in CodeIgniter is much easier and you may set preferences following your needs. CodeIgniter supplies an Email library for sending emails in the program. Codeigniter supported the inbuilt Mail library which simplifies the email sending process.

To send an email in Codeigniter we need to do a little configuration, need to load the email library.

Codeigniter supports Protocols, Mail, SMTP, and send mail,, multiple recipients, BCCs and CC , HTML or Plain text email, Attachments, Word wrapping and many more.

You also configure the preferences regarding email in CodeIgniter provides the following for send mails −

  • Multiple Protocols − Mail, Sendmail, and SMTP
  • TLS and SSL Encryption for SMTP
  • Multiple recipients
  • CC and BCCs
  • HTML or Plaintext email
  • Attachments
  • Word wrapping
  • Priorities
  • BCC Batch Mode, enabling large email lists to be broken into small BCC batches.
  • Email Debugging tools

Email class has the following functions to simplify the job of sending emails.

 

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 are subject() and message() that round up the requirements for sending emails in CodeIgniter. Here is how these functions are used in the code:

Code hare

$this->email->from(’email@example.com’, ‘Identification’);

$this->email->to(’emailto@example.com’);

$this->email->subject(‘Send Email Codeigniter’);

$this->email->message(‘The email send using codeigniter library’);

 

Controller

Create controller Sendemail_controller.php and save it in the application/controller/. and following code

 

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@xyz.com”;

 $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’);

}}

 

View

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

Php code

echo $this->session->flashdata(’email_sent’);

echo form_open(‘/Sendingemail_Controller/send_mail’);

 

html code

<input type = “email” name = “email” required />

<input type = “submit” value = “SEND MAIL”>

echo form_close();

 

Add Code Following applications/config/routes.php

$route[’email’] = ‘Sendingemail_Controller’;

 

Mail SMTP Configuration

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

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

 

$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(“rn”);

 

Summary

The built-in email library makes it easy for us to send emails with minimal code. The library is also very flexible in the sense that you can configure it to meet your requirements.

Adding email functionality to CodeIgniter applications deployed on any Web Hosting for PHP is a simple matter of using 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 in sending email in your CodeIgniter applications, do leave a comment below and I will get back to you.

 

Related:

 

also question

send mail in Codeigniter using phpmailer,

how to send email in Codeigniter from localhost,

send email in Codeigniter without SMTP,

send email Codeigniter – stack overflow,

email library in Codeigniter

Scroll to Top