HTML/PHP 中的联系表发送到我的电子邮件

Contact form in HTML/PHP to my e-mail

我正在尝试使用 PHP 脚本在 HTML 中为我的网站创建一个简单的联系表单。 Internet 上到处都是示例,但它们通常太简单或太复杂。

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

        <form action="send.php" method="POST" id="form">
              <label for="latitude">Lat. °N:</label>
              <input id="latitude" name="latitude" type="text" />
              <label for="longitude">Long. °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.

@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;

}

?>