我在网站上有一个联系表格,它使用php脚本将详细信息发送到我的电子邮箱。
我遇到的问题是,First Name字段中的数据根本没有发送到我的电子邮件。
我有一种感觉,这将是一个非常明显的错误,但我似乎无法找到错误!
PHP
<?php
$ToEmail = 'myemail@mycompany.com';
$EmailSubject = 'BGM Contact Form';
$mailheader = "From: ".$_POST["email"]."\r\n";
$mailheader .= "Reply-To: ".$_POST["email"]."\r\n";
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
$MESSAGE_BODY = "First Name: ".$_POST["firstName"]."";
$MESSAGE_BODY = "Last Name: ".$_POST["lastName"]."";
$MESSAGE_BODY .= "Email: ".$_POST["email"]."";
$MESSAGE_BODY .= "Comment: ".nl2br($_POST["message"])."";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");
?>
HTML
<form action="thankyou.php" onsubmit="return validateForm();" method="post">
<div class="row collapse">
<div class="large-2 columns">
<label class="inline">First Name</label>
</div>
<div class="large-10 columns">
<input type="text" id="firstName" name="firstName" placeholder="Jane" required>
</div>
</div>
<div class="row collapse">
<div class="large-2 columns">
<label class="inline">Last Name</label>
</div>
<div class="large-10 columns">
<input type="text" id="lastName" name="lastName" placeholder="Smith" required>
</div>
</div>
<div class="row collapse">
<div class="large-2 columns">
<label class="inline" > Your Email</label>
</div>
<div class="large-10 columns">
<input type="text" name="email" id="yourEmail" placeholder="jane@smithco.com" required>
</div>
</div>
<label>Your Message</label>
<textarea rows="8" name="message" required></textarea>
<br><br>
<button type="submit" class="radius button">Submit</button>
</form>
答案 0 :(得分:2)
错误在这里
$MESSAGE_BODY = "First Name: ".$_POST["firstName"]."";
$MESSAGE_BODY = "Last Name: ".$_POST["lastName"]."";
将其更改为
$MESSAGE_BODY = "First Name: ".$_POST["firstName"]."";
$MESSAGE_BODY .= "Last Name: ".$_POST["lastName"]."";
您正在覆盖包含firstName
的第一个$ MESSAGE_BODY答案 1 :(得分:1)
使用单引号".
"
,尝试以下操作
<?php
$MESSAGE_BODY .= "First Name: '$_POST["firstName"]'";
$MESSAGE_BODY .= "Last Name: '$_POST["lastName"]'";
$MESSAGE_BODY .= "Email: '$_POST["email"]'";
$MESSAGE_BODY .= "Comment: 'nl2br($_POST["message"])'";
?>
答案 2 :(得分:0)
您的php脚本中存在问题
$ToEmail = 'myemail@mycompany.com';
$EmailSubject = 'BGM Contact Form';
$mailheader = "From: ".$_POST["email"]."\r\n";
$mailheader .= "Reply-To: ".$_POST["email"]."\r\n";
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
$MESSAGE_BODY = "First Name: ".$_POST["firstName"]."";
$MESSAGE_BODY .= "Last Name: ".$_POST["lastName"]."";
$MESSAGE_BODY .= "Email: ".$_POST["email"]."";
$MESSAGE_BODY .= "Comment: ".nl2br($_POST["message"])."";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");
使用上面的php脚本