0 Replies Latest reply: Aug 13, 2012 2:23 PM by Kuronk RSS

    PHP Email Attachment Trouble Shooting

    Kuronk Community Member

      I am having problems finding a good example php script to send an attachment in my email form. I have written many scripts. Can someone help me? I will attach my current script below.

       

      <?php

       

      $subject = 'Online Application Candidate';

       

      $emailadd = 'kuronk@duq.edu';

       

      $url = 'http://www.mydomain.com/Form/application_recieved.htm';

       

      $req = '0';

       

       

      $mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";

       

       

         $tmp_name = $_FILES['file']['tmp_name'];

         $type = $_FILES['file']['type'];

         $name = $_FILES['file']['name'];

         $size = $_FILES['file']['size'];

       

       

      $text = "MIME-Version: 1.0\r\n" .

          "Content-Type: multipart/mixed;\r\n" .

          " boundary=\"{$mime_boundary}\"";

                "--{$mime_boundary}\n" .

          "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .

          "Content-Transfer-Encoding: 7bit\n\n" .

          $text . "\n\n";

                "--{$mime_boundary}\n" .

          "Content-Type: {$type};\n" .

          " name=\"{$name}\"\n" .

          "Content-Disposition: attachment;\n" .

          " filename=\"{$Form_Attachments}\"\n" .

          "Content-Transfer-Encoding: base64\n\n" .

          $data . "\n\n" .

          "--{$mime_boundary}--\n";

       

      $text = "Application Results:\n\n";

      $space = ' ';

      $line = '

      ';

      foreach ($_POST as $key => $value)

      {

      if ($req == '1')

      {

      if ($value == '')

      {echo "$key is empty";die;}

      }

      $j = strlen($key);

      if ($j >= 20)

      {echo "Name of form element $key cannot be longer than 20 characters";die;}

      $j = 20 - $j;

      for ($i = 1; $i <= $j; $i++)

      {$space .= ' ';}

      $value = str_replace('\n', "$line", $value);

      $conc = "{$key}:$space{$value}$line";

      $text .= $conc;

      $space = ' ';

      }

       

       

      function heal($str) {

                $injections = array('/(\n+)/i',

                '/(\r+)/i',

                '/(\t+)/i',

                '/(%0A+)/i',

                '/(%0D+)/i',

                '/(%08+)/i',

                '/(%09+)/i'

                );

                $str= preg_replace($injections,'',$str);

                return $str;

      }

       

       

      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;

                }

      }

      $allowedExts = array("doc", "docx", "xml", "xls", "xlsx", "pdf");

      $extension = end(explode(".", $_FILES["file"]["name"]));

      if ((($_FILES["file"]["type"] == "application/doc")

      || ($_FILES["file"]["type"] == "application/docx")

      || ($_FILES["file"]["type"] == "application/xml")

      || ($_FILES["file"]["type"] == "application/xls")

      || ($_FILES["file"]["type"] == "application/xlsx")

      || ($_FILES["file"]["type"] == "application/pdf"))

      && ($_FILES["file"]["size"] < 20000)

      && in_array($extension, $allowedExts))

        {

        if ($_FILES["file"]["error"] > 0)

          {

          echo "Return Code: " . $_FILES["file"]["error"] . "<br />";

          }

        else

          {

          echo "Upload: " . $_FILES["file"]["name"] . "<br />";

          echo "Type: " . $_FILES["file"]["type"] . "<br />";

          echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";

          echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

       

       

          if (file_exists("upload/" . $_FILES["file"]["name"]))

            {

            echo $_FILES["file"]["name"] . " already exists. ";

            }

          else

            {

            move_uploaded_file($_FILES["file"]["tmp_name"],

            "upload/" . $_FILES["file"]["name"]);

            echo "Stored in: " . "upload/" . $_FILES["file"]["name"];

            }

          }

        }

      else

        {

        echo "Invalid file";

        }

      mail($emailadd, $subject, $text, 'From: '.$emailadd.'');

      echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';

      ?>