1
votes

I'm getting following PHP Errors when using a Contact form, which worked fine until the client said that they aren't receiving emails since a week. As a friend helped me with the backend, and hes not available right now to ask, I wanted to ask kindly if you could give me a hint what I would need to fix.

The Errors:

`[Wed Feb 17 16:43:44 2016] [warn] [client 37.24.118.150] mod_fcgid: stderr: PHP Notice: Undefined variable: headers in /var/www/vhosts/wscgmbh.de/httpdocs/php/contact-form-handler.php on line 29, referer: http://www.wscgmbh.de/contact.html

[Wed Feb 17 16:43:44 2016] [warn] [client 37.24.118.150] mod_fcgid: stderr: PHP Warning: mail(): Multiple or malformed newlines found in additional_header in /var/www/vhosts/wscgmbh.de/httpdocs/php/contact-form-handler.php on line 32, referer: http://www.wscgmbh.de/contact.html

[Wed Feb 17 16:45:18 2016] [warn] [client 37.24.118.150] mod_fcgid: stderr: PHP Notice: Undefined variable: headers in /var/www/vhosts/wscgmbh.de/httpdocs/php/contact-form-handler.php on line 29, referer: http://www.wscgmbh.de/contact.html

[Wed Feb 17 16:45:18 2016] [warn] [client 37.24.118.150] mod_fcgid: stderr: PHP Warning: mail(): Multiple or malformed newlines found in additional_header in /var/www/vhosts/wscgmbh.de/httpdocs/php/contact-form-handler.php on line 32, referer: http://www.wscgmbh.de/contact.html

[Wed Feb 17 17:02:25 2016] [warn] [client 37.24.118.150] mod_fcgid: stderr: PHP Notice: Undefined index: name in /var/www/vhosts/wscgmbh.de/httpdocs/php/contact-form-handler.php on line 12

[Wed Feb 17 17:02:25 2016] [warn] [client 37.24.118.150] mod_fcgid: stderr: PHP Notice: Undefined index: email in /var/www/vhosts/wscgmbh.de/httpdocs/php/contact-form-handler.php on line 13

[Wed Feb 17 17:02:25 2016] [warn] [client 37.24.118.150] mod_fcgid: stderr: PHP Notice: Undefined index: message in /var/www/vhosts/wscgmbh.de/httpdocs/php/contact-form-handler.php on line 14

[Thu Feb 18 09:40:15 2016] [warn] [client 213.23.122.15] mod_fcgid: stderr: PHP Notice: Undefined variable: headers in /var/www/vhosts/wscgmbh.de/httpdocs/php/contact-form-handler.php on line 29, referer: http://www.wscgmbh.de/contact.html

[Thu Feb 18 09:40:15 2016] [warn] [client 213.23.122.15] mod_fcgid: stderr: PHP Warning: mail(): Multiple or malformed newlines found in additional_header in /var/www/vhosts/wscgmbh.de/httpdocs/php/contact-form-handler.php on line 32, referer: http://www.wscgmbh.de/contact.html`


This is the Code of the form-handler which worked the whole time, but suddenly doesnt

<meta http-equiv="Content-Type" content="text/html"; charset="utf-8" />
 <?php 
$errors = '';
$myemail = '[email protected]';
if(empty($_POST['name'])  || 
empty($_POST['email']) || 
empty($_POST['message']))
{
$errors .= "\n Error: all fields are required";
}

$name = $_POST['name']; 
$email_address = $_POST['email']; 
$message = $_POST['message']; 

if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]    {2,3})$/i", 
$email_address))
{
$errors .= "\n Error: Invalid email address";
}

if( empty($errors))
{
$to = $myemail; 
$email_subject = "Sie haben eine neue Nachricht von: $name";
$email_body = "Sie haben eine neue Nachricht erhalten, hier sind die     Details: \n\nName: $name \nEmail: $email_address \nNachricht: \n$message"; 

$headers [] .= 'From: WSC-Kontaktformular' . "\r\n";
$headers [] = "Antworten Sie: {$email_address}";

mail($to,$email_subject,$email_body,implode("\r\n",$headers));
//redirect to the 'thank you' page

echo '<script> 
        alert("Danke für Ihre Nachricht. Wir werden uns bald bei  melden!");
        window.location = "http://www.wscgmbh.de"
    </script>';
 } 
 ?>

Im not an expert and PHP, so I really hope somebody could help me! Thank you very much in advance!!!

1
I posted an answer a while ago but noticed something else in your code, the use of Antworten Sie: in the headers which seems to translate to Reply (to):. I have edited my answer about it near the bottom of my answer and you need to reload it under Edit...Funk Forty Niner

1 Answers

1
votes

The problem here is that you're missing (and declare the headers as an array):
(Also consult my Edit below in regards to Antworten Sie:).

$headers   = array();

to be placed above:

$headers [] .= 'From: WSC-Kontaktformular' . "\r\n";
$headers [] = "Antworten Sie: {$email_address}";

You also need to remove the dot (concatenate) and the . "\r\n".

The implode("\r\n",$headers) will take care of it.

$headers   = array();
$headers [] = 'From: WSC-Kontaktformular';
$headers [] = "Antworten Sie: {$email_address}";

As per the mail manual's example:

$headers   = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/plain; charset=iso-8859-1";
$headers[] = "From: Sender Name <[email protected]>";
$headers[] = "Bcc: JJ Chong <[email protected]>";
$headers[] = "Reply-To: Recipient Name <[email protected]>";
$headers[] = "Subject: {$subject}";
$headers[] = "X-Mailer: PHP/".phpversion();

mail($to, $subject, $email, implode("\r\n", $headers));

Plus, From: expects an email and not a name, so mail may end up in spam because of it.

  • Use an email address.

Reference:


If you want to use dots (concatenates) and \r\n's, then you need to use the following instead, and as per an example from the manual:

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'To: Mary <[email protected]>, Kelly <[email protected]>' . "\r\n";
$headers .= 'From: Birthday Reminder <[email protected]>' . "\r\n";
$headers .= 'Cc: [email protected]' . "\r\n";
$headers .= 'Bcc: [email protected]' . "\r\n";

// Mail it
mail($to, $subject, $message, $headers);

Edit:

I noticed the words Antworten Sie: which in German seems to mean "reply" (when I visited Google Translate).

If that is intended to be a From: or Reply-to, then you can't use that as part of the mail header, but as Reply-To: as per the manual states:

Reply-To: [email protected]

Therefore, your code needs to read as:

$headers   = array();
$headers [] = 'From: WSC-Kontaktformular';
$headers [] = "Reply-To: {$email_address}";

That would also be a contributing factor to the headers failing.

Change mail($to,$email_subject,$email_body,implode("\r\n",$headers));

to:

if(mail($to,$email_subject,$email_body,implode("\r\n",$headers))) { 
      echo "Mail sent."; 
   } else { 
      echo "Error."; 
   }

If you see "Mail sent", then mail() has done its job. See your spam also.

Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Then the rest of your code

Sidenote: Displaying errors should only be done in staging, and never production.


Consider using PHPMailer, or Swiftmailer:

which are alternatives to PHP's mail() function.