Reputation: 57
I am setting up a website on my own home server using a website template. The contact form uses php and the php mail() function to email to info entered in the contact sheet, but I don't have a local smtp server to use in the mail(). So I want to alter the code to instead save the submitted info to a text file. If there is a possible way to do this can someone point me in the correct direction. my current html code and php code are as follows: 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 = "[email protected]";
$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);
?>
Upvotes: 1
Views: 12066
Reputation: 818
You should use fwrite
function of php! And it would be better to put the content in a html file, because you can use <br>
, style the page and access it from your browser
<?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);
?>
Upvotes: 2
Reputation: 1653
you can alter this line, not tested
mail($email_to, "Message", $text, $headers);
to
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);
}
Upvotes: 1
Reputation: 470
I would strongly recommend that you use a Database entry rather than a text file as the later becomes really messy really quickly. Look into setting up an SQL server and data entries using the MySQLi API for PHP.
Note: you can use Gmail or countless other free e-mail services as your SMTP see Send email using the GMail SMTP server from a PHP page
Hope that helps!
Upvotes: 2