1

我正在使用网站模板在我自己的家庭服务器上建立一个网站。联系表格使用 php 和 php mail() 函数通过电子邮件发送到联系表中输入的信息,但我没有本地 smtp 服务器可在 mail() 中使用。因此,我想更改代码以将提交的信息保存到文本文件中。如果有可能的方法可以做到这一点,有人可以指出我正确的方向。我目前的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);
    ?>   
4

3 回答 3

2

我强烈建议您使用数据库条目而不是文本文件,因为后者很快就会变得非常混乱。研究使用 MySQLi API for PHP 设置 SQL 服务器和数据条目。

注意:您可以使用 Gmail 或无数其他免费电子邮件服务作为您的 SMTP,请参阅从 PHP 页面使用 GMail SMTP 服务器发送电子邮件

希望有帮助!

于 2013-11-13T20:20:22.400 回答
2

你应该使用fwritephp的功能!最好将内容放在 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);

?>
于 2013-11-13T20:27:53.253 回答
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);
}
于 2013-11-13T20:23:34.817 回答