我正在使用网站模板在我自己的家庭服务器上建立一个网站。联系表单使用php和php mail()函数通过电子邮件发送到联系表中输入的信息,但我没有在mail()中使用本地smtp服务器。所以我想改变代码,而不是将提交的信息保存到文本文件中。如果有可能的方法,有人可以指出我正确的方向。我目前的html代码和php代码如下: HTML:
<form action="mailer.php" id="contact_form" method="post">
<ul class="form">
<li class="short">
<label>First Name<span class="required"></span></label>
<input type="text" name="first" id="first" value="First Name" class="requiredField" onblur="if(this.value == '') { this.value = 'First Name'; }" onfocus="if(this.value == 'First Name') { this.value = ''; }" />
</li>
<li class="short">
<label>Last Name<span class="required"></span></label>
<input type="text" name="last" id="last" value="Last Name" class="requiredField" onblur="if(this.value == '') { this.value = 'Last Name'; }" onfocus="if(this.value == 'Last Name') { this.value = ''; }" />
</li>
<li class="long">
<label>Email Address<span class="required"></span></label>
<input type="text" name="email" id="email" value="Email Address" class="requiredField email" onblur="if(this.value == '') { this.value = 'Email Address'; }" onfocus="if(this.value == 'Email Address') { this.value = ''; }" /> </li>
<li class="short">
<label>Company Name</label>
<input type="text" name="company" id="company" value="Company Name" class="requiredField" onblur="if(this.value == '') { this.value = 'Company Name'; }" onfocus="if(this.value == 'Company Name') { this.value = ''; }" />
</li>
<li class="short">
<label>Telephone Number</label>
<input type="text" name="phone" id="phone" value="Telephone Number" class="requiredField" onblur="if(this.value == '') { this.value = 'Telephone Number'; }" onfocus="if(this.value == 'Telephone Number') { this.value = ''; }" />
</li>
<li class="textarea">
<label>Message<span class="required"></span></label>
<textarea name="message" id="message" rows="20" cols="30"></textarea>
</li>
<li class="button"><input name="submitted" id="submitted" value="Submit" class="submit" type="submit" />
</li>
PHP:
<?php
$email_to = "michaelggerhart@gmail.com";
$first = $_POST["first"];
$last = $_POST["last"];
$email = $_POST["email"];
$company = $_POST["company"];
$phone = $_POST["phone"];
$message = $_POST["message"];
$text = "NAME: $first $last <br>
EMAIL: $email<br>
COMPANY: $company<br>
TELEPHONE NUMBER: $phone<br>
MESSAGE: $message";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html; charset=utf-8" . "\r\n";
$headers .= "From: <$email>" . "\r\n";
mail($email_to, "Message", $text, $headers);
?>
答案 0 :(得分:2)
我强烈建议您使用数据库条目而不是文本文件,因为后者变得非常混乱。研究使用MySQLi API for PHP设置SQL服务器和数据条目。
注意:您可以使用Gmail或无数其他免费电子邮件服务作为SMTP,请参阅Send email using the GMail SMTP server from a PHP page
希望有所帮助!
答案 1 :(得分:2)
你应该使用php的fwrite
功能!将内容放在html文件中会更好,因为您可以使用<br>
,设置页面样式并从浏览器访问它
<?php
$first = $_POST["first"];
$last = $_POST["last"];
$email = $_POST["email"];
$company = $_POST["company"];
$phone = $_POST["phone"];
$message = $_POST["message"];
$text = "NAME: $first $last <br>
EMAIL: $email<br>
COMPANY: $company<br>
TELEPHONE NUMBER: $phone<br>
MESSAGE: $message<br><hr><br><br><br>";
$file = fopen("contactrequests.html","a+");
fwrite($file, $text);
fclose($file);
?>
答案 2 :(得分:1)
你可以改变这一行,而不是测试
mail($email_to, "Message", $text, $headers);
到
if(!mail($email_to, "Message", $text, $headers)){
$file = 'yourfilename.txt';//must give correct path if in another place
// Open the file to get existing content
$current = file_get_contents($file);
// Append a new data to the file
$current .= $text"\n\n\n";
// Write the contents back to the file
file_put_contents($file, $current);
}