Home > front end >  Form seems not to work anymore - not sending mails either
Form seems not to work anymore - not sending mails either

Time:09-23

a friend of mine has a side business and an associated website. the person who always took care of his website is no longer working with him and since he has 0.0 clue about all this, he has turned to me. I've been looking for the problem for 1 1/2 days now and can't find it.

At the bottom of the page is a small form to enter contact information and receive a mail. however, clicking on the "submit" button no longer displays a confirmation, nor does it send a mail via phpmailer. the page is reloaded afterwards, but the input is still there. That captcha seems not to do anything too, because the form has the same behavior with or without the captcha.

Form

This is the script for the mails.

<?php
    
/**
* Get Template Text 
*/
function getTemplateText($text, $center = false) {
    $html = '<table border="0" cellpadding="0" cellspacing="0" width="100%"  style="min-width:100%;">';
        $html .= '<tr>';
            $html .= '<td  style="padding-bottom: 15px;'.($center ? ' text-align:center;' : '').'">'.$text.'</td>';
        $html .= '</tr>';
    $html .= '</table>';
    
    return $html;
}

$isMailError = false;
if(isset($_POST) && isset($_POST['email'])) {
    if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])) {
        //your site secret key
        $secret = 'secret';
        //get verify response data
        $verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']);
        $responseData = json_decode($verifyResponse);
        if($responseData->success) {
            require_once('../_mailer/class.phpmailer.php');
            $mailer = new PHPMailer;
            
            $mailer->IsSMTP(); //per SMTP verschicken
            $mailer->Host     = SMTP_SERVER; //SMTP-Server
            $mailer->SMTPAuth = true; //SMTP mit Authentifizierung benutzen
            $mailer->Username = SMTP_USER; //SMTP-Benutzername
            $mailer->Password = SMTP_PASSWORD; //SMTP-Passwort
        
            $mailer->CharSet = 'utf-8';
            
            $mailer->FromName = NOREPLY_MAIL_NAME;
            $mailer->From = NOREPLY_MAIL;
            $mailer->Sender = NOREPLY_MAIL;
            
            //$mailer->WordWrap = 50;
            $mailer->IsHTML(true);
            
            $data = array(
                'company' => '',
                'name' => '',
                'email' => '',
                'phone' => '',
                'message' => '',
                'page_uri' => ''
            );
            
            foreach($_POST as $i => $v) {
                if(isset($data[$i])) {
                    $data[$i] = $v;
                }
            }
            $data['message'] = nl2br($data['message']);
            
            $mailer->AddAddress(CONTACT_MAIL); //Add a recipient
            $mailer->Subject = 'SUBJECT changed';
            $msg = '<p>Vielen Dank für Ihre Anfrage, wir werden uns in kürze bei Ihnen melden.</p>';
            $msg .= '<p><strong>Ihre Anfrage im Detail:</strong></p>';
            $msg .= '<p>';
                $msg .= '<strong>Firma:</strong> '.$data['company'].'<br/>';
                $msg .= '<strong>Name:</strong> '.$data['name'].'<br/>';
                $msg .= '<strong>E-Mail:</strong> '.$data['email'].'<br/>';
                $msg .= '<strong>Telefon:</strong> '.$data['phone'].'<br/>';
                $msg .= '<strong>Nachricht:</strong><br/>'.$data['message'].'</p>';
                
            $mailer->Body = getTemplateText($msg.'<strong>Seite:</strong> '.$data['page_uri'].'</p>');
            $mail_send = $mailer->Send() ? true : false;
            $mailer->clearAllRecipients();
            $mailer->Body = getTemplateText($msg);
            $mailer->Subject = 'subject changed';
            $mailer->AddAddress($data['email']); //Add a recipient
            $mail_send = $mailer->Send() ? true : false;
            
            $_POST = array();
            echo '<div id="anfrage_success"></div>';
        } else {
            $isMailError = true;
        }
    } else {
        $isMailError = true;
    }
}

I just dont know what to do anymore. Has anyone an idea why it seems to stoped working? We did not change anything.

Here are the last error logs I was able to get from the webhosting:

[Thu Sep 22 01:00:23 2022] [ssl:warn] [pid 11739] [client [host AH01909: ****.com:443:0 server certificate does NOT include an ID which matches the server name [Thu Sep 22 16:02:48 2022] [ssl:warn] [pid 11739] [client [host ****: ****:443:0 server certificate does NOT include an ID which matches the server name [Thu Sep 22 16:04:34 2022] [ssl:warn] [pid 11739] [client [host ****: ****:443:0 server certificate does NOT include an ID which matches the server name [Thu Sep 22 16:04:59 2022] [ssl:warn] [pid 11739] [client [host ****: ****.com:443:0 server certificate does NOT include an ID which matches the server name [Thu Sep 22 18:06:40 2022] [autoindex:error] [pid 23089] [client ****] [host ****.server-he.de] : Cannot serve directory /is/htdocs/_E1ZMITARW2/www/: No matching DirectoryIndex (index.html,index.htm,index.shtml,index.php,index.php5,index.wml,index.xml) found, and server-generated directory index forbidden by Options directive

CodePudding user response:

Vielen Dank für Ihre Anfrage.

Please do not share your reCAPTCHA secret key!

EDIT:

You will need to reset and update the reCAPTCHA keys since the secret key was posted publicly here.

I would suggest that if nothing has changed recently in that particular form template and it suddenly stopped working (but the reCAPTCHA still validates) then there is likely an issue higher up in system.

First let's rule out the alternative - that it's a problem in the browser: open your developer console in your browser and see whether there is an error either when the form loads or when you submit the form. If so, edit your question to include the error.

If there's no error, you'll want to take a look next in the PHP logs. Do you have access to the hosting solution where the form is hosted? I would assume so, since you have the PHP.

EDIT 2:

If you can't find your PHP logs in the control panel for the hosting, please create a file on the server with:

<?php phpinfo(); ?>

and then run it in the browser to see where your log is.

Then please review the log entries from exactly the moment you send the form and record the output here. Obviously you should obfuscate any information like email addresses, host names, passwords, etc.

CodePudding user response:

if you can't get to the logs, try turning on error reporting at the top of the page:

ini_set('display_errors', 1);
error_reporting(E_ALL);

and set it to exit(); before the page redirect, so that you can look at any errors in the browser without it redirecting the page.

if that doesn't display anything, try testing each suspected failure point. for instance, put an echo after the captcha check to see if it is passing. then put the entire php mailer code into a try/catch block, to catch any exceptions and print them to the browser.

try {
  $mail->AddReplyTo('[email protected]', 'First Last');
  $mail->AddAddress('[email protected]', 'John Doe');
  $mail->SetFrom('[email protected]', 'First Last');
  $mail->AddReplyTo('[email protected]', 'First Last');
  $mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
  $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
  $mail->MsgHTML(file_get_contents('contents.html'));
  $mail->AddAttachment('images/phpmailer.gif');      // attachment
  $mail->AddAttachment('images/phpmailer_mini.gif'); // attachment
  $mail->Send();
  echo "Message Sent OK\n";
} catch (phpmailerException $e) {
  echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
  echo $e->getMessage(); //Boring error messages from anything else!
}

make sure to turn off error reporting etc after you figure out the issue. phpMailer stuff should always be in a try catch block, so it can fail gracefully anyways if it can't connect to SMTP or whatever. original coder dropped the ball on this.

my guess is it has something to do with connecting to SMTP or authenticating that email/password to send the mail.

  • Related