How send Email in PHP using PHPMailer and GMAIL SMTP

 

 

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 the following 3 files and paste them in new folder "includes" (you have to create) under root/any folder you need.

Step 2: Now create a new php file name by 'index.php' under "root" / "any folder" paste the follow piece of code in it.

<?php

// INCLUDE PHP FILES
require 'includes/PHPMailer.php';
require 'includes/SMTP.php';
require 'includes/Exception.php';

// USE NAME SPACES
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

                       
    $mail = new PHPMailer();
    
    // SMTP configuration
    $mail->isSMTP();
    $mail->Host     = 'smtp.gmail.com';
    $mail->SMTPAuth = true;
    $mail->SMTPSecure = 'tls';
    $mail->Port     = '587';
    $mail->Username = 'youremail@gmail.com';
    $mail->Password = 'your_password';

    // Email subject
    $mail->Subject = 'EMAIL SUBJECT HERE';
    $mail->setFrom('
youremail@gmail.com');
    
    // Add a recipient
    $mail->addAddress('receiver@ur_mail.com');
    
    // Add cc or bcc
    //$mail->addCC('cc@example.com');
    //$mail->addBCC('bcc@example.com');
    
    // Set email format to HTML
    $mail->isHTML(true);
    
    // Email body content
    $mailContent = "<h1>Welcome to GMAIL SMTP</h1>
        <p>This is a test email </p>";
    $mail->Body = $mailContent;
    
    // Send email
    if($mail->send()){
        echo "Email submitted successfully";
    }else{
        echo "Error...";
    }

    $mail->smtpClose();


?>

 Every thing is done. 

Now check url : http://localhost/root/index.php

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...

 


Previous
Next Post »
Related Posts Plugin for WordPress, Blogger...