Sei sulla pagina 1di 3

TUTORIALS HOW TO DEMOS SCRIPTS DEALS SERVICES WEB TOOLS Search tutorials 

Home » CodeIgniter » Send Email using PHPMailer in CodeIgniter

SUBSCRIBE FOR FREE NEWSLETTER


Send Email using PHPMailer in CodeIgniter Join our 75,000+ subscribers and get the latest tutorials and resources, straight to
your inbox.

By: CodexWorld In: CodeIgniter Last Updated: Dec 19, 2018 Share Tweet
ENTER YOUR EMAIL...

SUBSCRIBE
Send Email using PHPMailer in CodeIgniter

CodeIgniter’s Email class is the simplest way to send email in CodeIgniter application. Not only the text/html email but also you can send the 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, PHPMailer library is the best option to send 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.

Integrate PHPMailer in CodeIgniter TRENDING TUTORIALS


At first, download the latest PHPMailer library files and place all the files in the application/third_party/ folder of your CodeIgniter application.
Login with Facebook using PHP

application/
Login with Google Account using PHP
└── third_party/
└── PHPMailer/ PayPal Standard Payment Gateway Integration in PHP
├── Exception.php
Dynamic Dependent Select Box using jQuery, Ajax and PHP
├── OAuth.php
├── PHPMailer.php Add Remove Input Fields Dynamically using jQuery
├── POP3.php
└── SMTP.php Autocomplete Textbox using jQuery, PHP and MySQL

Integrate new Google reCAPTCHA Checkbox with PHP


Note that: All the PHPMailer library files are included in the source code, you don’t need to download PHPMailer separately.

Upload Multiple Images using jQuery, Ajax and PHP


Now, create a library ( application/libraries/Phpmailer_lib.php ) to handle the PHPMailer object.

Include the PHPMailer library files.

Initialize the PHPMailer class.


TOPICS all topics
Return the PHPMailer object.
PHP CodeIgniter
<?php
defined('BASEPATH') OR exit('No direct script access allowed'); WordPress JavaScript

/** GoogleMap Drupal


* CodeIgniter PHPMailer Class
* HTML&CSS CakePHP
* This class enables SMTP email with PHPMailer
* Bootstrap Web Development
* @category Libraries
* @author CodexWorld PayPal GoogleAPI
* @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;
} LATEST HOW TO GUIDES
}

How to Test Facebook OAuth in Development Mode


Send Email using PHPMailer in CodeIgniter
How to Remove Arrows (spinner) from Number Input using CSS

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
How to Trigger Button Click on Enter Key Press using JavaScript
using PHPMailer from the controller of the CodeIgniter application.
How to Extract Content Between HTML Tags using PHP
Load the PHPMailer_Lib library.
How to Bind jQuery Click Event on Ajax Loaded Content
Call the load() function of PHPMailer_Lib.

Specify the host ( $mail‐>Host ), username ( $mail‐>Username ), password ( $mail‐>Password ), and port ( $mail‐>Port ) as per your SMTP server credentials.

Set 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';
}
}

Send Email with Gmail SMTP using PHPMailer in CodeIgniter

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.

1. Login to your Google account and go to the My Account page.

2. Click the Signing in to Google link under the Sign-in & security section.

3. Scroll down to Password & sign-in method section and turn Off the 2-Step Verification.

4. Scroll down to Apps with account access section and turn on Allow less secure apps.

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;

Are you want to get implementation help, or modify or extend the functionality of this script? Submit paid service request

 Download Source Code


 Verify Email Address and Check if Email is Real using jQuery UI Autocomplete with Images and Custom HTML 

PHP in PHP

RECOMMENDED TUTORIALS FOR YOU

Leave a reply

Comment *

Your Name * Your Email * Your Website

Post Comment

Copyright © 2019 CodexWorld. All rights reserved. About Us | Privacy Policy | Terms & Conditions | Write For Us | Advertise | Contact

Potrebbero piacerti anche