I have modified my code, not tested it yet as I do not have FTP access at the moment.
Was wondering if this was a better attempt at the code, I am still a relative newbie to programming and am not sure if nesting my statements like this will work.
<?php
//Declare mail function
$to = "webmaster@enemydesign.co.uk";
$nameto = "Webmaster";
$from = $_REQUEST['email'];
$namefrom = $_REQUEST['name'];
$subject = $_REQUEST['subject'] . " " . "Query From Web site";
$message = $_REQUEST['body'];
authSendEmail($from, $namefrom, $to, $nameto, $subject, $message);
//End Function Declaration
//Store contents for string spam check
contains_bad_str($email);
contains_bad_str($subject);
contains_bad_str($message);
//Store contents for new line in spam check
contains_newlines($email);
contains_newlines($subject);
//Filter out non standard email address'
if (!filter_var($from, FILTER_VALIDATE_EMAIL)){
echo "E-Mail is not valid";
}elseif{
//If string_to_test contains any of the string stored in the array then drop out and return an error message to the user
function contains_bad_str($str_to_test) {
$bad_strings = array(
"content-type:",
"mime-version:",
"multipart/mixed",
"Content-Transfer-Encoding:",
"bcc:",
"cc:",
"to:"
);
foreach($bad_strings as $bad_string) {
if(eregi($bad_string, strtolower($str_to_test))) {
echo "$bad_string found. Suspected injection attempt - mail not being sent.";
exit;
}
}
}
}elseif{
//If strings_to_test contains new lines dropout and return an error message to the user
function contains_newlines($str_to_test) {
if(preg_match("/(%0A|%0D|\\n+|\\r+)/i", $str_to_test) != 0) {
echo "<p>newline found in $str_to_test. Suspected injection attempt - mail not being sent.</p>";
exit;
}
}
//If incorect access to the request method is detected drop out and return error message to the user
}elseif($_SERVER['REQUEST_METHOD'] != "POST"){
echo = "<p>Unauthorized attempt to access page.</p>";
exit;
}
//If all is well in the above proceed to send the email
//Authenticate Send
//This will send an email using auth smtp and output a log array
//logArray - connection,
function authSendEmail($from, $namefrom, $to, $nameto, $subject, $message)
{
//SMTP Server Details
/* * * * CONFIGURATION START * * * */
$smtpServer = "smtp.dsl.pipex.com";
$port = "25";
$timeout = "30";
$username = "mizo09@dsl.pipex.com";
$password = "aaronson";
$localhost = "localhost";
$newLine = "\r\n";
/* * * * CONFIGURATION END * * * * */
//Connect to the host on the specified port
$smtpConnect = fsockopen($smtpServer, $port, $errno, $errstr, $timeout);
$smtpResponse = fgets($smtpConnect, 515);
if(empty($smtpConnect)){
$output = "Failed to connect: $smtpResponse";
echo $output;
}
else{
$logArray['connection'] = "Connected: $smtpResponse";
}
//Request Auth Login
fputs($smtpConnect,"AUTH LOGIN" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['authrequest'] = "$smtpResponse";
//Send Username
fputs($smtpConnect, base64_encode($username) . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['authusername'] = "$smtpResponse";
//Send Password
fputs($smtpConnect, base64_encode($password) . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['authpassword'] = "$smtpResponse";
//Say Hello to SMTP
fputs($smtpConnect, "HELO $localhost" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['heloresponse'] = "$smtpResponse";
//Email From
fputs($smtpConnect, "MAIL FROM: $from" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['mailfromresponse'] = "$smtpResponse";
//Email To
fputs($smtpConnect, "RCPT TO: $to" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['mailtoresponse'] = "$smtpResponse";
//The Email
fputs($smtpConnect, "DATA" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['data1response'] = "$smtpResponse";
//Construct Headers
$headers = "MIME-Version: 1.0" . $newLine;
$headers .= "Content-type: text/html; charset=iso-8859-1" . $newLine;
$headers .= "To: $nameto <$to>" . $newLine;
$headers .= "From: $namefrom <$from>" . $newLine;
fputs($smtpConnect, "To: $to\nFrom: $from\nSubject: $subject\n$headers\n\n$message\n.\n");
$smtpResponse = fgets($smtpConnect, 515);
$logArray['data2response'] = "$smtpResponse";
// Say Bye to SMTP
fputs($smtpConnect,"QUIT" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['quitresponse'] = "$smtpResponse";
}
?>
The HTML form looks like this;
<div class="content">
<form action='/contact/mail.php' method='post'>
<table border='0'>
<tr>
<td>Name:</td><td><input type='text' name='name' size='50' maxlength='50' /></td>
</tr><tr>
<td>Email:</td><td><input type='text' name='email' size='50' maxlength='50' /></td>
</tr><tr>
<td>Subject:</td><td><input type='text' name='subject' size='50' maxlength='50' /></td>
</tr><tr>
<td>Message:</td><td><textarea name='body' rows='10' cols='50'></textarea></td>
</tr><tr>
<td> </td><td><input type='submit' value='Submit' /> <input type='reset' value='Reset' /></td>
</tr>
</table>
</form>
<?php include ($_SERVER['DOCUMENT_ROOT'] . "/includes/sendmail.php");?>
</div>
</div>
<!-- primary content end -->
</div>
<?php include($_SERVER['DOCUMENT_ROOT'] . "/includes/sidebar.inc.php");?>
</div>
<?php include($_SERVER['DOCUMENT_ROOT'] . "/includes/footer.inc.php");?>
When the page has reloaded it stops right after <?php include ($_SERVER['DOCUMENT_ROOT'] . "/includes/sendmail.php");?>.