繁体   English   中英

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

[英]How to send Email with HTML form and PHP Script

有人请帮助我编写代码,一切似乎都很好,但是,每当我单击“提交”按钮时,浏览器就会显示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>

您不会关闭if(isset($_POST['submit']))语句。

您最后缺少}

您的服务器似乎配置不正确。 我建议您先尝试在php中发送电子邮件以使工作成为一个Hello World示例:

<?php
   echo phpinfo();
?>

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM