oshirowanen
oshirowanen

Reputation: 15925

Stop user from sending multiple emails

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:

[email protected]

This works fine and the email gets delivered. However, the user can also do this:

[email protected], [email protected], [email protected]

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 = "[email protected]";
    $email_to = $_POST["referred_email"];
    $email_subject = 'test subject here';
    $email_message = $_POST["referred_message"];

    // create email headers
    $headers = 'From: '. $email_from ."\r\n".
    'Reply-To: '. $email_from ."\r\n" .
    '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... ';

?>

Upvotes: 0

Views: 124

Answers (2)

Jeroen Ingelbrecht
Jeroen Ingelbrecht

Reputation: 808

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

Upvotes: 0

Ulrich Schmidt-Goertz
Ulrich Schmidt-Goertz

Reputation: 1116

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
}

Upvotes: 3

Related Questions