2

I want to use a php script to send emails from a html file on a website. Would this php script be secure enough against hacking and spam?

<?php 

$to = "[email protected]";
$subject = "Sent from site";

$email = $_POST['emailFrom'];
$message = $_POST['message'];

$email   = filter_var($email , FILTER_SANITIZE_EMAIL);
$message = filter_var($message , FILTER_SANITIZE_EMAIL);

$message = $email . $message;

mail($to, $subject, $message, "From: [email protected]");
?>
3
  • 1
    Looks good. As long as anything that can be posted is checked, you are on the safe side. Commented Mar 7, 2012 at 17:30
  • 1
    Probably a better fit for codereview.stackexchange.com. Commented Mar 7, 2012 at 17:30
  • 2
    As long as your script does not let someone specify their own recipient address, it will not be useful for spammers. Commented Mar 7, 2012 at 17:31

1 Answer 1

1

FILTER_SANITIZE_EMAIL removes illegal email address characters from a string; this is, therefore not the best option for the contents of an email (however useful it may be for email addresses). Whilst removing HTML special characters is useful when preventing XSS attacks, it is worth noting that there are legitimate reasons to post < and > in messages (i.e. right now). Therefore it is better to convert these characters to their html entities.

I.e.
< would become &lt;
and > would become &gt;

So in order to change html characters to their entities replace:
$message = filter_var($message , FILTER_SANITIZE_EMAIL); with
$message = htmlspecialchars($message);
Other than that it looks good; but remember, in cases where a database is involved database sanitisation should also be added.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.