提交HTML5表单到电子邮件(通过PHP?)

时间:2014-02-03 18:29:13

标签: php html5 forms submit

我正在尝试创建一个HTML5表单,在不打开用户的电子邮件客户端的情况下提交基本的供应商信息,并在表单提交时使用“已提交”(或其他类似的)确认消息。根据我的研究,我发现我需要使用PHP,但我对如何实现PHP脚本毫无头绪。

这是我的表格:

<form id="vendorInfo" action="process_form_vendor.php" method="post">
    <label for="vendorName">Vendor Name:</label>
    <br />
    <input id="vendorName" name="vendorName" type="text" maxlength="30" required>
    <br />
    <label for="contactName">Contact Name:</label>
    <br />
    <input id="contactName" name="contactName" type="text" maxlength="35" required>
    <br />
    <label for="vendorType">Organization Type:</label>
    <br />
    <select id="vendorType" name="vendorType">
        <option value="carrier">
                Insurance Carrier
        </option>
        <option value="tech_crm">
                Technology/CRM Management
        </option>
        <option value="leadProvider">
                Lead Provider   
        </option>
        <option value="info_comm">
                Information/Communication
        </option>
        <option value="other">
                Other (please describe below)
        </option>
    </select>
    <br />
    <label for="other1">Other Organization Type:</label>
    <br />
    <input id="other1" name="other1" type="text" maxlength="25">
    <br />
    <label for="email">Email:</label>
    <br />
    <input id="email" name="email" type="email" maxlength="30" required>
    <br />
    <label for="phone">Phone:</label>   
    <br />  
    <input id="phone" name="phone" type="tel" maxlength="12" required placeholder="xxx-xxx-xxxx">
    <br />
    <label for="questions">Any questions or comments? Leave them here:</label>
    <br />
    <textarea id="questions" name="questions" rows="10" maxlength="300"></textarea>
    <br />
    <br />
<fieldset id="selectionBox">
    <legend id="packageSelect">
    The following sponsorship packages are available for the Sales Summit; contact <a href="example@domain.com">Amanda</a> for pricing and                              details: 
    </legend>
    <input type="radio" name="packageSelect" value="Bronze Package" checked>&nbsp;Bronze
    <br />
    <br />
    <input type="radio" name="packageSelect" value="Silver Package">&nbsp;Silver
    <br />
    <br />
    <input type="radio" name="packageSelect" value="Gold Lunch Package">&nbsp;Gold&nbsp;(breakfast; exclusive sponsorship)
        <br />
    <br />
    <input type="radio" name="packageSelect" value="Gold Breakfast Package">&nbsp;Gold&nbsp;(lunch; exclusive sponsorship)
    <br />
    <br />
        <input type="radio" name="packageSelect" value="Gold Trade Show Package">&nbsp;Gold&nbsp;(trade&nbsp;show; exclusive sponsorship)
</fieldset>
<br />
<button type="submit">Submit</button>&nbsp;<button type="reset">Reset</button><br />

这是我写的PHP脚本:

<?php

if(!isset($_POST['submit']))
{
    echo "error; you need to submit the form!";
}

$vendorName = $_POST['vendorName'];
$contactName = $_POST['contactName'];
$vendorType = $_POST['vendorType'];
$other1 = $_POST['other1'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$questions = $_POST['questions'];
$packageSelect = $_POST['packageSelect'];

if (empty($vendorName)||(empty($contactName)||(empty($vendorType)||(empty($email)||(empty($phone)||(empty($packageSelect)) {
    echo "Vendor Name, Contact Name, Vendor Type, Email, Phone, and Package Selection are mandatory!";
    exit;   
}

$email_from = 'example@domain.com';
$email_subject = '2014 SMS Sales Summit - New Vendor Reservation Request';
$email_body = "You have received a new vendor reservation request for the 2014 SMS Sales Summit from $contactName at $vendorName.\n".
              "Vendor Type: $vendorType\n".
              "Other Vendor Type: $other1\n".
              "Email Address: $email\n".
              "Phone Number: $phone\n".
              "Additional Questions: $questions\n".
              "Sponsorship Level: $packageSelect\n".

$to = 'example@domain.com';
$headers = "$email_from \r\n";
$headers .= "Reply-To: $email \r\n";

mail($to,$email_subject,$email_body,$headers);
header('Location: thank-you.html');



?>

我很确定我已经正确设置了所有内容以将提交的信息提取到“输入”字段中,但是我不确定我是否已经正确地为单选按钮和下拉列表实现了这一点选择。任何有关此实现的帮助都将非常受欢迎。谢谢!

2 个答案:

答案 0 :(得分:0)

您需要将表单提交到服务器上的邮件程序脚本(mail.php)。一旦服务器具有表单信息,您就可以开始在那里构建消息体并使用php mail()函数发送消息。

要了解通过php发送邮件,我建议在这里阅读php文档:http://www.php.net/manual/en/function.mail.php

您还可以使用html格式化邮件正文,以便在电子邮件客户端中很好地呈现邮件。

您可能需要研究如何通过您使用的任何服务器启用发送邮件。我认为默认情况下它被禁用。

答案 1 :(得分:0)

  1. 在表单中添加一个方法:

    <form action="" method="post" id="vendorInfo"> //its an example

  2. 将name属性赋予您的所有输入文件:

    <input type="text" name="whatever"> //its an example

  3. 然后通过php中的post方法捕获这些

    <?php
    if(isset($_POST['your_submit_name']))
    {
    $input_field=$_POST['your_input_field_name'];
    $to = "somebody@example.com";
    $subject = "your subject";
    $message= "Hello mail!". "\r\n";
    $message.= $input_field."\r\n";
    $headers = "From: webmaster@example.com" . "\r\n" ;
    mail($to,$subject,$message,$headers);
    ?>