Jump to content

Recommended Posts

Hi everybody,

 

I could use a little help with this secure and simple email script i'm trying to write. It's just directing to mailscript.php when you hit submit in the form and it doesn't send any email.

 

<?php

// check for injection characters

function injection_chars($s) {
return (eregi("\r", $s) || eregi("\n", $s) || eregi("%0a", $s) || eregi("%0d", $s)) ? TRUE : FALSE;
}

// make output safe for the browser

function safe($input) {
return htmlspecialchars(stripslashes($input));
}

// subject and email variables

    $to = 'youremail@mail.com';
    $subject = 'Mail sent from contact form';

// gathering data variables

    $name = injection_chars(safe($_POST['name']));
    $email = injection_chars(safe($_POST['email']));
    $message = injection_chars(safe($_POST['message']));
    $body = <<<END
Name: $name
Email: $email
Message: $message
END;

$headers = "From: $email\r\n";
$headers .= "Content=type: text/html\r\n";

if (isset($_POST['submit']))
{
if (empty($_POST['name'])) {
	// if sender hasn't entered a name
        echo"<p>You must enter a name.</p>";
    	}
elseif (empty($_POST['email'])) {
	// if sender hasn't entered an email
        echo "<p>You must enter your e-mail.</p>";
   		}
elseif (empty($_POST['message'])) {
	// if sender hasn't entered a message
        echo "<p>You must enter a message.</p>";
	}
}
else
{
// sending the mail

mail($to, $subject, $body, $headers);
}
// directing user to a thank-you page

header('Location: thanks.php');
?>

 

I've looked at it for several hours now, but think I've gone blind on it by now.

 

Link to comment
https://forums.phpfreaks.com/topic/174919-help-with-mail-form/
Share on other sites

you put the mail function in the ELSE statement.. meaning if !isset($_POST['SUBMIT']) would be what the else translates to in this script.. so if they click the submit button.. the email won't get sent.. move the mail() function into the if not the else

Link to comment
https://forums.phpfreaks.com/topic/174919-help-with-mail-form/#findComment-921909
Share on other sites

Okay, this is what I came up with but it still doesn't work.  :confused:

 

<?php

// check for injection characters

function injection_chars($s) {
return (eregi("\r", $s) || eregi("\n", $s) || eregi("%0a", $s) || eregi("%0d", $s)) ? TRUE : FALSE;
}

// make output safe for the browser

function safe($input) {
return htmlspecialchars(stripslashes($input));
}

// subject and email variables

    $to = 'aandersmj@gmail.com';
    $subject = 'Mail sent from contact form';

// gathering data variables

    $name = injection_chars(safe($_POST['name']));
    $email = injection_chars(safe($_POST['email']));
    $message = injection_chars(safe($_POST['message']));
    $body = <<<END
Name: $name
Email: $email
Message: $message
END;

$headers = "From: $email\r\n";
$headers .= "Content=type: text/html\r\n";

if (isset($_POST['submit']))  {
if (empty($_POST['name'])) {
	// if sender hasn't entered a name
        echo"<p>You must enter a name.</p>";
    	}
elseif (empty($_POST['email'])) {
	// if sender hasn't entered an email
        echo "<p>You must enter your e-mail.</p>";
   		}
elseif (empty($_POST['message'])) {
	// if sender hasn't entered a message
        echo "<p>You must enter a message.</p>";
	}

// sending the mail

mail($to, $subject, $body, $headers);

// directing user to a thank-you page

header('Location: thanks.php');
}

else {
echo '<p>The email was not sent!</p>';
}
?>

 

Link to comment
https://forums.phpfreaks.com/topic/174919-help-with-mail-form/#findComment-922154
Share on other sites

This should halt sending if not validated.

<?php

// check for injection characters

function injection_chars($s) {
   return (eregi("\r", $s) || eregi("\n", $s) || eregi("%0a", $s) || eregi("%0d", $s)) ? TRUE : FALSE;
}

// make output safe for the browser

function safe($input) {
   return htmlspecialchars(stripslashes($input));
}

// subject and email variables

    $to = 'aandersmj@gmail.com';
    $subject = 'Mail sent from contact form';

// gathering data variables

    $name = injection_chars(safe($_POST['name']));
    $email = injection_chars(safe($_POST['email']));
    $message = injection_chars(safe($_POST['message']));
    $body = <<<END
Name: $name
Email: $email
Message: $message
END;

   $headers = "From: $email\r\n";
   $headers .= "Content=type: text/html\r\n";

if (isset($_POST['submit']))  {
   if (empty($_POST['name'])) {
      // if sender hasn't entered a name
        echo"<p>You must enter a name.</p>";
       }
   elseif (empty($_POST['email'])) {
      // if sender hasn't entered an email
        echo "<p>You must enter your e-mail.</p>";
         }
   elseif (empty($_POST['message'])) {
      // if sender hasn't entered a message
        echo "<p>You must enter a message.</p>";
      }
   else{
       // sending the mail

      mail($to, $subject, $body, $headers);

      // directing user to a thank-you page

      header('Location: thanks.php');
   
   }


}

else {
echo '<p>The email was not sent!</p>';
}
?>

Link to comment
https://forums.phpfreaks.com/topic/174919-help-with-mail-form/#findComment-922159
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.