How to use PHPMailer with gmail smtp in codeigniter

Codeigniter

Hello everyone,

In this tutorial explains how we can send email using PHPMailer in codeingiter. PHPMailer is a php class and that provides to send html based and e-mails with attachements. PHPMailer simplify the email forward to multiple recepients with cc,bcc etc. You can send emails from your own SMTP (Simple Mail Transfer Protocol) servers.

Please follow few steps describes below to send email using PHPmailer from codeigniter framework.

  1. Download and Install PHPMailer
  2. Create a PHPmailer library
  3. Create a controller to send email.
  4. change gmail configuration

Detailed description of the steps are listed below, please check it.

Step 1 : Download PHPMailer

Please visit this link to download PHPMailer  https://github.com/PHPMailer/PHPMailer  .

Step 2: Install PHPMailer

Extract downloaded zip file and copy the PHPmailer folder. After that open thrid party folder of your codeigniter application and past the PHPMailer folder there. This folder include Exception.php, Oauth.php, PHPMailer.php, POP.php and SMTP.php files.

Step 3: Create Phpmailer library file

Create a library for PHPmailer under your codeigniter application. Open application/library folder. Create a new PHP file with name Phpmailer.php and the page contains below code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php defined('BASEPATH') OR exit('No direct script access allowed');

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
class PHPMailer
{

      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 4: Create a controller

create a controller called sendmail to to send the mail.

Note: here we need to load the phpmailer library. Whre $email is the instance of phpmailer library class. We where using SMTP to send emails, so set the host as ‘smtp.gmail.com’. Then mention your/company gmail account info(email address and password) to send the mail. The port number is 587.

Enter the email address of recipient in the addAddress().

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 
22 
23
24 
25 
26 
27 
28 
29 
30
31
32
33
34
35
36
37
38
39
40
<?php defined('BASEPATH') OR exit('No direct script access allowed'); 
class SendMail extends CI_Controller { 
        function __construct() {
            parent::__construct(); 
            // Load PHPMailer library
            $this->load->library('phpmailer');
	}
	public function index(){
            // PHPMailer object
            $mail = $this->phpmailer->load();
            // SMTP configuration
           $mail->isSMTP();
           $mail->Host = 'smtp.gmail.com';
           $mail->SMTPAuth = true;
           $mail->Username = '*********@gmail.com';//enter your gmail address
           $mail->Password = '**********'; //password of your gmail account
           $mail->SMTPSecure = 'tls';
           $mail->Port = 587;
 
           $mail->setFrom('***enter the email address of sender***', 'your name');
           $mail->addReplyTo('***enter the email addresss to reply***', 'reply user name');
          
           // Add a recipient
           $mail->addAddress('******'); //email address of recipient

           // Email subject
           $mail->Subject = 'Send Email using PHPMailer with gmail smtp in codeigniter';
           
           //If sending HTML email set to true else set to false
           $mail->isHTML(true);
           $mail->Body = 'Test mail';
           if($mail->send())
              echo 'Mail Send';
           else
              echo 'Mail not send'.'<br> Error:'. $mail->ErrorInfo;
	}	
			
	
}

use addAttchmnet() function to attach the files along with the mail,

$path – required,path of the attached file

$name -optional, name of the file,

// Attachments
    $mail->addAttachment($path);         
    $mail->addAttachment($path, $filename);  

Step 5 : Login to Gmail Account

Login into your gmail acount that used to send email. Go to my account settings->signing in to google, here turn on allow on less secure apps.

Now run the application and  check your mail.

Leave a Reply

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