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 Protocol. SMTP 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();
}
}
- create a controller called Sendmail
- load the email library
- initializing config array to set some values
- set the protocol as smtp
- set your host address smtp_host
- specify your email id and password for smtp_user and smtp_pass respectively
- set port as 587
- initialize the config array
- 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.
Reading your article helped me a lot and I agree with you. But I still have some doubts, can you clarify for me? I’ll keep an eye out for your answers.
Happy to help you, please reach me at tech.qawall@gmail.com