Learn How To Send Emails Using PHP Mailer With PHP

admin_img Posted By Bajarangi soft , Posted On 02-09-2020

PHPMailer  library for sending email. Sending emails directly through PHP code requires a high-level familiarity to SMTP. SMTP(Smiple Mail Transfer Protocal).

send_email

let's learn

1.Install PHPMailer is by using composer.

2.Run the below command in Command prompt.
composer require phpmailer/phpmailer


3.Using PHPMailer:

  • Include the packages and files of PHPMailer classe:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer-master/src/Exception.php';
require 'PHPMailer-master/src/PHPMailer.php';
require 'PHPMailer-master/src/SMTP.php';
 
  • Initialize PHP Mailer and set SMTP as mailing protocol:
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Mailer = "smtp";
 
  • Set required parameters for making an SMTP connection like server, port and account credentials needed. SSL and TLS  will provide authentication and data encryption between servers.
 
$mail->SMTPDebug  = 1;  
$mail->SMTPAuth   = TRUE;
$mail->SMTPSecure = "tls";
$mail->Port       = 587;
$mail->Host       = "smtp.gmail.com";
$mail->Username   = "your-email@gmail.com";
$mail->Password   = "your-gmail-password";


4.server settings:

SMTPDebug:  display messages of problems in connectivity and sending emails. 
      It has following values:
      0: It is default value.
      1: Display output messages sent by the client.
      2: As 1, plus display responses received from the server.
      3: As 2, plus more information about the initial connection – this level can help diagnose STARTTLS failures.
      4: As 3, plus display even lower-level information.
isSMTP(): Set mailer to use SMTP.
isMail(): Set mailer to use PHP’s mail function.
Host: Specifies the servers.
SMTPAuth: Enable/Disable SMTP Authentication.
Username:  username.
Password: user password.
SMTPSecure: Specify encryption technique. Accepted values ‘TLS’ or ‘SSL’.
Port: Specify the TCP port which is to be connected.


5.arguments  for email header and body:

$mail->IsHTML(true);
$mail->AddAddress("recipient-email@domain", "recipient-name")
$mail->SetFrom("from-email@gmail.com", "from-name")
$mail->AddReplyTo("reply-to-email@domain", "reply-to-name")
$mail->AddCC("cc-recipient-email@domain", "cc-recipient-name")
$mail->Subject = "Test is Test Email sent via Gmail SMTP Server using PHP Mailer"
$mail->Body =An HTML or plain text message body.


6.Finally, send the email:

if($mail->Send())        //Send an Email. Return true on success or false on error
{
    echo "Thank you for contacting us";
}
else
{
    echo "There is an Error";
}


Complete PHP Code  to send e-mail using PHPMailer.

<?php
$error = '';
$name = '';
$email = '';
$subject = '';
$message = '';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
if(isset($_POST["submit"]))
{
    require 'PHPMailer-master/src/Exception.php';
    require 'PHPMailer-master/src/PHPMailer.php';
    require 'PHPMailer-master/src/SMTP.php';
    $mail = new PHPMailer();
    $mail->IsSMTP();
    $mail->Mailer = "smtp";
    $mail->SMTPDebug = 1;
    $mail->SMTPAuth = TRUE;
    $mail->SMTPSecure = "tls";
    $mail->Port = 587;
    $mail->Host = "smtp.gmail.com";
    $mail->Username = "your-email@gmail.com";
    $mail->Password = "your-gmail-password";
    $mail->From = $_POST["email"];     //Sets the From email address for the message
    $mail->FromName = $_POST["name"];    //Sets the From name of the message
    $mail->IsHTML(true);
    $mail->AddAddress("recipient-email@domain", "recipient-name");
    $mail->SetFrom("from-email@gmail.com", "from-name");
    $mail->AddReplyTo("reply-to-email@domain", "reply-to-name");
    $mail->AddCC($_POST["email"], $_POST["name"]);
    $mail->Subject =  $_POST["subject"];
    $mail->Body = $_POST["message"];    //An HTML or plain text message body
    if ($mail->Send())        //Send an Email. Return true on success or false on error
    {
        echo "Thank you for contacting us";
    } else {
        echo "There is an Error";
    }

}
?>
<!DOCTYPE html>
<html>
   <head>
      <title>Sending Email By PHP with PHPMailer</title>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
   </head>
   <body>
      <br />
      <div class="container">
         <div class="row">
            <div class="col-md-8" style="margin:0 auto; float:none;">
               <h3 align="center">Sending Mail in PHP By PHPMailer </h3>
               <br />
               <?php echo $error; ?>
               <form method="post">
                  <div class="form-group">
                     <label>Enter Name</label>
                     <input type="text" name="name" placeholder="Enter Name" class="form-control" value="<?php echo $name; ?>" />
                  </div>
                  <div class="form-group">
                     <label>Enter Email</label>
                     <input type="text" name="email" class="form-control" placeholder="Enter Email" value="<?php echo $email; ?>" />
                  </div>
                  <div class="form-group">
                     <label>Enter Subject</label>
                     <input type="text" name="subject" class="form-control" placeholder="Enter Subject" value="<?php echo $subject; ?>" />
                  </div>
                  <div class="form-group" >
                     <input type="submit" name="submit" value="Submit" class="btn btn-success" />
                  </div>
               </form>
            </div>
         </div>
      </div>
   </body>
</html>

Related Post