CodeIgniter’s Email class is the simplest way to send email in CodeIgniter application. Not only text/HTML email but also you can send email via SMTP server using CodeIgniter Email library. This Email class is a system library and comes with the CodeIgniter framework. So, it can be easily used in the CodeIgniter application without an additional library. Alternatively, the PHPMailer library is the best option for sending email via SMTP server without using the CodeIgniter default Email library.
Generally, PHPMailer library is used to send email with SMTP server in PHP. You can also use the PHPMailer library in CodeIgniter framework to send email using SMTP server. In this tutorial, we will show you how to integrate PHPMailer in CodeIgniter 3 application and send email via SMTP server using PHPMailer in CodeIgniter.
At first, download the latest PHPMailer library files and place all the files in the application/third_party/
folder of your CodeIgniter application.
application/ └── third_party/ └── PHPMailer/ ├── Exception.php ├── OAuth.php ├── PHPMailer.php ├── POP3.php └── SMTP.php
Note that: All the PHPMailer library files are included in the source code, you don’t need to download PHPMailer separately.
Now, create a library (application/libraries/Phpmailer_lib.php
) to handle the PHPMailer object.
<?php defined('BASEPATH') OR exit('No direct script access allowed'); /** * CodeIgniter PHPMailer Class * * This class enables SMTP email with PHPMailer * * @category Libraries * @author CodexWorld * @link https://www.codexworld.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; } }
Using PHPMailer_Lib library you can send email with PHPMailer in your CodeIgniter application. The following example code shows how to send email via SMTP server using PHPMailer from the controller of the CodeIgniter application.
load()
function of PHPMailer_Lib.$mail->Host
), username ($mail->Username
), password ($mail->Password
), and port ($mail->Port
) as per your SMTP server credentials.isHTML()
to TRUE for sending HTML email.<?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.example.com'; $mail->SMTPAuth = true; $mail->Username = 'user@example.com'; $mail->Password = '********'; $mail->SMTPSecure = 'ssl'; $mail->Port = 465; $mail->setFrom('info@example.com', 'CodexWorld'); $mail->addReplyTo('info@example.com', 'CodexWorld'); // Add a recipient $mail->addAddress('john.doe@gmail.com'); // Add cc or bcc $mail->addCC('cc@example.com'); $mail->addBCC('bcc@example.com'); // Email subject $mail->Subject = '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'; } } }
You can use Gmail SMTP to sending email with PHPMailer in CodeIgniter. Before getting started with Gmail SMTP, some changes are needed in your Google account settings to use Gmail SMTP.
You are done! Now Google will allow you to use Gmail SMTP for sending email from the PHP script of your CodeIgniter application.
Specify your Gmail account’s email address as username ($mail->Username
), password ($mail->Password
), SMTP host and port.
// SMTP configuration $mail->isSMTP(); $mail->Host = 'smtp.gmail.com'; $mail->SMTPAuth = true; $mail->Username = 'codexworld@gmail.com'; $mail->Password = '********'; $mail->SMTPSecure = 'tls'; $mail->Port = 587;
Do you want to get implementation help, or enhance the functionality of this script? Click here to Submit Service Request
these codes is really helping me to learn something new
nice explanations of codes..