如何使用HTML表单和PHP脚本发送电子邮件

如何使用HTML表单和PHP脚本发送电子邮件,php,html,Php,Html,有人请帮助我的代码一切似乎都很好,但每当我点击提交按钮,浏览器显示PHP脚本,而不是发送电子邮件到我的邮件帐户。安装了PHP,我认为这就是问题所在。请帮忙??????这是我的PHP脚本 <?php if(isset($_POST['submit'])) { $email_to = "example@domain.com"; $email_subject = "Wedding Wishes"; functi

有人请帮助我的代码一切似乎都很好,但每当我点击提交按钮,浏览器显示PHP脚本,而不是发送电子邮件到我的邮件帐户。安装了PHP,我认为这就是问题所在。请帮忙??????这是我的PHP脚本

                     <?php
    if(isset($_POST['submit'])) {


     $email_to = "example@domain.com";
    $email_subject = "Wedding Wishes";


     function died($error) {
    // your error code can go here
    echo "We are very sorry, but there were error(s) found with the form you";
    echo "These errors appear below.<br /><br />";
    echo $error."<br /><br />";
    echo "Please go back and fix these errors.<br /><br />";
    die();
    }

    // validation expected data exists
    if(!isset($_POST['txtName']) ||
    !isset($_POST['txtSurname']) ||
    !isset($_POST['txtEmail']) ||
    !isset($_POST['txtRegion']) ||
    !isset($_POST['cmbRelation']) ||
    !isset($_POST['txtMessage'])) {
    died('We are sorry, but there appears to be a problem with the form you');                  
 }

$first_name = $_POST['txtName']; // required
$last_name = $_POST['txtSurname']; // required
$email_from = $_POST['txtEmail']; // required
$region = $_POST['txtRegion']; 
$relation = $_POST['txtRelation'];// not required
$message = $_POST['txtMessage']; // required

$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
 }
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
if(strlen($message) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";

function clean_string($string) {
  $bad = array("content-type","bcc:","to:","cc:","href");
  return str_replace($bad,"",$string);
}

$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Region: ".clean_string($region)."\n";
$email_message .= "Relation: ".clean_string($relation)."\n";
$email_message .= "Message: ".clean_string($message)."\n";


// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($email_to, $email_subject, $email_message, $headers);  
    ?> 

我的Html表单如下所示

 <div class="grid_16" style="width:400px;">
  <div class="top-1">
  <form id="form" method="post" action="SendEmail_Wishes.php" style="width: 400px;" >
  <fieldset>
    <p>
      <label><strong>Name:</strong>
        <input type="text" name="txtName" style= "width:300px; height:25px;">
        <strong class="clear"></strong></label>
      <label><strong>Surname:</strong>
        <input type="text" name="txtSurname" style= "width:300px; height:25px;">       
        <strong class="clear"></strong></label>
      <label><strong>Email:</strong>
         <input name="txtEmail" type="text" style= "width:300px; height:25px;">
        <strong class="clear"></strong></label>
      <label><strong>Region:</strong>
        <input name="txtRegion" type="text" id="txtRegion" value="Country & City"  >
        <strong class="clear"></strong></label>
      <label><strong>Relation:</strong>
        <select name="cmbRelation" id="cmbRelation" accesskey="2" tabindex="2">
          <option value="Family">Family</option>
          <option value="Friend" selected>Friend</option>
        </select>
         <br>
       </label>
       <label><strong>Message:</strong>
        <textarea name="txtMessage" id="txtMessage"  style= "width:300px;"></textarea>  
<font size="1"><br />
<br />
<br />
<br />
<br />
<br />
(Maximum characters: 250)<br>
<input name="submit" type="submit" value="submit" style="width:50px;">



<br />
      <strong class="clear"></strong></label>
    </p>

    <script type="text/javascript">
function blank(a) { if(a.value == a.defaultValue) a.value = ""; }
function unblank(a) { if(a.value == "") a.value = a.defaultValue; }
</script> 

<script language="javascript" type="text/javascript">
function limitText(limitField, limitCount, limitNum) {
if (limitField.value.length > limitNum) {
    limitField.value = limitField.value.substring(0, limitNum);
} else {
    limitCount.value = limitNum - limitField.value.length;
}
}
</script>



  </fieldset>
</form>


名称:

姓氏:

电子邮件:

地区:

关系:
家庭
朋友

消息:





(最多250个字符)

函数blank(a){if(a.value==a.defaultValue)a.value=”“;} 函数unblank(a){if(a.value==“”)a.value=a.defaultValue;} 函数limitText(limitField、limitCount、limitNum){ 如果(limitField.value.length>limitNum){ limitField.value=limitField.value.substring(0,limitNum); }否则{ limitCount.value=limitNum-limitField.value.length; } }
您的服务器似乎配置不正确。 在尝试用php发送电子邮件之前,我建议您使用hello world示例:

<?php
   echo phpinfo();
?>


在使用任何html页面之前,请尝试直接从浏览器调用您的页面。

您的服务器似乎配置不正确。 在尝试用php发送电子邮件之前,我建议您使用hello world示例:

<?php
   echo phpinfo();
?>


在使用任何html页面之前,请尝试直接从浏览器调用您的页面。

您不会关闭
if(isset($\u POST['submit'])
语句


您缺少结尾处的
}

您不关闭
if(isset($\u POST['submit'])
语句


最后缺少
}

下载SMTP服务器,在单击“提交”之前运行服务器。

下载SMTP服务器,在单击提交之前运行服务器。

如果安装了PHP,听起来好像您没有正确配置它-您可以在httpd.conf中搜索
PHP
,并将这些行粘贴到问题中吗?如果显示的是PHP,则不会执行。您如何访问这个PHP文件?它是在服务器上还是在本地机器上?您使用的是什么操作系统?Windows、Linux或Mac?我正在使用Windows,并在其上安装了wamp服务器。运行时会发生什么?如果安装了PHP,听起来好像您没有正确配置它-您可以在httpd.conf中搜索
PHP
,并将这些行粘贴到问题中吗?如果显示的是PHP,则不会执行。您如何访问这个PHP文件?它是在服务器上还是在本地机器上?您使用的是什么操作系统?Windows、Linux或Mac?我正在使用Windows,并在其上安装了wamp服务器。当您运行时会发生什么?哦,thanx,但您认为这是导致此问题的主要原因吗?如果您忘记它们,您的代码将无法工作。我不知道这是否导致了整个问题。代码看起来不错。如果您正在编程@localhost,则mail()函数会出现问题。哦,thanx,但是您认为这是导致此问题的主要原因吗?如果您忘记了这些问题,您的代码将无法工作。我不知道这是否导致了整个问题。代码看起来不错。如果您正在编程@localhost,则mail()函数会出现问题。