将HTML / PHP中的表单联系到我的电子邮箱

时间:2015-01-03 11:30:44

标签: php html

我正在尝试使用PHP脚本为我的网站创建一个简单的HTML联系表单。互联网上有很多例子,但它们通常太简单或太复杂。

这是我在HTML中的表单,我需要PHP脚本。

        <form action="send.php" method="POST" id="form">
              <label for="latitude">Lat. &deg;N:</label>
              <input id="latitude" name="latitude" type="text" />
              <label for="longitude">Long. &deg;E:</label>
              <input id="longitude" name="longitude" type="text" />
              <label for="desc">Description:</label>
              <textarea style="resize:none" name="desc" cols="45" rows="5"></textarea>
              <label for="mail">E-mail:</label>
              <input type="text" name="mail" /><br>
              <input type="submit" name="submit" value="Send">
        </form>
  1. 它应包含charset=utf-8以在消息中显示我所有语言的变音符号。
  2. 如果所有字段都不为空,则应进行验证。不需要电子邮件验证。
  3. 应以非常简单的方式通知Message send!Error, try again.

1 个答案:

答案 0 :(得分:1)

@EWit是对的......你不能像这样使用Stack Overflow。但是,下面的PHP应该可以工作。尝试将其拉开,以便您可以从中学习。干杯

<?PHP

define('kOptional', true);
define('kMandatory', false);

error_reporting(E_ERROR | E_WARNING | E_PARSE);
ini_set('track_errors', true);

function DoStripSlashes($fieldValue)  { 

 if ( function_exists( 'get_magic_quotes_gpc' ) && get_magic_quotes_gpc() ) { 
  if (is_array($fieldValue) ) { 
   return array_map('DoStripSlashes', $fieldValue); 
  } else { 
   return trim(stripslashes($fieldValue)); 
  } 
 } else { 
  return $fieldValue; 
 } 
}

function FilterCChars($theString) {
 return preg_replace('/[\x00-\x1F]/', '', $theString);
}

function CheckEmail($email, $optional) {
 if ( (strlen($email) == 0) && ($optional === kOptional) ) {
  return true;
  } elseif ( preg_match("/^([\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+\.)*[\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+@((((([a-z0-9]{1}[a-z0-9\-]{0,62}[a-z0-9]{1})|[a-z])\.)+[a-z]{2,6})|(\d{1,3}\.){3}\d{1,3}(\:\d{1,5})?)$/i", $email) == 1 ) {
  return true;
 } else {
  return false;
 }
}

if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
 $clientIP = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
 $clientIP = $_SERVER['REMOTE_ADDR'];
}

$FTGlatitude = DoStripSlashes( $_POST['latitude'] );
$FTGlongitude = DoStripSlashes( $_POST['longitude'] );
$FTGdesc = DoStripSlashes( $_POST['desc'] );
$FTGmail = DoStripSlashes( $_POST['mail'] );
$FTGsubmit = DoStripSlashes( $_POST['submit'] );

$validationFailed = false;

if (!CheckEmail($FTGmail, kMandatory)) {
 $FTGErrorMessage['mail'] = 'Please enter a valid email address';
 $validationFailed = true;
}


if ($validationFailed === true) {

 $errorPage = '<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8" /><title>Error</title></head><body>Errors found: <!--VALIDATIONERROR--></body></html>';

 $errorPage = str_replace('<!--FIELDVALUE:latitude-->', $FTGlatitude, $errorPage);
 $errorPage = str_replace('<!--FIELDVALUE:longitude-->', $FTGlongitude, $errorPage);
 $errorPage = str_replace('<!--FIELDVALUE:desc-->', $FTGdesc, $errorPage);
 $errorPage = str_replace('<!--FIELDVALUE:mail-->', $FTGmail, $errorPage);
 $errorPage = str_replace('<!--FIELDVALUE:submit-->', $FTGsubmit, $errorPage);
 $errorPage = str_replace('<!--ERRORMSG:mail-->', $FTGErrorMessage['mail'], $errorPage);

 $errorList = @implode("<br />\n", $FTGErrorMessage);
 $errorPage = str_replace('<!--VALIDATIONERROR-->', $errorList, $errorPage);

 echo $errorPage;

}

if ( $validationFailed === false ) {

 $emailSubject = FilterCChars("Website Contact");

 $emailBody = chunk_split( base64_encode( "<html>\n"
  . "<head>\n"
  . "<title></title>\n"
  . "</head>\n"
  . "<body>\n"
  . "Latitude : $FTGlatitude<br />\n"
  . "Longitude : $FTGlongitude<br />\n"
  . "Desc : " . nl2br( $FTGdesc ) . "<br />\n"
  . "Mail : $FTGmail<br />\n"
  . "Submit : $FTGsubmit<br />\n"
  . "</body>\n"
  . "</html>\n"
  . "" ) )
  . "\n";
  $emailTo = 'Contact <your@email.com>';

  $emailFrom = FilterCChars("$FTGmail");

  $emailHeader = "From: $emailFrom\n"
   . "MIME-Version: 1.0\n"
   . "Content-Type: text/html; charset=\"UTF-8\"\n"
   . "Content-Transfer-Encoding: base64\n"
   . "\n";

  mail($emailTo, $emailSubject, $emailBody, $emailHeader);

$successPage = '<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8" /><title>Success</title></head><body>Form submitted successfully. It will be reviewed soon.</body></html>';

$successPage = str_replace('<!--FIELDVALUE:latitude-->', $FTGlatitude, $successPage);
$successPage = str_replace('<!--FIELDVALUE:longitude-->', $FTGlongitude, $successPage);
$successPage = str_replace('<!--FIELDVALUE:desc-->', $FTGdesc, $successPage);
$successPage = str_replace('<!--FIELDVALUE:mail-->', $FTGmail, $successPage);
$successPage = str_replace('<!--FIELDVALUE:submit-->', $FTGsubmit, $successPage);

echo $successPage;

}

?>