Hey you can send emails using PHPMailer very easily in CodeIgniter frame work in the following steps:
Step 1: Download latest version of PHPMailer master. Unzip it. Go to folder 'src' and copy all the files in this folder and paste them in new folder PHPMailer (you have to create) under 'third_party' folder located in CodeIgniter as follows.
C:\xampp\htdocs\CI\application\third_party\PHPMailer
Step 2: Now create a new php file name by 'Phpmailer_lib.php' under
'libraries' folder located in 'application' of CodeIgniter.
C:\xampp\htdocs\CI\application\libraries
Now paste the follow piece of code in it.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
* CodeIgniter PHPMailer Class
*
* This class enables SMTP email with PHPMailer
*
* @category Libraries
* @author Lonarbi
* @link https://www.lonarbi.blogspot.com
*/
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
class PHPMailer_Lib
{
public function __construct(){
log_message('Debug', 'PHPMailer class is loaded.');
}
public function load(){
// Include PHPMailer library files
require_once APPPATH.'third_party/PHPMailer/Exception.php';
require_once APPPATH.'third_party/PHPMailer/PHPMailer.php';
require_once APPPATH.'third_party/PHPMailer/SMTP.php';
$mail = new PHPMailer;
return $mail;
}
}
?>
Step 3: Now create a new controller name by 'Email.php' under 'controllers' and paste the following code in it (update your email information).
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Email extends CI_Controller{
function __construct(){
parent::__construct();
}
function send(){
// Load PHPMailer library
$this->load->library('phpmailer_lib');
// PHPMailer object
$mail = $this->phpmailer_lib->load();
// SMTP configuration
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'youremail@gmail.com';
$mail->Password = 'password_here';
$mail->SMTPSecure = 'ssl'; // may use 'tls'
$mail->Port = 465; //may use 587
$mail->setFrom('contact@mywebsite.com', 'Lonarbi');
$mail->addReplyTo('contact@mywebsite.com', 'Lonarbi');
// Add a recipient
$mail->addAddress('recipient_email@gmail.com');
// Add cc or bcc
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');
// Email subject
$mail->Subject = 'Lonarbi - Send Email via SMTP using PHPMailer in CodeIgniter';
// Set email format to HTML
$mail->isHTML(true);
// Email body content
$mailContent = "<h1>Send HTML Email using SMTP in CodeIgniter</h1>
<p>This is a test email sending using SMTP mail server with PHPMailer.</p>";
$mail->Body = $mailContent;
// Send email
if(!$mail->send()){
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}else{
echo 'Message has been sent';
}
}
}
?>
Every thing is done.
Now check url : http://localhost/CI/email/send
NOTE: Please follow the below steps to allow your gmail to send email through SMTP.
Go to 'Security' under 'Settings'... make sure that
2-Step verification is 'OFF'
Less secure app access is 'ON' (By default it is in 'OFF' status)
Thank you ... Happy Coding...
ConversionConversion EmoticonEmoticon