0

We're having trouble getting our free estimate request form to function correctly. You can see our most current build here: http://tosa.atwebpages.com/free-estimate.html

We have wrapped all of our inputs in

<form id="free-estimate" method="post" name="contact.php" role="form" class="needs-validation" novalidate>

Here is the code from our contact.php

<?php
/*
 *  CONFIGURE EVERYTHING HERE
 */

// an email address that will be in the From field of the email.
$from = 'donations.co@theothersideacademy.com';

// an email address that will receive the email with the output of the form
$sendTo = 'donations.co@theothersideacademy.com';

// subject of the email
$subject = 'New Moving and Storage Estimate Request';

// form field names and their translations.
// array variable name => Text to appear in the email
$fields = array('name' => 'Name', 'surname' => 'Surname', 'need' => 'Need', 'email' => 'Email', 'message' => 'Message'); 

// message that will be displayed when everything is OK :)
$okMessage = 'Thank you for submitting your request! We will contact you soon.';

// If something goes wrong, we will display this message.
$errorMessage = 'There was an error while submitting the form. Please try again later';

/*
 *  LET'S DO THE SENDING
 */

// if you are not debugging and don't need error reporting, turn this off by error_reporting(0);
error_reporting(E_ALL & ~E_NOTICE);

try
{

    if(count($_POST) == 0) throw new \Exception('Form is empty');
            
    $emailText = "You have a new request for moving and storage\n";

    foreach ($_POST as $key => $value) {
        // If the field exists in the $fields array, include it in the email 
        if (isset($fields[$key])) {
            $emailText .= "$fields[$key]: $value\n";
        }
    }

    // All the neccessary headers for the email.
    $headers = array('Content-Type: text/plain; charset="UTF-8";',
        'From: ' . $from,
        'Reply-To: ' . $from,
        'Return-Path: ' . $from,
    );
    
    // Send email
    mail($sendTo, $subject, $emailText, implode("\n", $headers));

    $responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (\Exception $e)
{
    $responseArray = array('type' => 'danger', 'message' => $errorMessage);
}


// if requested by AJAX request return JSON response
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
    $encoded = json_encode($responseArray);

    header('Content-Type: application/json');

    echo $encoded;
}
// else just display the message
else {
    echo $responseArray['message'];
}

The problem we are having is that we never receive an email once the form has been submitted. Everything appears to be working correctly as we don't see any errors in the console and we receive the success message ($okMessage)

We have followed the tutorial from here: https://bootstrapious.com/p/how-to-build-a-working-bootstrap-contact-form

One thing I have noticed when testing the form is that when we submit, it validates the form with the green arrows, then shows our $okMessage ("Thank you for submitting your request! We will contact you soon."), but also shows that the (now) empty forms need to be filled out. example

We are using awardspace.com for our demo server if that is relevant.

tosaDevs
  • 19
  • 4
  • Check the mail configuration in your `php.ini`. – Barmar Jun 28 '21 at 22:57
  • Just a thought: you could use PHPMailer which is much more simple and effiecient. – AD-1 Jun 29 '21 at 04:58
  • Does this answer your question? [PHP mail function doesn't complete sending of e-mail](https://stackoverflow.com/questions/24644436/php-mail-function-doesnt-complete-sending-of-e-mail) – CBroe Jun 29 '21 at 07:01

0 Answers0