php smtp mailer
        In this example, we are going to show you how to send emails in PHP with phpmailer.

Why PHPMailer?

        PHPMailer is an email-sending library for PHP also known as php smtp mailer that offers a range of features and benefits. Unlike other email-sending options, PHPMailer provides robust email validation, support for HTML and plain text messages, file attachments, and SMTP authentication. It's also easy to install and use, making it a top choice for developers around the world.

Contents

  • Download PHPMailer Library
  • Install PHPMailer
  • Import PHPMailer Library
  • Create a new project folder
  • Create new PHP file
  • Import PHPMailer code
  • Run the program
  • Conclusion 

Getting Started with PHPMailer

        To start using PHPMailer, you'll need to install it on your server. Once you have PHPMailer installed, you can create a new PHP file and begin coding your email. In this guide, we'll cover the basic steps for sending an email with PHPMailer:

  1. Import the PHPMailer library
  2. Set up your email
  3. Send your email

How to install PHPMailer

  • Download PHPMailer
  • Composer to install

Importing the PHPMailer Library

        The first step in using PHPMailer is to import the library into your project. You can do this by downloading the latest version of PHPMailer from the official GitHub repository and placing the PHPMailer directory in your project. Alternatively, you can use Composer to install PHPMailer.


        Can you install the PHPMailer library in your PHP project by running the following command in Composer


composer require phpmailer/phpmailer

After Installations 


Create a new PHP file in your project path project folder/enquiry.php

Set up and Send your email using PHPMailer

// Set the SMTP server and port $mail->isSMTP(); $mail->Host = 'smtp.example.com'; //or $mail->Host = 'mail.example.com'; $mail->Port = 587; // Set SMTP authentication $mail->SMTPAuth = true; $mail->Username = 'your_email@example.com'; $mail->Password = 'your_password'; // Set SMTP encryption $mail->SMTPSecure = 'tls';

 Error Messages in PHPMailer

        $mail ->ErrorInfo is a function that can be used to retrieve error messages in more than 40 different languages.

$mail->ErrorInfo;

You can send email from a local web server by using the code below.

<?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';

//Create an instance; passing `true` enables exceptions
$mail = new PHPMailer(true);

        $mail = new PHPMailer;
        $mail->isSMTP();
        //$mail->SMTPDebug = 2;
        $mail->Debugoutput = 'html';
        $mail->Host = 'mail.yourhost.com';
        $mail->SMTPAuth = true;
        $mail->Username = 'youremail@example.com';
        $mail->Password = 'yourpassword';
        $mail->SMTPSecure = 'tls';
        $mail->Port = 587;

        $mail->setFrom('from@gmail.com');
        $mail->addAddress('receive@gmail.com');     //Add a recipient
        //$mail->addAddress('ellen@example.com');   //Name is optional
        $mail->addReplyTo('no-reply@example.com');
        $mail->addCC('addCC@example.com');
        $mail->addBCC('bcc@example.com');

        //Attachments
        $mail->addAttachment('/var/tmp/file.tar.gz');         //Add attachments
        $mail->addAttachment('/tmp/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';

        if($mail->send())
        {
            //$mail->send();
            echo 'Message has been sent';
        }
        else
        {
            echo "Message could not be sent. <p>";
           echo "Mailer Error: " . $mail->ErrorInfo;
           exit;
        }
       

    ?>

        Before sending an email through SMTP, hostname, port number, and encryption are required. Also, you may need a username and password for authentication. 

        Finally enter this url in your browser: http://localhost/phpmailer/enquiry.php

        We hope this guide has been helpful. If you have any questions or need further assistance, Please feel free to comment below, with your suggestion and problems you face - we are here to solve your problems.