我是编码的新手,所以我基本上从各种来源复制了下面的代码,直到我认为它可能有用,但不是,我需要的是将表单信息从html页面发送到电子邮件一旦填满。请帮忙。
表单的php代码:
<?php ini_set('display_errors',1);?>
$emailTo = '<abc@gmail.com>';
if($_SERVER['REQUEST_METHOD'] == 'POST') {
$Name=$_POST['Name'];
$Mobile=$_POST['Mobile'];
$Email=$_POST['Email'];
$City=$_POST['City'];
$emailIsValid = preg_match('/^[^0-9][A-z0-9_]+([.][A-z0-9_]+)*[@][A-z0-9_]+([.][A-z0-9_]+)*[.][A-
z]{2,4}$/', $Email);
if($Name && $Mobile $Email && $emailIsValid && $City){
$subject = "[Contact via Site] $subject";
$body = "Name: $Name <br /> Email: $Email <br /> Mobile: $Mobile <br /> City: $City";
$headers = 'MIME-Version: 1.1' . PHP_EOL;
$headers .= 'Content-type: text/html; charset=utf-8' . PHP_EOL;
$headers .= "From: $name <$Email>" . PHP_EOL;
$headers .= "Return-Path: $emailTo" . PHP_EOL;
$headers .= "Reply-To: $Email" . PHP_EOL;
mail($emailTo, $subject, $body, $headers);
$emailSent = true;
} else {
$hasError = true;
}
}
?>
html代码: 谢谢,我们很快就会打电话给你 错误请检查所有字段。
<div class="col-md-6 col-md-offset-3">
<form action="<?php echo $_SERVER['/Thanks.html']; ?>" id="FORM" class="form-horizontal"
role="form" method="post" style="height: 300px; padding-left:10px; padding-top:10px;">
<div class="formContainer" >
<div class="label">
<label>Name: </label>
</div>
<div class="textbox">
<input type="text" id=name name="Name"/>
</div> <br>
<div class="label">
<label>Mobile: </label>
</div>
<div class="textbox">
<input type="tel" id=mobile name="Mobile" />
</div> <br>
<div class="label">
<label>Email: </label>
</div>
<div class="textbox">
<input type="email" id=email name="Email"/>
</div> <br>
<div class="label">
<label>City: </label>
</div>
<div class="textbox">
<input type="text" id=city name="City"/>
</div> <br>
<div class="submit">
<input type="submit" value="Submit">
</div>
</form>
答案 0 :(得分:0)
只需使用这个PHPMailer类..它经过优化,功能齐全,可以发送电子邮件,设置附件,更改标题属性等等,甚至无需知道它们是如何实现的: https://github.com/PHPMailer/PHPMailer