Sending SMTP mail in CodeIgniter

How to send SMTP mail in CodeIgniter ?

We have normal mail() function in CodeIgniter but in some servers we may have to send emails using SMTP method. So in this article we will discuss how to send SMTP mail in CodeIgniter.

To check sending mail in codeigniter goto this link.

SMTP stands for Simple Mail Transfer ProtocolSMTP server is simply a computer running SMTP, and which acts more or less like the postman. Once the messages have been picked up they are sent to this server, which takes care of concretely delivering emails to their recipients.

Controller :

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

class Sendmail extends CI_Controller 
{
    public function __construct() 
    {
        parent::__construct();
    }

    public function sendMail()
    {
        $this->load->library('email');  
      
        $config = array();  
        $config['protocol'] = 'smtp';  
        $config['smtp_host'] = 'xxx.xxxxx.xx';  //your smtp host
        $config['smtp_user'] = 'xxxx@xxxx.xxx';  //mail id
        $config['smtp_pass'] = 'xxxxx';  //mail password
        $config['smtp_port'] = 587; 
        $this->email->initialize($config);
        $this->email->set_newline("\r\n");  
        $to =  'xxxx@xxxx.xxx';  // mail id to recieve mails
        $subject = 'Testing SMTP mail';

        $from = 'xxxx@xxxx.xxx';     // from mail id

        $message = 'testing';
        $this->email->from($from);
        $this->email->to($to);
        $this->email->subject($subject);
        $this->email->message($message);
        $this->email->send();
    }
}
  1. create a controller called Sendmail
  2. load the email library
  3. initializing config array to set some values
  4. set the protocol as smtp
  5. set your host address smtp_host
  6. specify your email id and password for smtp_user and smtp_pass respectively
  7. set port as 587
  8. initialize the config array
  9. then as normal mail function send mail

To get your SMTP details from cpanel check this link

You can run this from your local also, no need to upload in server.

Hope this article for sending SMTP mail in CodeIgniter will help you.

Related Posts

send mail in codeigniter

Send mail in CodeIgniter

How to send mail in CodeIgniter using the normal mail() email library. We can send mail in codeigniter using the mail() and SMTP methods. To know how…

generate qr code using codeigniter

Generate QR code using codeigniter

How to generate QR code using codeigniter ? In this tutorial we’ll discuss about generate QR code using codeigniter. QR code is an another form of barcode…

generate barcode in codeigniter

Generate barcode in codeigniter using zend library

How to generate barcode in codeigniter? Create display barcode using codeigniter with zend barcode library In this article we’ll discuss how to generate barcode in codeigniter using…

upload image from url

Upload image from URL in codeigniter

How to upload image from url in codeigniter? Rather than selecting a file from drive and uploading into folder we can upload image from URL. For that…

codeigniter pagination

Codeigniter Pagination

How to do codeigniter pagination with database? Codeigniter pagination is a very simple method to show large amount of data with a limit. Displaying large amount of…

Create PDF in Codeigniter using Dompdf – flexible library

How to generate a PDF in Codeigniter using Dompdf ? In this tutorial we will create pdf in codeigniter using Dompdf. Here I used image, inline css,…

This Post Has 2 Comments

Leave a Reply

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