wilcochris

upload and send email

Jul 21st, 2014
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.51 KB | None | 0 0
  1. if ($file_size > 2048000)
  2. {
  3.     header ("refresh: 5; url=attach.php");
  4.     include ("header.php");
  5.     echo "File size is to big. Size must be no bigger than 2Mb. Please go <a href='attach.php'>back</a>";
  6.     include ("footer.php");
  7.     exit;
  8. }
  9. else
  10. {
  11.     $pics = array(".bmp", ".gif", ".jpg", "jpeg", ".png"); //5
  12.     $docs = array(".doc", "docx", ".odt", ".pdf", ".ppt", "pptx", ".rtf", ".txt", ".xls", "xlsx"); //10
  13.     $misc = array(".csv", ".htm", "html", ".php", ".pkt", ".rar", ".sql", ".xpi", ".zip"); //9
  14.                        
  15.     $base = basename($file_name);
  16.     $extension = substr($base, strlen($base)-4, strlen($base));
  17.     $extension = strtolower($extension);
  18.                        
  19.     if (in_array($extension,$pics))
  20.     {
  21.         $target = "".FILES."/".FUP_PICS."/";
  22.     }
  23.                        
  24.     if (in_array($extension,$docs))
  25.     {
  26.         $target = "".FILES."/".FUP_DOCS."/";
  27.     }
  28.                            
  29.     if (in_array($extension,$misc))
  30.     {
  31.         $target = "".FILES."/".FUP_MISC."/";
  32.     }
  33.                        
  34.     $target = $target.$base;
  35.                        
  36.     $allowed_extensions = array(".bmp", ".csv", ".doc", "docx", ".gif", ".htm", "html",
  37.                 ".jpg", ".JPG", "jpeg", "JPEG", ".odt", ".pdf", ".php", ".pkt", ".png", ".ppt", "pptx",
  38.                 ".rtf", ".sql", ".txt", ".xls", "xlsx", ".zip"
  39.                 );
  40.  
  41.     if(in_array($extension,$allowed_extensions))
  42.     {
  43.         $from = ($_POST['from']);
  44.         $emailfrom = "[email protected]";
  45.         $emailto = "[email protected]";
  46.         $bcc = ($_POST['bcc']);
  47.         $subject = htmlspecialchars($_POST['subject']);
  48.         $sig = ($_POST['sig']);
  49.         $message = htmlspecialchars($_POST['message'], ENT_NOQUOTES);
  50.         $message1 = htmlspecialchars_decode($_POST['message'], ENT_QUOTES);
  51.         if ($sendhash == 'Y')
  52.         {
  53.             $message1 .= "\n\nThe following is your unique message ID: ";
  54.             $message1 .= $guid;
  55.             $message1 .= "\n\nAttachment has been scanned for viruses and is virus free.";
  56.             $message1 .= "\n\nPlease make sure the first part of the file name matches the unique message ID. If it does not, please DO NOT open the file";
  57.             $message1 .= "\n\nTo verify the validity of the message, click the link below or copy and paste it into your browser:";
  58.             $message1 .= "\n\n ";
  59.             $message1 .= HTTP_PATH;
  60.             $message1 .= "verify.php?uid=";
  61.             $message1 .= urlencode($guid);
  62.             $message1 .= "\n\nPlease note, verification link expires on ";
  63.             $message1 .= $expirydate;
  64.         }
  65.         else
  66.         {
  67.         }
  68.         if ($signature == 'Y')
  69.         {
  70.             if ($sig == "")
  71.             {
  72.                 $message1.= "\n\n--\n Sent from Chris' Address Book";
  73.             }
  74.             else
  75.             {
  76.                 $message1 .= "\n\n--\n".$sig;
  77.             }
  78.         }
  79.         else
  80.         {
  81.         }
  82.                            
  83.         $file = $temp_name;
  84.         $content = chunk_split(base64_encode(file_get_contents($file)));
  85.         $uid = md5(uniqid(time()));
  86.                            
  87.         if ($from == 'other')
  88.         {
  89.             $header = "From: ".$emailfrom."\r\n";
  90.             $header .= "Reply-To: ".$emailfrom."\r\n";
  91.             $from = $emailfrom;
  92.         }
  93.         else
  94.         {
  95.             $header = "From: ".$from."\r\n";
  96.             $header .= "Reply-To: ".$from."\r\n";                              
  97.         }
  98.         $header .= "Bcc: ".$bcc."\r\n";
  99.         $header .= "MIME-Version: 1.0\r\n";
  100.                            
  101.         $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
  102.         $header .= "This is a multi-part message in MIME format. \r\n";
  103.                    
  104.         $header .= "--".$uid."\r\n";
  105.         $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
  106.         $header .= $message1."\r\n";
  107.                            
  108.         $header .= "--".$uid."\r\n";
  109.         $header .= "Content-Type: ".$file_type."; name=\"".$file_name."\"\r\n";
  110.         $header .= "Content-Transfer-Encoding: base64\r\n";
  111.         $header .= "Content-Disposition: attachment; filename=\"".$file_name."\"\r\n\r\n";
  112.         $header .= $content."\r\n";
  113.                            
  114.         if ($_POST['emailto'] == '')
  115.         {
  116.             try
  117.             {
  118.                 $esql = $conn->prepare("SELECT * FROM ".PERSON." JOIN contact ON contact.personID = person.adbkid WHERE email1 = '$bcc' OR email2 = '$bcc'");
  119.                 $esql->execute();
  120.                                
  121.                 $esql->bindColumn('adbkid', $aid);
  122.                 $esql->bindColumn('pid', $pid);
  123.                                    
  124.                 $esqlc = $conn->prepare("SELECT COUNT(*) FROM ".PERSON." JOIN contact ON contact.personID = person.adbkid WHERE email1 = '$bcc' OR email2 = '$bcc'");
  125.                 $esqlc->execute();
  126.                                    
  127.                 $num = $esqlc->fetchColumn();
  128.             }
  129.             catch (PDOException $e)
  130.             {
  131.                 print '<div class="sqlerror">'.$e->getMessage().'</div>';
  132.             }
  133.         }
  134.         else
  135.         {
  136.             try
  137.             {
  138.                 $esql = $conn->prepare("SELECT * FROM ".PERSON." JOIN contact ON contact.personID = person.adbkid WHERE email1 = '$emailto' OR email2 = '$emailto'");
  139.                 $esql->execute();
  140.                                    
  141.                 $esql->bindColumn('adbkid', $aid);
  142.                 $esql->bindColumn('pid', $pid);
  143.                                    
  144.                 $esqlc = $conn->prepare("SELECT COUNT(*) FROM ".PERSON." JOIN contact ON contact.personID = person.adbkid WHERE email1 = '$emailto' OR email2 = '$emailto'");
  145.                 $esqlc->execute();
  146.                        
  147.                 $num = $esqlc->fetchColumn();
  148.             }
  149.             catch (PDOException $e)
  150.             {
  151.                 print '<div class="sqlerror">'.$e->getMessage().'</div>';
  152.             }
  153.         }
  154.  
  155.         while ($esql->fetch(PDO::FETCH_BOUND))
  156.         {                          
  157.             if ($num == 0)
  158.             {
  159.                 $selfid = "EMAILSA";
  160.             }
  161.             else
  162.             {
  163.                 $selfid = $aid;
  164.             }
  165.         }
  166.                            
  167.         if (mail($emailto, $subject, "", $header))
  168.         {
  169.             if ($sendhash == 'Y')
  170.             {
  171.                 try
  172.                 {
  173.                     // $sql=mysql_query("INSERT INTO ".EMAILS." (emailfrom, emailto, bcc, subject, message, getthedate, gettime, randhash, fileatt, fileext, showinsearch, expireit, showinverify, wasviewed, personID) VALUES ('$from', '$emailto', '$bcc', '$subject', '$message', '$getthedate', '$gettime', '$guid', '$file_name', '$extension', '$showinsearch', '$expireit', '$siv', '$wv', '$selfid')");
  174.                     $hsql = $conn->prepare("INSERT INTO ".EMAILS."
  175.                     (emailfrom, emailto, bcc, subject, message, getthedate, gettime, randhash, fileatt, fileext, showinsearch, expireit, showinverify, wasviewed, personID)
  176.                     VALUES
  177.                     (:from, :et, :bcc, :sub, :msg, :gd, :gt, :guid, :fn, :fe, :sis, :ex, :siv, :wv, :aid)");
  178.                                        
  179.                     $hsql->bindValue(':from', $from);
  180.                     $hsql->bindValue(':et', $emailto);
  181.                     $hsql->bindValue(':bcc', $bcc);
  182.                     $hsql->bindValue(':sub', $subject);
  183.                     $hsql->bindValue(':msg', $message);
  184.                     $hsql->bindValue(':gd', $getthedate);
  185.                     $hsql->bindValue(':gt', $gettime);
  186.                     $hsql->bindValue(':guid', $guid);
  187.                     $hsql->bindValue(':fn', $file_name);
  188.                     $hsql->bindValue(':fe', $extension);
  189.                     $hsql->bindValue(':sis', $showinsearch);
  190.                     $hsql->bindValue(':ex', $expireit);
  191.                     $hsql->bindValue(':siv', $siv);
  192.                     $hsql->bindValue(':wv', $wv);
  193.                     $hsql->bindValue(':aid', $selfid);
  194.                                        
  195.                     $hsql->execute();
  196.                                        
  197.                     $lastid = $conn->lastInsertId();
  198.                                
  199.                     $sqlrh = $conn->prepare("INSERT INTO ".HASH." (randhash) VALUES (:guid)");
  200.                                        
  201.                     $sqlrh->bindValue(':guid', $guid);
  202.                            
  203.                     $sqlrh->execute();
  204.                 }
  205.                 catch (PDOException $e)
  206.                 {
  207.                     print '<div class="sqlerror">'.$e->getMessage().'</div>';
  208.                 }
  209.             }
  210.             else
  211.             {
  212.                 $hsql = $conn->prepare("INSERT INTO ".EMAILS."
  213.                 (emailfrom, emailto, bcc, subject, message, getthedate, gettime, fileatt, fileext, showinsearch, expireit, showinverify, wasviewed, personID)
  214.                 VALUES
  215.                 (:from, :et, :bcc, :sub, :msg, :gd, :gt, :fn, :fe, :sis, :ex, :siv, :wv, :aid)");
  216.                                    
  217.                 $hsql->bindValue(':from', $from);
  218.                 $hsql->bindValue(':et', $emailto);
  219.                 $hsql->bindValue(':bcc', $bcc);
  220.                 $hsql->bindValue(':sub', $subject);
  221.                 $hsql->bindValue(':msg', $message);
  222.                 $hsql->bindValue(':gd', $getthedate);
  223.                 $hsql->bindValue(':gt', $gettime);
  224.                 $hsql->bindValue(':fn', $file_name);
  225.                 $hsql->bindValue(':fe', $extension);
  226.                 $hsql->bindValue(':sis', $showinsearch);
  227.                 $hsql->bindValue(':ex', $expireit);
  228.                 $hsql->bindValue(':siv', $siv);
  229.                 $hsql->bindValue(':wv', $wv);
  230.                 $hsql->bindValue(':aid', $selfid);
  231.                                    
  232.                 $hsql->execute();
  233.                        
  234.                 $lastid = $conn->lastInsertId();                           
  235.             }
  236.             try
  237.             {
  238.                 $fsql = $conn->prepare("INSERT INTO ".SENTFILES."
  239.                 (filename, filetype, fileext, filesize, filetempname, dateadded, timeadded, fileguid, sentmailid)
  240.                 VALUES
  241.                 (:fn, :ft, :fe, :fs, :tn, :gd, :gt, :guid, :li)");
  242.                                    
  243.                 $fsql->bindValue(':fn', $file_name);
  244.                 $fsql->bindValue(':ft', $file_type);
  245.                 $fsql->bindValue(':fe', $extension);
  246.                 $fsql->bindValue(':fs', $file_size);
  247.                 $fsql->bindValue(':tn', $temp_name);
  248.                 $fsql->bindValue(':gd', $getthedate);
  249.                 $fsql->bindValue(':gt', $gettime);
  250.                 $fsql->bindValue(':guid', $guid);
  251.                 $fsql->bindValue(':li', $lastid);
  252.                                    
  253.                 $fsql->execute();                              
  254.                                    
  255.                 $expire = $conn->prepare("UPDATE ".EMAILS." SET showinverify = 0 WHERE expireit < CURDATE()");
  256.                 $expire->execute();
  257.             }
  258.             catch (PDOException $e)
  259.             {
  260.                 print '<div class="sqlerror">'.$e->getMessage().'</div>';
  261.             }
  262.             //header ("refresh: 5; url=$url");
  263.             include ("header.php");
  264.             $ful = (move_uploaded_file($temp_name, $target)) ? "".$file_name." was uploaded to ".$target."" : "".$file_name.", was not uploaded. Please try a manual upload.";
  265.             echo "<title>Success sending email</title>";
  266.             echo "Your message has been successfully sent.<br/><br/>Message details have been added to the database.<br/><br/>$ful";
  267.             echo "<br/><br/>";
  268.             print_r($_FILES);
  269.             include ("footer.php");
  270.         }
  271.         else
  272.         {
  273.             //header ("refresh: 5; url=$url");
  274.             include ("header.php");
  275.             echo "<title>Error sending email</title>";
  276.             echo "There seems to be an error sending your email.";
  277.             include ("footer.php");
  278.         }
  279.         exit;
  280.     }
  281.     else
  282.     {
  283.         //header ("refresh: 5; url=attach.php");
  284.         include ("header.php");
  285.         echo "File type is not allowed. Please go <a href='attach.php'>back</a>";
  286.         echo"<br/><br/>";
  287.         print_r($_FILES);
  288.         include ("footer.php");
  289.         exit;
  290.     }
  291. }
Advertisement
Add Comment
Please, Sign In to add comment