0

I'm trying to create a contact form on my Bootstrap 4 website but I get a "This page isn’t working" error page when clicking submit button on the form. I've tried all my best to edit the rd-mailform.php it looks like I won't be able to solve this. I'll appreaciate any help. Thanks.

My modal form code:

<form class="rd-mailform text-left" data-form-output="form-output-global" data-form-type="contact" method="post" action="bat/rd-mailform.php">
                <div class="form-wrap">
                  <label class="form-label" style="color: white;" for="contact-name">NAME<span class="req-symbol">*</span><span class="fa fa-user pull-right"></span> </label>
                  <input class="form-input" id="contact-name" type="text" name="name" data-constraints="@Required">
                </div>
                <div class="form-wrap">
                  <label class="form-label" style="color: white;" for="contact-email">E-MAIL<span class="req-symbol">*</span><span class="fa fa-envelope pull-right"></span></label>
                  <input class="form-input" id="contact-email" type="email" name="email" data-constraints="@Required @Email">
                </div>
                <div class="form-wrap">
                  <label class="form-label" style="color: white;" for="contact-phone">PHONE<span class="req-symbol">*</span><span class="fa fa-mobile pull-right"></span></label>
                  <input class="form-input" id="contact-phone" type="text" name="phone" data-constraints="@Required @PhoneNumber">
                </div>
                <div class="form-wrap">
                  <label class="form-label" style="color: white;" for="contact-email">COMPANY NAME<span class="fa fa-copyright pull-right"></span></label>
                  <input class="form-input" id="contact-company" type="text" name="company" data-constraints="@Required">
                </div>                
                <div class="form-wrap">
                  <label class="form-label" style="color: white;" for="contact-about">ABOUT<span class="fa fa-book pull-right"></span></label>
                  <textarea class="form-input" id="contact-message" name="about" data-constraints="@Required"></textarea>
                </div>
                <div class="form-wrap">
                  <select class="form-input custom-label" id="plan" name="plan" data-constraints="@plan">
                    <option>CHOOSE PLAN</option>
                    <option>Basic</option>
                    <option>Standard</option>
                    <option>Business</option>
                  </select>
                </div>
                <!--Google captcha-->
                <div class="form-wrap text-left form-validation-left">
                  <div class="recaptcha" id="captcha1" data-sitekey="6LfZlSETAAAAAC5VW4R4tQP8Am_to4bM3dddxkEt"></div>
                </div>
                <div class="form-button group-sm text-center text-lg-left">
                  <button class="button button-primary-bright" type="submit">Submit</button>
                </div>
              </form>

rd-mailform.php

<?php

$formConfigFile = file_get_contents("rd-mailform.config.json");
$formConfig = json_decode($formConfigFile, true);

date_default_timezone_set('Lagos/UTC+1');

