Jump to content

PHP Sendmail - Email Form Validation


Ross2007

Recommended Posts

Hi All,

I am using the following code to submit a form from flash and would like to incorporate email validation.  I have looked at several methods on the web and am finding it confusing since I am relatively new to PHP, any help will greatly be appreciated.

Kind Regard

Ross

[code]
<?php
if(!empty($HTTP_POST_VARS['sender_mail']) || !empty($HTTP_POST_VARS['sender_message']) || !empty($HTTP_POST_VARS['sender_subject']) || !empty($HTTP_POST_VARS['sender_name']))
{
$to = "[email protected]";
$subject = stripslashes($HTTP_POST_VARS['sender_subject']);
$body = stripslashes($HTTP_POST_VARS['sender_message']);
$body .= "\n\n---------------------------\n";
$body .= "Mail sent by: " . $HTTP_POST_VARS['sender_name'] . " <" . $HTTP_POST_VARS['sender_mail']  . ">\n";
$header = "From: " . $HTTP_POST_VARS['sender_name'] . " <" . $HTTP_POST_VARS['sender_mail'] . ">\n";
$header .= "Reply-To: " . $HTTP_POST_VARS['sender_name'] . " <" . $HTTP_POST_VARS['sender_mail'] . ">\n";
$header .= "X-Mailer: PHP/" . phpversion() . "\n";
$header .= "X-Priority: 1";
if(@mail($to, $subject, $body, $header))
{
echo "output=sent";
} else {
echo "output=error";
}
} else {
echo "output=error";
}
?>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/35209-php-sendmail-email-form-validation/
Share on other sites

To start with (a little off topic) $HTTP_POST_VARS has long been depricated in favour of $_POST.

Now, how do you want to validate the email? If you just want to validate that the form was filled in and that it contains a valid email format your probably best doing it within flash itself, as well as in php using [url=http://php.net/isset]isset[/url] and [url=http://php.net/preg_match]preg_match[/url].

If you want to validate the email actually exists, you'll need to send it an email and ask the client to reply.
[code=php:0]
if(!empty($HTTP_POST_VARS['sender_mail']) || !eregi("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$]", $HTTP_POST_VARS['sender_mail']) || !empty($HTTP_POST_VARS['sender_message']) || !empty($HTTP_POST_VARS['sender_subject']) || !empty($HTTP_POST_VARS['sender_name']))
[/code]


Should be sufficient. then just change all instances of $HTTP_POST_VARS to $_POST.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.