title: Is this a safe PHP mail function?
body: <p>I've <em>finally</em> got this PHP email script working (didn't work on localhost…), but my concern is that it's not safe.</p>
  
  <p>So - is this safe for spamming and any other security  pitfalls I'm not aware of?</p>
  
  <pre><code>&lt;?php
  $email = 'notification@domain.com';
  $subject = 'Notify about stuff';
  $notify = $_REQUEST['email'];
  
  if (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $notify)) {
      echo "&lt;h4&gt;Your email address doesn't validate, please check that you typed it correct.&lt;/h4&gt;";
      echo "&lt;a href='javascript:history.back(1);'&gt;Back&lt;/a&gt;";
  }
  
  elseif(mail($email, $subject, $notify)) {
      echo "&lt;h4&gt;Thank you, you will be notified.&lt;/h4&gt;";
  } else {
      echo "&lt;h4&gt;Sorry, your email didn't get registered.&lt;/h4&gt;";
  }
  ?&gt;
  </code></pre>
  
  <p>Unrelated: is there a PHP function I can use instead of <code>javascript:history.back(1)</code> ?</p>
  
  <p>Edit: the script using <a href="http://us.php.net/manual/en/filter.examples.validation.php" rel="nofollow noreferrer">filter</a> instead of RegEx</p>
  
  <pre><code>&lt;?php
  $email = 'notification@domain.com';
  $subject = 'Notify about stuff';
  $notify = $_REQUEST['email'];
  
  if (!filter_var($notify, FILTER_VALIDATE_EMAIL)) {
      echo "&lt;h4&gt;This email address ($notify) is not considered valid, please check that you typed it correct.&lt;/h4&gt;";
      echo "&lt;a href='javascript:history.back(1);'&gt;Back&lt;/a&gt;";
  }
  
  elseif(mail($email, $subject, $notify)) {
      echo "&lt;h4&gt;Thank you, you will be notified.&lt;/h4&gt;";
  } else {
      echo "&lt;h4&gt;Sorry, your email didn't get registered.&lt;/h4&gt;";
  }
  ?&gt;
  </code></pre>