我希望能够收集我网站上已存在的所有数据并将其发送到我的电子邮箱。
我基于这个问题here,并基于此调整了我的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: $name\n E-Mail: $email\n Mobile:\n $mobile Subject: $subject\n Situation:\n $situation";
// set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// 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>
答案 0 :(得分:0)
PHP需要安装并运行的邮件系统。要使用的程序由php.ini文件中的配置设置定义。
答案 1 :(得分:0)
首先,您需要更改PHP邮件配置:
您可以使用PHPMailer邮件
这可能是世界上最流行的从PHP发送电子邮件的代码!
答案 2 :(得分:0)
您的准则很完美。我在经过一些小改动后检查了它,就像你把$ subject放在单引号中这是错误的,你也忘了打印$ result的输出。另外,你的代码是完美的。
运行此代码,如果显示错误,请为您的服务器启用SEND MAIL选项。
<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: $name\n E-Mail: $email\n Mobile:\n $mobile Subject: $subject\n Situation:\n $situation";
// set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// 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;
}
?>