单击提交按钮后如何向用户输入的电子邮件地址发送电子邮件?在PHP中

时间:2018-05-14 09:07:25

标签: php html

我是编码时的新手。这是表单页面代码



<?php
// Start the session
session_start();
?>
<!DOCTYPE HTML>  
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>  

<?php
$fnameErr = $emailErr = $addressErr = $countryErr = $stateErr = $suburbErr = "";
$fname = $email = $address = $country = $state = $suburb = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if (empty($_POST["fname"])) {
    $fnameErr = "Name is required";
  } else {
    $fname = test_input($_POST["fname"]);
  }
  
  if (empty($_POST["email"])) {
    $emailErr = "Email is required";
  } else {
    $email = test_input($_POST["email"]);
  }
    
  if (empty($_POST["address"])) {
    $addressErr = "Address is required";
  } else {
    $address = test_input($_POST["address"]);
  }

  if (empty($_POST["country"])) {
    $countryErr = "City is required";
  } else {
    $country = test_input($_POST["country"]);
  }

  if (empty($_POST["state"])) {
    $stateErr = "State is required";
  } else {
    $state = test_input($_POST["state"]);
  }
  
  if (empty($_POST["suburb"])) {
    $suburbErr = "State is required";
  } else {
    $suburb = test_input($_POST["suburb"]);
  }
}

function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}
?>

<h2 align="cemter">Check out</h2>
<p><span class="error">* required field</span></p>
  
  Full Name: <input type="name" name="fname">
  <span class="error">* <?php echo $fnameErr;?></span>
  <br><br>
  E-mail: <input type="email" name="email">
  <span class="error">* <?php echo $emailErr;?></span>
  <br><br>
  Address: <input type="text" name="address">
  <span class="error">*<?php echo $addressErr;?></span>
  <br><br>
  Country: <input type="text" name="country">
  <span class="error">*<?php echo $countryErr;?></span>
  <br><br>
  State: <input type="text" name="state">
  <span class="error">*<?php echo $stateErr;?></span>
  <br><br>
  Suburb: <input type="text" name="suburb">
  <span class="error">*<?php echo $suburbErr;?></span>
  <br><br>
  <form method="post" action="../mailer/index.php">
  <input type="submit" name="submit" value="Purchase"> 
  </form>
</body>
</html>
&#13;
&#13;
&#13;

但是在我填完所有信息并点击提交后,该页面表示无法连接到SMTP服务器主机。奇怪的是,我可以单独运行phpmailer index.php发送电子邮件。表单页面php文件和邮件程序php文件位于不同的文件夹但是相同的父文件夹,这是问题吗?

1 个答案:

答案 0 :(得分:0)

如果一个页面正常工作而另一个页面不起作用,并且这些页面位于不同的文件夹中,那么是的,这可能是问题所在。但是,解决方案可能不仅仅是移动其中一个文件(这可能会给您带来其他路径相关的问题),而是确保它们都可以到达必要的配置(在这种情况下是SMTP服务器主机)。