我希望能够从已经上线的网站表单中收集所有数据并将其发送到我的电子邮件。
我在这里基于这个问题,并基于此调整了我的 php。但是,当我单击提交按钮时,字段变为空,但是当我检查电子邮件时,我什么也没收到。
如何从网站收集数据并发送到我的电子邮件?
我的代码如下所示:
<form role="form" method="POST">
<br style="clear:both">
<h3 style="margin-bottom: 25px; text-align: center;">Contact a Conveyancing Property Lawyer Now</h3>
<div class="form-group">
<input type="text" class="form-control" id="name" name="name" placeholder="Name" required>
</div>
<div class="form-group">
<input type="text" class="form-control" id="email" name="email" placeholder="Email" required>
</div>
<div class="form-group">
<input type="text" class="form-control" id="mobile" name="mobile" placeholder="Contact Number" required>
</div>
<div class="form-group">
<input type="text" class="form-control" id="subject" name="subject" placeholder="Subject" required>
</div>
<div class="form-group">
<select name="situation" id="situation">
<option value="Unemployed">Unemployed</option>
<option value="Employed">Employed</option>
</select>
</div>
<button type="submit" id="submit" name="submit" class="btn btn-primary">Submit</button>
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$mobile = $_POST['mobile'];
$subject = $_POST['subject'];
$situation = $_POST['situation'];
$from = 'sidney@web2web.co.za';
$to = 'sidney@web2web.co.za';
$subject = '$subject';
$body ="From: $namen E-Mail: $emailn Mobile:n $mobile Subject: $subjectn Situation:n $situation";
// set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "rn";
$headers .= "Content-type:text/html;charset=UTF-8" . "rn";
// More headers optional/headers
$headers .= "From:$from";
if (mail($to,$subject,$body,$headers)) {
$result='<div class="alert alert-success">Thank You! I will be in touch</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
}
}
?>
</form>
PHP 需要一个已安装并正在运行的邮件系统。要使用的程序由 php.ini 文件中的配置设置定义。
首先,您需要更改 PHP 邮件配置:
- 打开你的 php.ini 文件。
- 搜索[邮件功能]。
- 添加/更改邮件服务器的详细信息。这可以是本地邮件服务器或ISP的邮件服务器。
- 保存 php.ini 文件。
- 重新启动服务器。
您可以使用 PHPMailer for mail
这可能是世界上最流行的从PHP发送电子邮件的代码!
你的代码是完美的。 我在一些小改动后检查了它,就像你把$subject放在单引号中是错误的,你也忘了打印输出,这是$result的。 其他明智的是,您的代码是完美的。
运行此代码,如果显示错误,请启用"发送邮件"选项到您的服务器。
<form role="form" method="POST">
<br style="clear:both">
<h3 style="margin-bottom: 25px; text-align: center;">Contact a Conveyancing Property Lawyer Now</h3>
<div class="form-group">
<input type="text" class="form-control" id="name" name="name" placeholder="Name" required>
</div>
<div class="form-group">
<input type="text" class="form-control" id="email" name="email" placeholder="Email" required>
</div>
<div class="form-group">
<input type="text" class="form-control" id="mobile" name="mobile" placeholder="Contact Number" required>
</div>
<div class="form-group">
<input type="text" class="form-control" id="subject" name="subject" placeholder="Subject" required>
</div>
<div class="form-group">
<select name="situation" id="situation">
<option value="Unemployed">Unemployed</option>
<option value="Employed">Employed</option>
</select>
</div>
<button type="submit" id="submit" name="submit" class="btn btn-primary">Submit</button>
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$mobile = $_POST['mobile'];
$subject = $_POST['subject'];
$situation = $_POST['situation'];
$from = 'sidney@web2web.co.za';
$to = 'sidney@web2web.co.za';
$subject = $subject;
$body ="From: $namen E-Mail: $emailn Mobile:n $mobile Subject: $subjectn Situation:n $situation";
// set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "rn";
$headers .= "Content-type:text/html;charset=UTF-8" . "rn";
// More headers optional/headers
$headers .= "From:$from";
if (mail($to,$subject,$body,$headers)) {
$result='<div class="alert alert-success">Thank You! I will be in touch</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
}
echo $result;
}
?>