Reputation: 469
I've made a very simple email form in HTML that should be running a php script to send an email to my email address:
<form id="emailForm" action="sendMail.php" method="post">
Name:<br>
<input type="text" name="name" value="your name"><br>
E-mail:<br>
<input type="text" name="email" value="your email"><br>
Message:<br>
<textarea cols="40" rows="5" name="message">your message</textarea><br>
<input type="submit" value="Send">
<input type="reset" value="Reset">
</form>
My php script looks like this:
<?php
// This function checks for email injection. Specifically, it checks for carriage returns - typically used by spammers to inject a CC list.
function isInjected($str) {
$injections = array('(\n+)',
'(\r+)',
'(\t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str)) {
return true;
}
else {
return false;
}
}
// Load form field data into variables.
$name = $_REQUEST['name'] ;
$email_address = $_REQUEST['email'] ;
$comments = $_REQUEST['message'] ;
// If the user tries to access this script directly, redirect them to feedback form,
if (!isset($_REQUEST['email'])) {
header( "Location: contact.html" );
}
// If the form fields are empty, redirect to the error page.
elseif (empty($email_address) || empty($comments) || empty($name)) {
header( "Location: error_message.html" );
}
// If email injection is detected, redirect to the error page.
elseif ( isInjected($email_address) ) {
header( "Location: error_message.html" );
}
// If we passed all previous tests, send the email!
else {
mail( "[email protected]", "Message From Your Website!",
$comments, "From: $email_address" );
header( "Location: thank_you.html" );
}
?>
As I am quite new to scripting, this script is mostly taken from example code, but it all appears to make sense to me. The html page that the form is located in and the php file are both uploaded to the same directory on the server. However, when I press the "submit" button on the form, I am simply taken in my browser to a text version of the php file itself (ie I just go to the php file in my directory) and no email is sent. Why might this be happening?
Upvotes: 1
Views: 576
Reputation: 22711
Can you try this,
$headers = 'From: '.$email_address . "\r\n" .
'Reply-To: '.$email_address . "\r\n" ;
mail( "[email protected]", "Message From Your Website!", $comments, $headers);
Upvotes: 0
Reputation: 943142
You need to run your PHP program by having your browser visit an HTTP(S) URL provided by a web server that supports PHP and is configured to execute it for the file you have placed your PHP code in.
Either:
.php
extension)Upvotes: 2