0

Apologies, I'm mostly self taught. I'm using the following code live on https://www.poadvisory.com/contact.html and the contact form appears to be working, ReCaptcha as well, but the email never comes. I've spent countless times on the phone with my hosting provider and they claim is a code issue and not a server issue. Surely it's something obvious I don't see. If anyone can help, I'd really appreciate it. I've searched the current questions but don't believe this is a duplicate question.

<?php
require('recaptcha-master/src/autoload.php');


$sendTo = 'Contact Form < my email goes here>';

$subject = 'New message from contact form';

$fields = array('name' => 'Name', 'surname' => 'Surname', 'phone' => 'Phone', 'email' => 'Email', 'message' => 'Message');

$okMessage = 'Contact form successfully submitted. Thank you, We will get back to you soon!';

$errorMessage = 'There was an error while submitting the form. Please try again later';


$recaptchaSecret = 'removed';


error_reporting(E_ALL & ~E_NOTICE);

try {
    if (!empty($_POST)) {

        
        if (!isset($_POST['g-recaptcha-response'])) {
            throw new \Exception('ReCaptcha is not set.');
        }
        
        $recaptcha = new \ReCaptcha\ReCaptcha($recaptchaSecret, new \ReCaptcha\RequestMethod\CurlPost());
        
        
        $response = $recaptcha->verify($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']);

        if (!$response->isSuccess()) {
            throw new \Exception('ReCaptcha was not validated.');
        }
        
        
        $emailText = "You have a new message from your contact form\n=============================\n";

        foreach ($_POST as $key => $value) {
            if (isset($fields[$key])) {
                $emailText .= "$fields[$key]: $value\n";
            }
        }
    
        $headers = array('Content-Type: text/plain; charset="UTF-8";',
            'From: ' . $from,
            'Reply-To: ' . $from,
            'Return-Path: ' . $from,
        );
        
        mail($sendTo, $subject, $emailText, implode("\n", $headers));

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

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 {
    echo $responseArray['message'];
}
  • 1
    Are you sure it's the mail function that fails? Start small and create a simple test script that just sends mail and see if that works. – j08691 Mar 28 '22 at 19:01
  • Where are you setting the `$from` variable? What debugging have you done? What's in your error log? I would also recommend you to use one of the tried and tested mail libraries, like PHPMailer, SwiftMailer or similar instead of the low level `mail()`-function. Those libraries doesn't only give you a much easier and more readable API, they also makes your code much more portable since you then configure the SMTP settings (which is recommended to use) in your code instead of needing to configure php.ini to be able to send emails. – M. Eriksson Mar 28 '22 at 19:11
  • *"but the email never comes"* this a black-box debugging problem since we have no idea what happens on your hosting provider's end. – GetSet Mar 28 '22 at 19:11
  • @M.Eriksson Small fyi, "_Swiftmailer is not maintained anymore. Use Symfony Mailer instead_" from their homepage – brombeer Mar 28 '22 at 19:20
  • @brombeer - Ah, did not know that. Thanks. – M. Eriksson Mar 28 '22 at 19:31
  • @M.Eriksson I actually deleted the from variable. I thought it was not necessary...perhaps that is the issue. I pulled this code from an online source, no error logs, but perhaps is a reflection of my experience level. This was my source: https://bootstrapious.com/p/bootstrap-recaptcha#google_vignette – Sarah Paul Moore Mar 28 '22 at 19:42
  • Yes, that is an issue. It should throw an "undefined variable" notice/warning since you still _use_ that variable in your code. Also, emails _must_ have a from address. Also, change the error reporting to `error_reporting(E_ALL);` to bet _all_ errors and notices (important when debugging). It should also be in the top of your file to catch _all_ issues. Best is if you enable it in your php.ini file instead of in your code. – M. Eriksson Mar 28 '22 at 19:44
  • 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) – M. Eriksson Mar 28 '22 at 19:47

0 Answers0