How to send email with attachment From Local/Online Server using PHPMailer in Hindi | Updated 2020

 


PHPMailer is mostly used PHP library for sending the email . This library is very fast and you will get email direct in your inbox whereas when we use php mail function then it send email but email will go inside the SPAM . 

Email sending process using PHPMailer : - 

Step 1 : Download  PHPMailer Library From https://github.com/PHPMailer/PHPMailer      .

Step 2 : Extract Zip File and copy 3 Files from extracted folder named PHPMailer.php , SMTP.php and Exception.PHP . Keep these files in your project and include by using built in function "require" of PHP .

Strep 3 : Here we are using GMAIL SMTP but you can use according to you . Set smtp detail and then set username (email id) and password of your account .

Note : Here we are not using composer for this so we don't need to include autoload.php file . If you need any help then you can reach us at dular88@gmail.com . For more detail visit at https://github.com/PHPMailer/PHPMailer




Demo : https://www.youtube.com/watch?v=I6ewaTaAoVA

Code Sample : -

<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

// Load Composer's autoloader
//require 'vendor/autoload.php';
require 'path/to/PHPMailer/src/Exception.php';
require 'path/to/PHPMailer/src/PHPMailer.php';
require 'path/to/PHPMailer/src/SMTP.php';
 // Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);

try {
    //Server settings
    $mail->SMTPDebug = SMTP::DEBUG_SERVER;                      // Enable verbose debug output
    $mail->isSMTP();                                            // Send using SMTP
    $mail->Host       = 'smtp1.example.com';                    // Set the SMTP server to send through
    $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
    $mail->Username   = 'user@example.com';                     // SMTP username
    $mail->Password   = 'secret';                               // SMTP password
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;         // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
    $mail->Port       = 587;                                    // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above

    //Recipients
    $mail->setFrom('from@example.com', 'Mailer');
    $mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
   

    // Attachments
   
    $mail->addAttachment('path/to/image.jpg', 'new.jpg');    // Optional name

    // Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}

Comments

Popular posts from this blog