1

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>&nbsp;</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;'>&nbsp;</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;'>&nbsp;</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;'>&nbsp;</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;'>&nbsp;</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:

enter image description here

Zy0n
  • 810
  • 2
  • 14
  • 33
  • Just a quick thing. Many email clients will strip out ` – misterManSam Jun 18 '14 at 11:15
  • Thanks, i just did that for some basic formatting. The issue really is with the functions not being called :/ – Zy0n Jun 18 '14 at 11:16
  • @CianGallagher I think you edited your question and removed the part causing the problem. Is that correct? I don't see `detectUrl(validate($_POST[$row+1]))` anymore. – nl-x Jun 18 '14 at 11:32
  • Sorry, fixed. Removed the wrong paragraph – Zy0n Jun 18 '14 at 11:36
  • One issue is here: `$_POST['$row+3']`. Remove those single quotes. That is in your 'else if($col==3)' section. – Ethan Turk Jun 18 '14 at 11:41
  • Didn't see that, thanks. I'll update the OP. – Zy0n Jun 18 '14 at 11:43
  • Actually `$_POST` is an associative array with key/value pairs. Meaning you cannot simply traverse the entries with an incrementing index like `for($i=0;$i$v) { echo "$k: $v"; }` here. – nl-x Jun 18 '14 at 11:48
  • I've used `foreach()` previously in the first iteration and had the same issue. – Zy0n Jun 18 '14 at 11:50
  • I'm using it within my `for()` loop man. Look and you will see. The functions im using are in the same script at the top of the file. Again as is displayed in my code above.. And no they are not being sent via POST variables. In case i needed to elaborate that.. – Zy0n Jun 18 '14 at 11:55
  • @CianGallagher Please check your quotes (`""`) near your function calls. Maybe some of them have become some curly quotes (`“”`) – nl-x Jun 18 '14 at 12:00

0 Answers0