如何使用PHP以HTML格式发送电子邮件?

时间:2017-10-25 22:49:38

标签: php

当我提交表单时,电子邮件中的所有字段都是空白的,我无法弄清楚原因。它开始于获取未写入的变量的php错误,然后在此处搜索后我找到了一个解决方案来添加“isset”并将变量启动到空白字符串。我在错误日志中没有任何内容,但是当表单发送到电子邮件时,所有细节都是空白的。

这是我的HTML表单代码:

  <form action="dish-business.php">

            <div class="col-md-4">
                              <div class="control-group">
                <div class="form-group floating-label-form-group controls">
                  <label for="businessname" class="sr-only control-label">Business Name</label>
                  <input name="businessname" id="businessname" type="text" placeholder="Business Name&#42" required="" data-validation-required-message="Please enter your business name" class="form-control input-lg"><span class="help-block text-danger"></span>
                </div>
              </div>
              </div>

               <div class="col-md-4">
                              <div class="control-group">
                <div class="form-group floating-label-form-group controls">
                  <label for="name" class="sr-only control-label">Name*</label>
                  <input name="name" id="name" type="text" placeholder="Full Name&#42" required="" data-validation-required-message="Please enter your full name" class="form-control input-lg"><span class="help-block text-danger"></span>
                </div>
              </div>
              </div>

              <div class="col-md-4">

                   <div class="control-group">
                <div class="form-group floating-label-form-group controls">
                  <label for="phone" class="sr-only control-label">Phone*</label>
                  <input name="phone" id="phone" type="tel" placeholder="Phone&#42" required="" data-validation-required-message="Please enter phone number" class="form-control input-lg"><span class="help-block text-danger"></span>
                </div>
              </div>

              </div>

              <div class="col-md-4">
              <div class="control-group">
                <div class="form-group floating-label-form-group controls">
                  <label for="email" class="sr-only control-label">Email*</label>
                  <input name="email" id="email" type="email" placeholder="Email&#42" required="" data-validation-required-message="Please enter email" class="form-control input-lg"><span class="help-block text-danger"></span>
                </div>
              </div>
              </div>

               <div class="col-md-4">
                              <div class="control-group">
                <div class="form-group floating-label-form-group controls">
                  <label for="business" class="sr-only control-label">Type of Business</label>
                <!--  <input id="business" type="text" placeholder="Type of Business" required="" data-validation-required-message="Please enter your type of business" class="form-control input-lg"><span class="help-block text-danger"></span> -->
                  <select name="business" class="form-control input-lg"><span class="help-block text-danger">
    <option value="">Type of Business</option>
    <option value="Airports">Airports</option>
    <option value="Automotive">Automotive</option>
    <option value="Banks" onclick="changeValue(&#39;Banks&#39;);">Banks</option>
    <option value="Bar/Restaurant" onclick="changeValue(&#39;Bar/Restaurant&#39;);">Bar/Restaurant</option>
    <option value="Beauty Services" onclick="changeValue(&#39;Beauty Services&#39;);">Beauty Services</option>
    <option value="Church" onclick="changeValue(&#39;Church&#39;);">Church</option>
    <option value="Firehouse/Oil Rig/EMT" onclick="changeValue(&#39;Firehouse/Oil Rig/EMT&#39;);">Firehouse/Oil Rig/EMT</option>
    <option value="Golf Course" onclick="changeValue(&#39;Golf Course&#39;);">Golf Course</option>
    <option value="Government" onclick="changeValue(&#39;Government&#39;);">Government</option>
    <option value="Grocery/Health Food Store" onclick="changeValue(&#39;Grocery/Health Food Store&#39;);">Grocery/Health Food Store</option>
    <option value="Hair salon" onclick="changeValue(&#39;Hair salon&#39;);">Hair salon</option>
    <option value="Health/Fitness" onclick="changeValue(&#39;Health/Fitness&#39;);">Health/Fitness</option>
    <option value="Health Care" onclick="changeValue(&#39;Health Care&#39;);">Health Care</option>
    <option value="Liquor Store" onclick="changeValue(&#39;Liquor Store&#39;);">Liquor Store</option>
    <option value="Lobbies" onclick="changeValue(&#39;Lobbies&#39;);">Lobbies</option>
    <option value="Medical / Dental" onclick="changeValue(&#39;Medical / Dental&#39;);">Medical / Dental</option>
    <option value="Nail Salon" onclick="changeValue(&#39;Nail Salon&#39;);">Nail Salon</option>
    <option value="Office" onclick="changeValue(&#39;Office&#39;);">Office</option>
    <option value="Private Club" onclick="changeValue(&#39;Private Club&#39;);">Private Club</option>
    <option value="Professional Services" onclick="changeValue(&#39;Professional Services&#39;);">Professional Services</option>
    <option value="Retail Store" onclick="changeValue(&#39;Retail Store&#39;);">Retail Store</option>
    <option value="School" onclick="changeValue(&#39;School&#39;);">School</option>
    <option value="Service Industry" onclick="changeValue(&#39;Service Industry&#39;);">Service Industry</option>
    <option value="Sports Facilities" onclick="changeValue(&#39;Sports Facilities&#39;);">Sports Facilities</option>
    <option value="Stadiums" onclick="changeValue(&#39;Stadiums&#39;);">Stadiums</option>
    <option value="Other" onclick="changeValue(&#39;Other&#39;);">Other</option>

