Submitting HTML5 forms to email (via php?)

2019-08-31 03:51发布

问题:

I'm attempting to create an HTML5 form that submits basic vendor information without opening the user's email client, and with a "Submitted" (or something of the like) confirmation message as the form submits. From my research, I've found that I'd need to use PHP for this, but I'm clueless as to how to implement the PHP scripts.

Here's my form:

<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 />

And here is the PHP script I've written:

<?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');



?>

I'm pretty sure that I've got everything set up correctly to pull the information submitted into the 'input' fields, but I'm not sure if I've implemented this correctly for the radio buttons and the drop down selector. Any help with this implementation would be most appreciated. Thanks!

回答1:

  1. add a method to your form as:

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

  2. Give name attribute to your all input filed as:

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

then catch these by post method in php

<?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);
?>


回答2:

You would need to submit the form to a mailer script on your server (mail.php). Once the server has the form information you can start building the message body there and use the php mail() function to send the message.

To learn about sending mail via php i suggest reading the php docs here: http://www.php.net/manual/en/function.mail.php

You can also format the body of your message with html to have it render nicely in an email client.

You may have to research how to enable send mail via whatever server you'er using. I think by default it is disabled.