Php Mailer Is not Working

<?php
    $result='';
    if(isset($_POST["submit"]))
     
{
   
 require 'phpmailer/PHPMailerAutoload.php';
 $mail = new PHPMailer;

 $mail->Host='smtp.gmail.com';
 $mail->Port=587;
 $mail->SMTPAuth=true;
 $mail->SMTPSecure='tls';
 $mail->Username='[email protected]';
 $mail->Password='1234567';
 $mail->setFrom($_POST["email"]);
 $mail->addAddress('[email protected]');
 $mail->addReplyTo($_POST["email"]);
 $mail->isHTML(true);
 $mail->Subject="Form Submission :".$_POST["subject"];
 $mail->Body='<h1>Name :'.$_POST['name'].'<br/> Email :'.$_POST['email'].'<br/> Message : '.$_POST['msg'].'</h1>';
 if(!$mail->send())
 {
     $result="Something Wrong";
 }
   
else
{
    $result="Thanks " .$_POST["name"]."For Connection Us";
}

}

?>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="author" content="Sahil Kumar">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width,initial-scale=1">
  <title>Contact Form Bootstrap</title>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">
  <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.0.13/css/all.css">
</head>

<body class="bg-dark">
  <div class="container">
    <div class="row justify-content-center">
      <div class="col-md-4 mt-5 bg-light rounded">
        <h1 class="text-center font-weight-bold text-primary">Contact Us</h1>
        <hr class="bg-light">
        <h5 class="text-center text-success"><?=  $result; ?></h5>
        <form action="" method="post" id="form-box" class="p-2">
          <div class="form-group input-group">
            <div class="input-group-prepend">
              <span class="input-group-text"><i class="fas fa-user"></i></span>
            </div>
            <input type="text" name="name" class="form-control" placeholder="Enter your name" required>
          </div>
          <div class="form-group input-group">
            <div class="input-group-prepend">
              <span class="input-group-text"><i class="fas fa-envelope"></i></span>
            </div>
            <input type="email" name="email" class="form-control" placeholder="Enter your email" required>
          </div>
          <div class="form-group input-group">
            <div class="input-group-prepend">
              <span class="input-group-text"><i class="fas fa-at"></i></span>
            </div>
            <input type="text" name="subject" class="form-control" placeholder="Enter subject" required>
          </div>
          <div class="form-group input-group">
            <div class="input-group-prepend">
              <span class="input-group-text"><i class="fas fa-comment-alt"></i></span>
            </div>
            <textarea name="msg" id="msg" class="form-control" placeholder="Write your message" cols="30" rows="4" required></textarea>
          </div>
          <div class="form-group">
            <input type="submit" name="submit" id="submit" class="btn btn-primary btn-block" value="Send">
          </div>
        </form>
      </div>
    </div>
  </div>
</body>

</html>

It never worked? Or doesn’t work now?

Enabling debug output

If you’re using SMTP (i.e. you’re calling isSMTP() ), you can get a detailed transcript of the SMTP conversation using the SMTPDebug property. The settings are as follows:

  • 1: show client -> server messages only. Don’t use this - it’s very unlikely to tell you anything useful.
  • 2: show client -> server and server -> client messages - this is usually the setting you want
  • 3: As 2, but also show details about the initial connection; only use this if you’re having trouble connecting (e.g. connection timing out)
  • 4: As 3, but also shows detailed low-level traffic. Only really useful for analyzing protocol-level bugs, very verbose, probably not what you need.

Set this option by including a line like this in your script:

$mail->SMTPDebug = 2;

Read the SMTP transcript

If you set SMTPDebug = 2 or higher, you will see what the remote SMTP server says. Very often this will tell you exactly what is wrong - things like “Incorrect password”, or sometimes a URL of a page to help you diagnose the problem. Read what it says . Google does this a lot.

if(!$mail->send()) {
  echo 'Message was not sent.';
  echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
  echo 'Message has been sent.';
}

The setFrom property should not be an email address other than your own email (i.e. the email client you’re sending the email from)…It could be mistaken as forgery.
I am not sure if that is why your code isn’t working tho.

And as thinsoldier mentioned, use a debugger like
$mail->SMTPDebug = 2; //for general debugging
or
$mail->SMTPDebug = 1; //for verbose debugging

Place it somewhere like after this line
$mail = new PHPMailer;

It could give you useful info after your email was sent/cancelled.

Sponsor our Newsletter | Privacy Policy | Terms of Service