0

I have a HTML/PHP contact form which I set up on a website about 2 years ago. I had been working fine initially but now it seems the emails are not being sent/received.

I have updated nothing my end so just wondered if I need to update it in any way to get it working again?

My code is pasted below...

 <form id="contact-form" method="post" action="contact.php" role="form">

<div class="messages"></div>

<div class="controls">
    <div class="row">
        <div class="col-md-8">
            <div class="form-group">
                <label for="form_name">Name *</label>
                <input id="form_name" type="text" name="name" class="form-control" placeholder="Please enter your name *" required data-error="Your name is required.">
                <div class="help-block with-errors"></div>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-md-8">
            <div class="form-group">
                <label for="form_email">Email *</label>
                <input id="form_email" type="email" name="email" class="form-control" placeholder="Please enter your email *" required data-error="Valid email is required.">
                <div class="help-block with-errors"></div>
            </div>
        </div>
        <div class="col-md-8">
            <div class="form-group">
                <label for="form_phone">Phone</label>
                <input id="form_phone" type="tel" name="phone" class="form-control" placeholder="Please enter your phone">
                <div class="help-block with-errors"></div>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-md-10">
            <div class="form-group">
                <label for="form_message">Message *</label>
                <textarea id="form_message" name="message" class="form-control" placeholder="Type your message here *" rows="10" required data-error="Please,leave us a message."></textarea>
                <div class="help-block with-errors"></div>
            </div>
        </div>
        <div class="col-md-12">
            <input type="submit" class="btn btn-success btn-send" value="Send message">
        </div>
    </div>
    <div class="row">
        <div class="col-md-12">
   </div>
    </div>
    </div>
    </form>

PHP Code

<?php
/*
*  CONFIGURE EVERYTHING HERE
*/

// an email address that will be in the From field of the email.
$from = 'me@myselfcom';

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

// subject of the email
$subject = 'New message from contact form';

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

// message that will be displayed when everything is OK :)
$okMessage = 'Thank you for getting in touch, we will get back to 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 message from your contact form\n=============================\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'];
}

Any help/advice/suggestions would be greatly appreciated.

Thanks,

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Chris
  • 199
  • 2
  • 6
  • 19
  • 1
    The `mail()` method uses the systems underlying sendmail facility to send mail. It is possible that a system upgrade broke your servers sendmail configuration. You need ssh access to the server to test. – Alex Barker Nov 27 '19 at 19:36
  • Thanks, how do I get this? I think the website is hosted with GoDaddy – Chris Nov 27 '19 at 19:44
  • You will need to contact GoDaddy... My best guess is they turned it off because of abuse. – Alex Barker Nov 27 '19 at 19:45
  • thanks, spoke to them, they said i needed to change the 'subject' line in the PHP to something less generic because they could see that was what was blocking it. Seems a weird request, hence i tried it and it didnt workk – Chris Nov 27 '19 at 21:12
  • It sounds like it was related to spam filtering. Glad you got it resolved. – Alex Barker Nov 27 '19 at 21:34

0 Answers0