使用php通过电子邮件发送以联系表格形式捕获

时间:2015-09-18 03:24:24

标签: php forms contact

所以我有一个联系表格,我想把捕获的信息发送到电子邮件。我想我错过了代码结构的东西。 PHP真的很新。抱歉,如果我在这里听起来很蠢。

刚刚更新了以下代码,以反映最近答案中建议的更改

    <form method="post" action="/home-value/">

<div class="hv-form">
<h4>Get Your Instant Home Value</h4>
<div class="street-row"><span class=""><input type="text" placeholder="Street" aria-invalid="false" class="" size="40" value="" name="Street"></span></div>

<div class="city-box"><span class=""><input type="text" placeholder="City" aria-invalid="false" class="" size="40" value="" name="City"></span></div>

<div class="state-box"><span><input type="text" placeholder="State" aria-invalid="false" size="40" value="" name="State"></span></div>

<div class="zip-box"><span ><input type="text" placeholder="Zip" aria-invalid="false"  size="40" value="" name="Zip"></span></div>
<div class="name-box"><span ><input type="text" placeholder="Name" aria-invalid="false"  size="40" value="" name="Name"></span></div>
<div class="phone-box"><span ><input type="tel" placeholder="Phone (Not Required)" aria-invalid="false"  size="40" value="" name="Phone"></span></div>
<div class="email-box"><span ><input type="email" placeholder="Email (Not Required)" aria-invalid="false"  size="40" value="" name="Email"></span></div>
<input type="hidden" name="form" id="form" value="form1" />
<div class="submit-box"><input type="submit" class="hv-submit" value="Get Home Value"></div>

</div>
</form>
<?php
if(isset($_POST['form']) && $_POST['form'] == 'form1')) {
$name=$_POST['Name'];
$name=$_POST['Name'];
$street=$_POST['Street'];
$email=$_POST['Email'];
$state=$_POST['State'];
$city=$_POST['City'];
$zip=$_POST['Zip'];
$phone=$_POST['Phone'];


// the message
$msg = "Street:".$street."\r\n" .
"City:".$city."\r\n" .
"State:".$state."\r\n" .
"Zip:".$zip."\r\n" .
"Name:".$name."\r\n" .
"Phone:".$phone."\r\n" .
"Email:".$email.;

// use wordwrap() if lines are longer than 70 characters
$msg = wordwrap($msg,70);
$headers='From:Home Valuation Lead Form'."\n".'Reply-to:none';
$to ='briandresscc@gmail.com';
$subject='New Home Valuation Contact Form has been filled out on Angelandpatty.com';
// send email
mail($to,$subject,$msg, $headers);
}
?>


    <?PHP

}

?>

1 个答案:

答案 0 :(得分:0)

您的电子邮件是在处理表单之前发送的,而您的电子邮件是空的。

尝试在提交按钮

之前添加此行
<input type="hidden" name="form" id="form" value="form1" />

然后改变这个

<?php
$name=$_POST['Name'];

到这个

<?php
if(isset($_POST['form']) && $_POST['form'] == 'form1') {
$name=$_POST['Name'];

并更改此

mail($to,$subject,$msg, $headers);
?>

到这个

mail($to,$subject,$msg, $headers);
}
?>

另外,您的消息行应编码如下

$msg = "Street:".$street."\r\n" .
"City:".$city."\r\n" .
"State:".$state."\r\n" .
"Zip:".$zip."\r\n" .
"Name:".$name."\r\n" .
"Phone:".$phone."\r\n" .
"Email:".$email;

然后将</form>标记移到这些行上方

,将您的电子邮件代码从表单中删除
</div>
<?php
$name=$_POST['Name'];

一起看起来像这样

<form method="post" action="/home-value/">
<div class="hv-form">
<h4>Get Your Instant Home Value</h4>
<div class="street-row"><span class=""><input type="text" placeholder="Street" aria-invalid="false" class="" size="40" value="" name="Street"></span></div>
<div class="city-box"><span class=""><input type="text" placeholder="City" aria-invalid="false" class="" size="40" value="" name="City"></span></div>
<div class="state-box"><span><input type="text" placeholder="State" aria-invalid="false" size="40" value="" name="State"></span></div>
<div class="zip-box"><span ><input type="text" placeholder="Zip" aria-invalid="false"  size="40" value="" name="Zip"></span></div>
<div class="name-box"><span ><input type="text" placeholder="Name" aria-invalid="false"  size="40" value="" name="Name"></span></div>
<div class="phone-box"><span ><input type="tel" placeholder="Phone (Not Required)" aria-invalid="false"  size="40" value="" name="Phone"></span></div>
<div class="email-box"><span ><input type="email" placeholder="Email (Not Required)" aria-invalid="false"  size="40" value="" name="Email"></span></div>
<input type="hidden" name="form" id="form" value="form1" />
<div class="submit-box"><input type="submit" class="hv-submit" value="Get Home Value"></div>

</div>
</form>
<?php
if(isset($_POST['form']) && $_POST['form'] == 'form1') {
$name=$_POST['Name'];
$street=$_POST['Street'];
$email=$_POST['Email'];
$state=$_POST['State'];
$city=$_POST['City'];
$zip=$_POST['Zip'];
$phone=$_POST['Phone'];

// the message
$msg = "Street:".$street."\r\n" .
"City:".$city."\r\n" .
"State:".$state."\r\n" .
"Zip:".$zip."\r\n" .
"Name:".$name."\r\n" .
"Phone:".$phone."\r\n" .
"Email:".$email;

// use wordwrap() if lines are longer than 70 characters
$msg = wordwrap($msg,70);
$headers='From:Home Valuation Lead Form'."r\n".'Reply-to:none';
$to ='briandresscc@gmail.com';
$subject='New Home Valuation Contact Form has been filled out on Angelandpatty.com';
// send email
mail($to,$subject,$msg, $headers);
}
?>

快乐的编码!