[英]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. PHP从来不是我的强项,因此我需要一些帮助来获取表单的内容,以便在PHP中通过电子邮件正确发送。
Here is the html form: 这是html形式:
<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:
<?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. 谢谢。
mail
功能的第三个属性是mail
的正文,而不是日期,就像您指定$emailDate
,您应该在此处指定电子邮件正文。
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. 现在,您正在发送$ emailDate作为邮件的正文。您只需要使用所需的数据构建一个变量。 ie
即
$emailBody = "Move date: " . $emailDate;
I think your header variable needs to have a \\r\\n at the end, too. 我认为您的标头变量也需要在结尾加上\\ r \\ n。
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. mail()函数的第三个参数( http://us.php.net/manual/en/function.mail.php )是消息,因此我们将消息创建为字符串($ body),然后将其传递作为mail()函数的参数。
Small Update: The PHP mail() help page says to use "\\n" as a line ending rather than "\\r\\n" 小更新: PHP mail()帮助页面说使用“ \\ n”作为行尾而不是“ \\ r \\ n”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.