</select>


                </div>
              </div>
              </div>

               <div class="col-md-4">
                              <div class="control-group">
                <div class="form-group floating-label-form-group controls">
                  <label for="locations" class="sr-only control-label">Locations*</label>
                  <input name="locations" id="locations" type="text" placeholder="Number of Locations&#42" required="" data-validation-required-message="Please enter the number of locations" class="form-control input-lg"><span class="help-block text-danger"></span>
                </div>
              </div>
              </div>

               <div class="col-md-4">
                              <div class="control-group">
                <div class="form-group floating-label-form-group controls">
                  <label for="units" class="sr-only control-label">Units*</label>
                  <input name="units" id="units" type="text" placeholder="Number of Units&#42" required="" data-validation-required-message="Please enter the number of units" class="form-control input-lg"><span class="help-block text-danger"></span>
                </div>
              </div>
              </div>

              <div class="col-md-4">
                              <div class="control-group">
                <div class="form-group floating-label-form-group controls">
                  <label for="dishcustomer" class="sr-only control-label">Are you a dish customer</label>
                <!--  <input id="business" type="text" placeholder="Type of Business" required="" data-validation-required-message="Please enter your type of business" class="form-control input-lg"><span class="help-block text-danger"></span> -->
                  <select name="dishcustomer" class="form-control input-lg"><span class="help-block text-danger">
    <option value="">Are you a current DISH Customer?</option>
    <option value="yes">Yes</option>
    <option value="no">No</option>
    <option value="idk">Don't Know</option>

</select>


                </div>
              </div>
              </div>

                         <div class="col-md-4">
                              <div class="control-group">
                <div class="form-group floating-label-form-group controls">
                  <label for="zip" class="sr-only control-label">Zip Code</label>
                  <input name="zip" id="zip" type="text" placeholder="Business Zip Code&#42" required="" data-validation-required-message="Please enter your business zip code" class="form-control input-lg"><span class="help-block text-danger"></span>
                </div>
              </div>
              </div>

           <div class="col-md-12">
              <div class="control-group">
                <div class="form-group floating-label-form-group controls">
                  <label for="message" class="sr-only control-label">Details</label>
                  <textarea name="message" id="message" rows="2" placeholder="Any Other Details" class="form-control input-lg"></textarea><span class="help-block text-danger"></span>
                </div>
              </div>
              <div id="success"></div>
              <button type="submit" class="btn btn-dark btn-lg text-center">Send</button>
              </div>

            </form>

这是我的PHP代码:

<?php

$EmailFrom = "mail@.com";
$EmailTo = "@gmail.com";
$Subject = "Mail";

$Name = "";
$Businessname="";
$Tel="";
$Email="";
$Business="";
$Locations="";
$Units="";
$DishCustomer="";
$Zip="";
$Message="";

if (isset($_POST['businessname'])) {
    $BusinessName = Trim(stripslashes($_POST['businessname'])); 
}
if (isset($_POST['name'])) {
    $Name = Trim(stripslashes($_POST['name'])); 
}
if (isset($_POST['phone'])) {
$Tel = Trim(stripslashes($_POST['phone'])); 
}
if (isset($_POST['email'])) {
$Email = Trim(stripslashes($_POST['email'])); 
}
if (isset($_POST['business'])) {
$Business = Trim(stripslashes($_POST['business'])); 
}
if (isset($_POST['locations'])) {
$Locations = Trim(stripslashes($_POST['locations'])); 
}
if (isset($_POST['units'])) {
$Units = Trim(stripslashes($_POST['units'])); 
}
if (isset($_POST['dishcustomer'])) {
$DishCustomer = Trim(stripslashes($_POST['dishcustomer'])); 
}
if (isset($_POST['zip'])) {
$Zip = Trim(stripslashes($_POST['zip'])); 
}
if (isset($_POST['message'])) {
$Message = Trim(stripslashes($_POST['message'])); 
}

// validation
$validationOK=true;
if (!$validationOK) {
  print "<meta http-equiv=\"refresh\" content=\"0;URL=404.html\">";
  exit;
}

// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Phone: ";
$Body .= $Tel;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Business: ";
$Body .= $Business;
$Body .= "\n";
$Body .= "Number of Locations: ";
$Body .= $Locations;
$Body .= "\n";
$Body .= "Units: ";
$Body .= $Units;
$Body .= "\n";
$Body .= "Currently a Dish Customer: ";
$Body .= $DishCustomer;
$Body .= "\n";
$Body .= "Zip Code: ";
$Body .= $Zip;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";

// send email 
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

// redirect to success page 
if ($success){
  print "<meta http-equiv=\"refresh\" content=\"0;URL=thank-you.html\">";
}
else{
  print "<meta http-equiv=\"refresh\" content=\"0;URL=404.html\">";
}
?>

任何帮助都将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:1)

表单默认为method="GET"。将表单更改为:

<form action="dish-business.php" method="POST">