开发者

Need some help with a HTML form PHP send email combo platter

PHP has never been my strong suit so I need some assistance getting the contents of a form to send via email properly in PHP.

Here is the html form:

<form action="estimate.php" method="post" >
                <fieldset>
                <input type="text" class="required" name="name" value="FULL NAME*" onfocus="if (this.value=='FULL NAME*') this.value='';开发者_开发问答"/>
                <input type="text" class="required" name="phone" value="PHONE NUMBER*" onfocus="if (this.value=='PHONE NUMBER*') this.value='';"/>
                <input type="text" class="required" name="email" value="EMAIL*" onfocus="if (this.value=='EMAIL*') this.value='';"/>
                <input type="text" class="required" name="date" value="MOVE DATE*" onfocus="if (this.value=='MOVE DATE*') this.value='';"/>
                <input type="text" class="required" name="origin" value="ORIGINATING ADDRESS*" onfocus="if (this.value=='ORIGINATING ADDRESS*') this.value='';"/>
                <input type="text" name="destination" value="DESTINATION ADDRESS" onfocus="if (this.value=='DESTINATION ADDRESS') this.value='';"/>
                <select name="move-type">
                    <option value="" selected="selected">TYPE OF MOVE</option>
                    <option value="Private">Private</option>
                    <option value="Commercial">Commercial</option>
                </select>
                <input id="quoteSubmit" 
                    type="image" src="_images/btn_submit.png" alt="" 
                    onmouseover="javascript:this.src='_images/btn_submit-over.png'" 
                    onmouseout="javascript:this.src='_images/btn_submit.png'"/>
                </fieldset>
 </form>

Here is the PHP I have thus far:

<?php
$emailFromName = $_POST['name'];
$emailFromPhone = $_POST['phone'];
$emailFrom = $_POST['email'];
$emailDate = $_POST['date'];
$emailOrigin = $_POST['origin'];
$emailDestination = $_POST['destination'];
$emailType = $_POST['move-type'];

if (empty($emailFromName)) {
    echo 'Please enter your name.';
} elseif (!preg_match('/^([A-Z0-9\.\-_]+)@([A-Z0-9\.\-_]+)?([\.]{1})([A-Z]{2,6})$/i', $emailFrom) || empty($emailFrom)) {
    echo 'The email address entered is invalid.';
} elseif (empty($emailDate)) {
    echo 'You must enter a Move date.';
} elseif (empty($emailOrigin)) {
    echo 'You must enter a message.';
} elseif (empty($emailDestination)) {
    echo 'You must enter a message.';    
} else {

    $emailTo = "info@movingsimple.com";

    if (!empty($emailFrom)) {
        $emailHeaders = 'From: "' . $emailFromName . '" <' . $emailFrom . '>';
    } else {
        $emailHeaders = 'From: "The Boss" <noreply@movingsimple.com>';
    }

    /* Send Email */
    if (mail($emailTo, $emailSubject, $emailDate,   $emailHeaders)) {
        echo 'Thank you! Your message has been sent.';
    } else {
        echo 'There was an internal error while sending your email.<br>';
        echo 'Please try again later.';    
    }
}
?>

I am having trouble with the body of the message. I need to display all the values from the form:

From: Email: Phone: Move date: Origin: Destination: Move type:

but I am not exactly sure how to compile all those into the body and would appreciate some help.

thanks.


Update this part of the code

if (!empty($emailFrom)) {
    $emailHeaders = 'From: "' . $emailFromName . '" <' . $emailFrom . '>';
} else {
    $emailHeaders = 'From: "The Boss" <noreply@movingsimple.com>';
}

/* Send Email */
if (mail($emailTo, $emailSubject, $emailDate,   $emailHeaders)) {
    echo 'Thank you! Your message has been sent.';

to generate a string containing the body of the email:

if (!empty($emailFrom)) {
    $emailHeaders = 'From: "' . $emailFromName . '" <' . $emailFrom . '>';
} else {
    $emailHeaders = 'From: "The Boss" <noreply@movingsimple.com>';
}

// Create a string representing the body. "\n" creates a new line.
// The ".=" operator appends the string onto the existing string.
$body = "From: ".$emailFromName."\n";
$body .= "Email: ".$emailFrom."\n";
$body .= "Phone: ".$emailFromPhone."\n";
$body .= "Move Date: ".$emailDate."\n";
$body .= "Origin: ".$emailOrigin."\n";
$body .= "Destination: ".$emailDestination."\n";
$body .= "Move Type: ".$emailType."\n";

/* Send Email */
if (mail($emailTo, $emailSubject, $body,   $emailHeaders)) {
    echo 'Thank you! Your message has been sent.';

// ... rest of the code ...

The third parameter of the mail() function (http://us.php.net/manual/en/function.mail.php) is the message, so we create the message as a string ($body) and then pass it as a parameter to the mail() function.

Small Update: The PHP mail() help page says to use "\n" as a line ending rather than "\r\n"


The third attribute to the mail function is for body of the email not date like you are specifying $emailDate, you should specify the email body there.


Right now you're sending the $emailDate as the body of the message.. You just need to build a variable with the data you want. i.e.

$emailBody = "Move date: " . $emailDate;

I think your header variable needs to have a \r\n at the end, too.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