I've made a PHP script that loops through POST data from an ajax request and sends an email on to the appropriate recipient. For the most part, the script works, however i have two functions which are there to validate and sanitize the POST data.
The problem is that these two functions are not being called, and are in fact being evaluated as strings when the email actually gets sent out. I've tried several different things to get this to function accordingly, including passing in variables by reference, to no avail. Could anyone here spot the issue?
<?php
error_reporting(E_ALL);
function validate($value){
$value = strip_tags($value);
$value = htmlspecialchars($value);
$value = stripcslashes($value);
return $value;
}
function detectUrl($string){
$pattern = "/https?\:\/\/[^\" ]+/i";
$val = preg_match_all($pattern, $string);
if($val > 0){
$replace = preg_replace($pattern, '', $string);
return $replace;
} else {
return $string;
}
}
if(isset($_POST) === true){
# Include our DB connection
if (!empty($include_func)) {
require_once $_module_dir . XC_DS . 'func.php';
}
// Current date
$date = date("d/m/y");
// Get Form content
$content = $_POST;
// Begin message body
#
# Prepare message body
##
$message = array();
$message[] = '<!DOCTYPE html>';
$message[] = '<html>';
$message[] = '<head>
<title>Sunglasses.ie Prescription form</title>
<style>
#wrapper {
width: 960px;
margin: 0 auto;
padding: 0 20px;
}
h1 {
text-align: center;
}
h3 {
text-align: center;
background: #eee;
border-top: 1px solid #000;
border-bottom: 1px solid #000;
padding: 5px 0;
}
table {
width: 100%;
margin-bottom: 20px;
}
table thead th {
border-bottom: 2px solid #ddd;
text-align: left;
vertical-align: bottom;
}
table tbody td {
border-bottom: 1px solid #ddd;
vertical-align: top;
}
table tfoot td {
border-top: 2px solid #000;
vertical-align: bottom;
}
</style>
</head>';
$message[] = '<body>';
$message[] = '<div id="wrapper">';
$message[] = '<img src="test.jpg" alt="Sunglasses.ie">';
$message[] = "<h1>A new Prescription has been generated: {$date}</h1>";
$message[] = '<table>';
$message[] = '<tbody>';
foreach($content as $key => $value){
$message[] = '<tr>';
$message[] = "<td style='padding:8px;font-weight:bold;text-transform:uppercase;'>{$key}</td>";
if(empty($value) || $value === null){
$message[] = "<td style='text-transform:uppercase;'>0</td>";
} else {
$message[] = "<td style='text-transform:uppercase;'>{$value}</td>";
}
$message[] = '</tr>';
}
$stop = 5*ceil(((count($content)-3)/2)/5); # get array length to nearest multiple of 5
/** Single POST Array **/
for($row=3; $row<$stop+3; $row+=5){ // count $_POST entries
$message[] = '<tr>';
for($col=0; $col<5; $col++){ // simple column count
if($col==2) {
$message[] = "<td> </td>";
} else if($col==0){
if(!empty($_POST[$row])){
$message[] = "<td style='padding:8px;font-weight:bold;text-transform:uppercase;'>".$_POST[$row]."</td>";
} else {
$message[] = "<td style='padding:8px;font-weight:bold;text-transform:uppercase;'> </td>";
}
} else if($col==3){
if(!empty($_POST[$row+3])){
$message[] = "<td style='padding:8px;font-weight:bold;text-transform:uppercase;'>".$_POST[$row+3]."</td>";
} else {
$message[] = "<td style='padding:8px;font-weight:bold;text-transform:uppercase;'> </td>";
}
} else if($j==1){
if (!empty($_POST[$row+1])){
$message[] = "<td style='text-transform:uppercase;'>".detectUrl(validate($_POST[$row+1]))."</td>";
} else {
$message[] = "<td style='text-transform:uppercase;'> </td>";
}
} else if($j==4){
if (!empty($_POST[$row+4])){
$message[] = "<td style='text-transform:uppercase;'>".detectUrl(validate($_POST[$row+3]))."</td>";
} else {
$message[] = "<td style='text-transform:uppercase;'> </td>";
}
}
}
$message[] = '</tr>';
}
$message[] = '</tbody>';
$message[] = '</table>';
#
# Prepare email headers
##
$to = 'me@me.ie';
$subject = 'A new Prescription form has been generated';
$headers = array();
$headers[] = "To: {$to}";
$headers[] = 'From: Prescriptions ';
$headers[] = 'Reply-To: Prescriptions - ';
$headers[] = "Subject: {$subject}";
$headers[] = 'X-Mailer: PHP/'.phpversion();
$headers[] = 'MIME-Version: 1.0';
$headers[] = 'Content-type: text/html; charset=iso-8859-1';
if(!empty($content)){
#
# Print message on the screen
##
print implode("\r\n", $message);
#
# Send email to intended recipients
##
mail('', $subject, implode("\r\n", $message), implode("\r\n", $headers));
echo 'Email Sent!';
echo '<pre>';
print_r($_POST);
echo '</pre>';
} else {
echo 'Mail not sent';
}
} else {
echo '<br>';
echo 'Nah';
}
The result from this script produces this: