如何使用PHP联系人表单格式化电子邮件的多选内容


How to format multiple selects for email using PHP contact form

我有一个关于我在联系人表单中使用的PHP代码的问题。

我目前有以下HTML代码用于我的表单:

      <div id="contact-form">
  
    <form method="post" action="sendmail.php">
    <div>
    Name * <input name="name" type="text" placeholder="John Doe" />
    </div>
    
    <div>
    Email * <input name="email" type="email" placeholder="johndoe@example.com" />
    </div>
    
    <div>
    Tel * <input name="tel" type="tel" />
    </div>
    
    <div>
    Organisation <input name="org" type="text" />
    </div>
    
    <div class="select">
    Requested Date<br>
      <select name="day">
                <option value="01">01</option>
                <option value="02">02</option>
                <option value="03">03</option>
                <option value="04">04</option>
                <option value="05">05</option>
                <option value="06">06</option>
                <option value="07">07</option>
                <option value="08">08</option>
                <option value="09">09</option>
                <option value="10">10</option>
                <option value="11">11</option>
                <option value="12">12</option>
                <option value="13">13</option>
                <option value="14">14</option>
                <option value="15">15</option>
                <option value="16">16</option>
                <option value="17">17</option>
                <option value="18">18</option>
                <option value="19">19</option>
                <option value="20">20</option>
                <option value="21">21</option>
                <option value="22">22</option>
                <option value="23">23</option>
                <option value="24">24</option>
                <option value="25">25</option>
                <option value="26">26</option>
                <option value="27">27</option>
                <option value="28">28</option>
                <option value="29">29</option>
                <option value="30">30</option>
                <option value="31">31</option>
         </select>
         <span>/</span>
         <select name="month">
                <option value="JAN">JAN</option>
                <option value="FEB">FEB</option>
                <option value="MAR">MAR</option>
                <option value="APR">APR</option>
                <option value="MAY">MAY</option>
                <option value="JUN">JUN</option>
                <option value="JUL">JUL</option>
                <option value="AUG">AUG</option>
                <option value="SEPT">SEPT</option>
                <option value="OCT">OCT</option>
                <option value="NOV">NOV</option>
                <option value="DEC">DEC</option>
         </select>
         <span>/</span>
         <select name="year">
                <option value="2015">2015</option>
                <option value="2016">2016</option>
                <option value="2017">2017</option>
                <option value="2018">2018</option>
                <option value="2019">2019</option>
                <option value="2020">2020</option>
            </select>
    </div>
    
    <div>
    Duration of Day/s<br>
         <select id="time" name="time">
                <option value="1-2 Hours">1-2 Hours</option>
                <option value="2-4 Hours">2-4 Hours</option>
                <option value="5-7 Hours">5-7 Hours</option>
                <option value="7+ Hours" >7+ Hours</option>
         </select>
    </div>
    
    <div>
    Message *
    <textarea name="message" rows="15" cols="40"></textarea>
    </div>
    
    <input type="submit" value="SEND" class="button submit-button" />
    </form>

下面是PHP代码(我从我的托管公司得到的):

<?php
/* 
=============================================
Sendmail.php - send an email from a web form. Make sure this file is called sendmail.php
when you upload it, otherwise the example form won't find the script and will error.
    
NOTE: This script is heavily commented. Text after double slashes // is ignored by PHP
=============================================
*/
// You only need to modify the following three lines of code to customise your form to mail script.
$email_to = "info@urbanedge-promotions.com";            // Specify the email address you want to send the mail to.
$email_subject = "Urban Edge Enquiry";  // Set the subject of your email.
// Specify a page on your website to display a thankyou message when the mail is sent
$thankyou_url = "http://www.info@urbanedge-promotions.com/thankyou.html";
                                            
// Get the details the user entered into the form
$name = $_POST["name"];
$email_from = $_POST["email"];
$tel = $_POST["tel"];
$org = $_POST["org"];
$time = $_POST["time"];
$message = $_POST["message"];
//Validate name is not empty
if(empty($name)) {
    die("<p>Please go back and enter your full name.</p>");
}
//Validate email is not empty
if(empty($email_from)) {
    die("<p>Please go back and enter your email address.</p>");
}
// Validate the email address entered by the user
if(!filter_var($email_from, FILTER_VALIDATE_EMAIL)) {
    // Invalid email address
    die("<p>The email address entered is invalid. Please go back and correct it.</p>");
}
//Validate tel is not empty
if(empty($tel)) {
    die("<p>Please go back and enter your telephone number.</p>");
}
//validate message is not empty
if(empty($message)){
    die("<p>Please go back and complete the message field.</p>");
}
//validate message is greater than 20 charcters
elseif(strlen($message) < 20){
    die("<p>Your message must be greater than 20 characters. Please go back and provide as much detail as possible regarding your enquiry.</p>");
}
// The code below creates the email headers, so the email appears to be from the email address filled out in the previous form.
// NOTE: The 'r'n is the code to use a new line.
$headers  = "From: " . $email_from . "'r'n";
$headers .= "Reply-To: " . $email_from . "'r'n";    // (You can change the reply email address here if you want to.)
// Now we can construct the email body which will contain the name and message entered by the user
$message = "Name: ". $name . "'r'nEmail: ". $email_from . "'r'nTel: ". $tel . "'r'nOrganisation: ". $org . "'r'nDate: ". $day . "'r'nDuration of Day: ". $time . "'r'nMessage: " . $message;
// This is the important ini_set command which sets the sendmail_from address, without this the email won't send.
ini_set("sendmail_from", $email_from);
// Now we can send the mail we've constructed using the mail() function.
// NOTE: You must use the "-f" parameter on Fasthosts' system, without this the email won't send.
$sent = mail($email_to, $email_subject, $message, $headers, "-f" . $email_from);
// If the mail() function above successfully sent the mail, $sent will be true.
if($sent) {
    header("Location: " . $thankyou_url);   // Redirect customer to thankyou page
} else {
    // The mail didn't send, display an error.
    echo "There has been an error sending your message. Please try later.";
}
    

>

我希望所选日期显示在我的电子邮件中,如"2014年11月29日",但目前为空。。。即:

名称:Someones名称

电子邮件:example@example.com

电话:07183737373

组织:

日期:

一天的持续时间:1-2小时

留言:大家好,我正在找人在我需要的时候帮我!感谢您的智慧和知识。

感谢

您必须访问$_POST数组才能获得日期、月份和年份下拉列表的值,并且必须执行以下操作才能将所选选项转换为所需日期。

$date = $_POST['day']."/".date('m', strtotime($_POST['month']))."/".$_POST['year'];

你不知道如何获取'select'元素的值吗?这不管用吗?

$_POST["day"]
$_POST["month"]
$_POST["year"]
$_POST["time"]