try {
    require './phpmailer/PHPMailerAutoload.php';

    $recipients = $formConfig['services@tdmstudios.com.ng', 'tdmstudiosofficial@gmail.com'];

    preg_match_all("/([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)/" , $recipients, $addresses, PREG_OFFSET_CAPTURE);

    if (!count($addresses[0])) {
        die('MF001');
    }

    function getRemoteIPAddress() {
        if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
            return $_SERVER['HTTP_CLIENT_IP'];

        } else if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            return $_SERVER['HTTP_X_FORWARDED_FOR'];
        }
        return $_SERVER['REMOTE_ADDR'];
    }

    if (preg_match('/^(127\.|192\.168\.|::1)/', getRemoteIPAddress())) {
        die('MF002');
    }

    $template = file_get_contents('rd-mailform.tpl');

    if (isset($_POST['form-type'])) {
        switch ($_POST['form-type']){
            case 'contact':
                $subject = 'A message from your site visitor';
                break;
            case 'subscribe':
                $subject = 'Subscribe request';
                break;
            case 'order':
                $subject = 'Order request';
                break;
            default:
                $subject = 'A message from your site visitor';
                break;
        }
    }else{
        die('MF004');
    }

    if (isset($_POST['email'])) {
        $template = str_replace(
            array("<!-- #{FromState} -->", "<!-- #{FromEmail} -->"),
            array("Email:", $_POST['email']),
            $template);
    }

    if (isset($_POST['message'])) {
        $template = str_replace(
            array("<!-- #{MessageState} -->", "<!-- #{MessageDescription} -->"),
            array("Message:", $_POST['message']),
            $template);
    }

    // In a regular expression, the character \v is used as "anything", since this character is rare
    preg_match("/(<!-- #\{BeginInfo\} -->)([^\v]*?)(<!-- #\{EndInfo\} -->)/", $template, $matches, PREG_OFFSET_CAPTURE);
    foreach ($_POST as $key => $value) {
        if ($key != "counter" && $key != "email" && $key != "message" && $key != "form-type" && $key != "g-recaptcha-response" && !empty($value)){
            $info = str_replace(
                array("<!-- #{BeginInfo} -->", "<!-- #{InfoState} -->", "<!-- #{InfoDescription} -->"),
                array("", ucfirst($key) . ':', $value),
                $matches[0][0]);

            $template = str_replace("<!-- #{EndInfo} -->", $info, $template);
        }
    }

    $template = str_replace(
        array("<!-- #{Subject} -->", "<!-- #{SiteName} -->"),
        array($subject, $_SERVER['SERVER_NAME']),
        $template);

    $mail = new PHPMailer();


    if ($formConfig['useSmtp']) {
        //Tell PHPMailer to use SMTP
        $mail->isSMTP();

        //Enable SMTP debugging
        // 0 = off (for production use)
        // 1 = client messages
        // 2 = client and server messages
        $mail->SMTPDebug = 0;

        $mail->Debugoutput = 'html';

        // Set the hostname of the mail server
        $mail->Host = $formConfig['mail.tdmstudios.com.ng'];

        // Set the SMTP port number - likely to be 25, 465 or 587
        $mail->Port = $formConfig['465'];

        // Whether to use SMTP authentication
        $mail->SMTPAuth = true;
        $mail->SMTPSecure = "ssl";

        // Username to use for SMTP authentication
        $mail->Username = $formConfig['username'];

        // Password to use for SMTP authentication
        $mail->Password = $formConfig['password'];
    }

    $mail->From = $_POST['services@tdmstudios.com.ng'];

    # Attach file
    if (isset($_FILES['file']) &&
        $_FILES['file']['error'] == UPLOAD_ERR_OK) {
        $mail->AddAttachment($_FILES['file']['tmp_name'],
            $_FILES['file']['name']);
    }

    if (isset($_POST['name'])){
        $mail->FromName = $_POST['name'];
    }else{
        $mail->FromName = "TDM STUDIOS";
    }

    foreach ($addresses[0] as $key => $value) {
        $mail->addAddress($value[0]);
    }

    $mail->CharSet = 'utf-8';
    $mail->Subject = $subject;
    $mail->MsgHTML($template);
    $mail->send();

    die('MF000');
} catch (phpmailerException $e) {
    die('MF254');
} catch (Exception $e) {
    die('MF255');
}
  • 1
    There's a high chance you have an error in your PHP code but it's being hidden from you. Check your web server's error log for details. Also, please see [How can I get useful error messages in PHP?](https://stackoverflow.com/questions/845021/how-can-i-get-useful-error-messages-in-php) – Phil Aug 22 '19 at 04:12
  • You have a syntax error on this line ~ `$recipients = $formConfig['services@tdmstudios.com.ng', 'tdmstudiosofficial@gmail.com'];`. Did you perhaps mean `$recipients = ['services@tdmstudios.com.ng', 'tdmstudiosofficial@gmail.com'];`? – Phil Aug 22 '19 at 04:14
  • you need to add in your php file `error_reporting(1);` it will show php syntax error due to that you are getting error. – Naveed Ramzan Aug 22 '19 at 06:24
  • 1
    @NaveedRamzan that is exactly what _won’t_ work for syntax errors. A syntax error means your whole script doesn’t execute – so `error_reporting(1);` won’t execute either in this case. Proper PHP error reporting for development purposes has to be set in the general PHP configuration, not attempted from inside scripts when it might be too late. – misorude Aug 22 '19 at 07:37
  • @Phil, your answer appears helpful. After changing the code as you suggested, I get a white blank page with "MF001" printed at the top left corner. – Ajayi Olamide Aug 22 '19 at 16:53
  • @Phil, the error I got looks like this 'Warning: preg_match_all() expects parameter 2 to be string, array given in /home2/tdmstudi/public_html/bat/rd-mailform.php on line 13 Warning: count(): Parameter must be an array or an object that implements Countable in /home2/tdmstudi/public_html/bat/rd-mailform.php on line 15 MF001' – Ajayi Olamide Aug 22 '19 at 17:14
  • Do those error messages not explain the problem? – Phil Aug 22 '19 at 21:19
  • Possibly yes. I just don't know how to fix it. This is what I have on lines 13-17:[code] preg_match_all("/([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)/" , $recipients, $addresses, PREG_OFFSET_CAPTURE); if (!count($addresses[0])) { die('MF001'); }[code] I'm not very good at using this platform yet. Kindly forgive the way I share my codes. – Ajayi Olamide Aug 23 '19 at 01:03
  • `$recipients` is an array (that you defined). [`preg_match_all()`](https://www.php.net/manual/function.preg-match-all.php) expects parameter 2 to be a string – Phil Aug 23 '19 at 05:23

0 Answers0