Is there are reliable way of stopping a user from sending multiple emails in one go?
I have a basic html form which allows the user to type in an email address:
email1@address1.com
This works fine and the email gets delivered. However, the user can also do this:
email1@address1.com, email2@address2.com, email3@address3.com
My code sends all 3 emails when the submit button is pressed. How do I stop that from happening. I want to restrict the user and only allow him/her to send 1 email per submit.
The code I currently have is this:
<?php
$email_from = "oshirowanen@localhost.com";
$email_to = $_POST["referred_email"];
$email_subject = 'test subject here';
$email_message = $_POST["referred_message"];
// create email headers
$headers = 'From: '. $email_from ."
".
'Reply-To: '. $email_from ."
" .
'X-Mailer: PHP/' . phpversion();
$result = mail($email_to, $email_subject, $email_message, $headers);
if ($result) echo 'Mail accepted for delivery ';
if (!$result) echo 'Test unsuccessful... ';
?>
Check that $email_to
is a valid e-mail address before sending the mail. The easiest way to do that is probably to use filter_var():
if (!filter_var($email_to, FILTER_VALIDATE_EMAIL)) {
// handle error
}
Use a regular expression to evaluate the content of the inputfield: http://www.regular-expressions.info/email.html
Or just use the new email type for an input field: http://www.w3schools.com/html/html5_form_input_types.asp