0

I got this PHP code somewhere and everything seems ok, but I keep getting an error message every-time I try send a test email; However, the network message says that it did send. Please see the images and code snippets. Please help. Thanks

PHP SCRIPT



    session_cache_limiter( 'nocache' );
    header( 'Expires: ' . gmdate( 'r', 0 ) );
    header( 'Content-type: application/json' );

    $to         = 'myemail@gmail.com';  // put your email here

    $email_template = 'simple.html';

    $subject    = strip_tags($_POST['subject']);
    $email       = strip_tags($_POST['email']);
    $phone      = strip_tags($_POST['phone']);
    $name       = strip_tags($_POST['name']);
    $message    = nl2br( htmlspecialchars($_POST['message'], ENT_QUOTES) );
    $result     = array();


    if(empty($name)){

        $result = array( 'response' => 'error', 'empty'=>'name', 'message'=>'<strong>Error!</strong>&nbsp; Name is empty.' );
        echo json_encode($result );
        die;
    } 

    if(empty($email)){

        $result = array( 'response' => 'error', 'empty'=>'email', 'message'=>'<strong>Error!</strong>&nbsp; Email is empty.' );
        echo json_encode($result );
        die;
    } 

    if(empty($message)){

         $result = array( 'response' => 'error', 'empty'=>'message', 'message'=>'<strong>Error!</strong>&nbsp; Message body is empty.' );
         echo json_encode($result );
         die;
    }



    $headers  = "From: " . $name . ' <' . $email . '>' . "\r\n";
    $headers .= "Reply-To: ". $email . "\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/html; charset=UTF-8\r\n";


    $templateTags =  array(
        '{{subject}}' => $subject,
        '{{email}}'=>$email,
        '{{phone}}'=>$phone,
        '{{name}}'=>$name,
        '{{message}}'=>$message
        );


    $templateContents = file_get_contents( dirname(__FILE__) . '/email-templates/'.$email_template);

    $contents =  strtr($templateContents, $templateTags);

    if ( mail( $to, $subject, $contents, $headers ) ) {
        $result = array( 'response' => 'success', 'message'=>'<strong>Thank You!</strong>&nbsp; Your email has been delivered.' );
    } else {
        $result = array( 'response' => 'error', 'message'=>'<strong>Error!</strong>&nbsp; Cann\'t Send Mail.'  );
}


    echo json_encode( $result );

    die;

HTML FORM - Here is my HTML CODE. Could there be something wrong with my form? I however think it's the PHP, or something doesn't match.

<!-- Contact Section -->
    <section id="contact" class="contact-section section-padding">
      <div class="container">
        <h2 class="section-title wow fadeInUp">Get in touch</h2>

        <div class="row">
          <div class="col-md-6">
            <div class="contact-form">
              <strong>Send me a message</strong>

              <form name="contact-form" id="contactForm" action="sendemail.php" method="POST">

                <div class="form-group">
                  <label for="name">Name</label>
                  <input type="text" name="name" class="form-control" id="name" required="">
                </div>

                <div class="form-group">
                  <label for="email">Email</label>
                  <input type="email" name="email" class="form-control" id="email" required="">
                </div>

                <div class="form-group">
                  <label for="phone">Phone</label>
                  <input type="phone" name="phone" class="form-control" id="phone" required="">
                </div>

                <div class="form-group">
                  <label for="subject">Subject</label>
                  <input type="text" name="subject" class="form-control" id="subject">
                </div>

                <div class="form-group">
                  <label for="message">Message</label>
                  <textarea name="message" class="form-control" id="message" rows="5" required=""></textarea>
                </div>

                <button type="submit" name="submit" class="btn btn-primary">Send Message</button>
              </form>
            </div><!-- /.contact-form -->
          </div><!-- /.col-md-6 -->

          <div class="col-md-6">
            <div class="row center-xs">
              <div class="col-sm-6">
                <i class="fa fa-map-marker"></i>
                <address>
                  <strong>Address/Street</strong>
                  Find me online,<br>
                  Anywhere, Really<br>
                </address>
              </div>

              <div class="col-sm-6">

              </div>
            </div>

          <div class="row">
            <div class="col-sm-12">
              <!--div class="location-map">
                <div id="mapCanvas" class="map-canvas"></div>
              </div-->
            </div>
          </div>

          </div>
        </div><!-- /.row -->
      </div><!-- /.container -->
    </section><!-- End Contact Section -->

error message shown

Network response

Anyone with a clue what could be wrong?

Carol Kariuki
  • 95
  • 1
  • 1
  • 8

2 Answers2

1

The reason you are getting a 200 error is due to you catching the error yourself and echoing the output. If you want the error to be as a 500, maybe try adding this in the error if/else:

header($_SERVER["SERVER_PROTOCOL"] . ' 500 Internal Server Error', true, 500);  
exit;

To debug the error from the mail function itself, this post explains:

$success = mail('example@example.com', 'My Subject', $message);
if (!$success) {
    $errorMessage = error_get_last()['message'];
}

How can I get the error message for the mail() function?

PHPology
  • 1,017
  • 6
  • 12
  • So the error reason I am getting now is a Null... response: "error" message: "Error!  Cann't Send Mail." reason: null could it be something in my form? – Carol Kariuki Jan 03 '20 at 14:27
  • hey Carol, are you on shared hosting? Could some of the ports be blocked? – PHPology Jan 03 '20 at 15:10
  • Also, another way is, could you check if option 2 on this URL works for you: https://www.php.net/manual/en/function.mail.php – PHPology Jan 03 '20 at 15:10
  • Yes, I am on shared hosting. Option 2 gives no errors, but still can't see any emails. – Carol Kariuki Jan 03 '20 at 15:45
  • You might want to ask your hosting company to see if they are blocking ports. If they are, this would prevent emails from being sent out. Maybe a good route would be to see if you are able to use PHPMailer via an SMTP server – PHPology Jan 03 '20 at 16:48
  • Great. Thanks PHPology, let me check those. Will give feedback. – Carol Kariuki Jan 03 '20 at 19:16
  • 1
    Thanks @PHPology! Actually got it working using PHPMailer via SMTP server. That's what my hosting company suggested as well. – Carol Kariuki Jan 14 '20 at 12:06
0

Your script respond HTTP 200, with error cause mail function doesn't success. Try replace line:

$result = array( 'response' => 'error', 'message'=>'<strong>Error!</strong>&nbsp; Cann\'t Send Mail.'  );

with:

$result = array( 'response' => 'error', 'message'=>'<strong>Error!</strong>&nbsp; Cann\'t Send Mail.', 'reason' => error_get_last()['message'] );

added in to array, what will show the error

'reason' => error_get_last()['message']

and you will see why your mail doesn't sent.

I hope it help to you :)

Aurimas
  • 138
  • 7
  • Now I see this as my error message: Seems like the form doesn't capture the details :( response: "error" message: "Error!  Cann't Send Mail." reason: null – Carol Kariuki Jan 03 '20 at 14:22
  • try replace the reason to: 'reason' => print_r(error_get_last(), true) and please look at the logs.. what you can see in here.. – Aurimas Jan 03 '20 at 14:36
  • I get this now: 0: true response: "error" message: "Error!  Cann't Send Mail." reason: true – Carol Kariuki Jan 03 '20 at 14:48
  • from chrome this is what I keep seeing: [Intervention] Unable to preventDefault inside passive event listener due to target being treated as passive. – Carol Kariuki Jan 03 '20 at 15:06
  • i mean servers logs, chrome logs is not responde for mail sending, from servers logs you can see is mail port disabled or not. – Aurimas Jan 06 '20 at 10:14