1

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 = 'service@wscgmbh.de';
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!!!

carlpoppa
  • 115
  • 1
  • 10
  • 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 Feb 18 '16 at 16:49

1 Answers1

1

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 <sender@domain.com>";
$headers[] = "Bcc: JJ Chong <bcc@domain2.com>";
$headers[] = "Reply-To: Recipient Name <receiver@domain3.com>";
$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 <mary@example.com>, Kelly <kelly@example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck@example.com' . "\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: webmaster@example.com

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.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • Hey, thank you for the fast answer! I changed it like you said, added the $headers = array(); above and also changed From to a mail adress. But Im still not receiving an email when I use the contact form :-( – carlpoppa Feb 18 '16 at 16:53
  • @carlpoppa you're welcome. 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. – Funk Forty Niner Feb 18 '16 at 16:55
  • @carlpoppa you did also see my note in the Edit about `Antworten Sie:` - `Reply-To:`, yes? – Funk Forty Niner Feb 18 '16 at 16:57
  • @carlpoppa please go over my answer again and very carefully. If you only added the `$headers = array();` part, it needs to read as `$headers = array(); $headers [] = 'From: WSC-Kontaktformular'; $headers [] = "Reply-To: {$email_address}";` as stated in the bottom of my answer under the **Edit** along with more info in there. with no dot like you had as `$headers [] .=` – Funk Forty Niner Feb 18 '16 at 17:06
  • yes, i saw it, thanks. Im receiving Mail sent, so it works, but I dont get any mail in my inbox. What else could be the problem? – carlpoppa Feb 18 '16 at 17:07
  • @carlpoppa you're welcome. could be anything really. Might even take some time for the server to process it, and/or that it's being filtered by the receiving end and trying to figure out if it's spam or not. Many a times, not using the right headers will trigger mail as spam. Have a look at the following Q&A's which may help http://stackoverflow.com/q/18229279/ - http://stackoverflow.com/q/5935087/ - http://stackoverflow.com/q/746809/ and using phpmailer or swiftmailer may also be useful instead of `mail()`. There isn't much else I can add to my answer to help you further, wish I could though. – Funk Forty Niner Feb 18 '16 at 17:11
  • @carlpoppa I believe I have answered the original question as to why the headers were failing. Mail not being sent/delivered is beyond the scope of the original question and is server-related, not code-related. This one should be marked as solved. If you have another problem, then a new question should be asked. However, questions about mail not being sent/or in spam may be closed or referred to another section of Stack. – Funk Forty Niner Feb 18 '16 at 17:15
  • @carlpoppa You are most welcome. I sincerely wish you well. Like I said, read through those links I gave you above and also check to see by going to Google and using these keywords: "mail goes to spam php" or "mail shows as sent but not delivered php". All the best, *cheers*. Oh and here's the link for phpmailer https://github.com/PHPMailer/PHPMailer and http://swiftmailer.org/ - I hope they serve you well. – Funk Forty Niner Feb 18 '16 at 17:19
  • Hey again, so the mails are ending up in my spam folder. How can I influence that? Gmails added comment: Why is the mail in spam folder? Because we recognized that alot of messages of Domain s037.bre.qsc.de are spam. What can I do about that? Thanks – carlpoppa Feb 18 '16 at 18:40
  • @carlpoppa that is too broad an area in order to provide a solution and has been an ongoing problem for a few years in regards to new and enforced spam laws. You would have to further research this, sorry. – Funk Forty Niner Feb 18 '16 at 18:45