digitalecartoon wrote:O, it's because i've read that besides filtering your headers with regexes
You should not filter your headers with regexs.
Write yourself a wrapper function for mail() which
- If the subject or any other header contains a newline, throws an exception
- Rejects other malformed junk as you see fit
- Passes the body unchanged.
Your TEXT body is safe from any kind of injection, no processing is done whatsoever on plain text.
If you send HTML, then of course you should escape data with htmlspecialchars(), otherwise the output will be wrong. I'm not convinced this is a security risk, but it could otherwise screw up your html output.
you also should apply htmlentities on your message text to prevent scripts from being inputed in the mailform?
As I said above, I don't think there are any security problems with people putting scripts in an email, no sane email client will run them.
But it's a good idea to escape stuff anyway if it's in HTML, and not if it's not.
The main risk of PHP mail forms is header injections, which you've already got covered in your header scanning function.
If you see unexpected newlines in your headers (or subject), don't strip them, instead throw a big fat and loud exception so the developer can see there's a problem (obviously your exception handler won't show the message to the user, will it?)
Mark