Search code examples
phpformsmailer

Tell a friend form not working


<?php
  if ($_POST['friends-email']) {
      $name = "Sender's Name";
      $email = "sender@domain.com";
      $recipient = preg_replace("/[^a-z0-9 !?:;,.\/_\-=+@#$&amp;\*\(\)]/im", "", $_POST['friends-email']);
      $recipient = preg_replace("/(content-type:|bcc:|cc:|to:|from:)/im", "", $recipient);
      $mail_body = "Default Description";
      $subject = "Default Subject";
      $header = "From: " . $name . " <" . $email . ">\r\n";
      mail($recipient, $subject, $mail_body, $header);

      echo 'Thank you for recommending <br /> us!';

  }  else {
      echo '<form action="" method="post" name="send2friend" id="send2friend">
                Friends Email*:
                <input name="friends-email" id="friends-email" type="text" />
                <input type="submit" value="continue" />
            </form>';
  }
?>

Not sure why this isn't working, I get the "thank you message" but the email is not delivered when I test using my own emails.


Solution

  • Just stating the obvious, but where you have put $email = "sender@domain.com";, you are actually changing that to a variable right?

    Do you have all PHP errors turned on and displaying?

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

    Use PHP interactive mode (php -a)to deploy an email

    $to      = 'dummymailing@mailinator.com'; $subject = 'the subject'; $message = 'hello'; $headers = 'From: webmaster@example.com' . "\r\n"; mail($to, $subject, $message, $headers);
    

    Then go here to check that it's received. This is just to check that it's not a problem with your email client - i.e. it's not blocking the email.

    If you're still having problems, it could be something to do with the system's sendmail function. This post on serverfault provides a lot of detailed advice on debugging the sendmail function.