2012-06-12 77 views
0

我想知道我是否可以選擇你的大腦。PHP中的多個郵件附件

我在我的網站上有一個郵件腳本,我可以用它來發送帶有單個附件的郵件。發送單個附件工作得很好。

我希望能夠發送帶有多個附件的電子郵件,並且我遇到了問題。

這是文件選擇框中我有以下形式:

<input type="file" name="attachment[]" id="attachment[]" multiple onfocus="this.style.backgroundColor='#e7e7e7'" onblur="this.style.backgroundColor='#ffffff'"/> 

然後,當文件被選中,這是處理它的PHP:

if (!empty($_FILES['attachment']['name'])) 
    { 
     if(count($_FILES['attachment']['name'])>0) 
     { 
      $count = 0; 
      foreach ($_FILES['attachment']['name'] as $file) 
      { 
       $name_of_file = $_FILES['attachment']['name'][$count]; 
       $file_name = $guid." - ".$name_of_file; 
       $temp_name = $_FILES['attachment']['tmp_name'][$count]; 
       $file_type = $_FILES['attachment']['type'][$count]; 
       $file_size = $_FILES['attachment']['size'][$count]; 
       $count++; 

       if ($file_size > 2048000) 
       { 
        header ("refresh: 5; url=attach.php"); 
        include ("header.php"); 
        echo "File size is to big. Size must be no bigger than 2Mb. Please go <a href='attach.php'>back</a>"; 
        include ("footer.php"); 
        exit; 
       } 
       else 
       { 
        $pics = array(".bmp", ".gif", ".jpg", "jpeg", ".png"); //5 
        $docs = array(".doc", "docx", ".odt", ".pdf", ".ppt", "pptx", ".rtf", ".txt", ".xls", "xlsx"); //10 
        $misc = array(".csv", ".htm", "html", ".php", ".pkt", ".rar", ".sql", ".xpi", ".zip"); //9 

        $base = basename($file_name); 
        $extension = substr($base, strlen($base)-4, strlen($base)); 
        $extension = strtolower($extension); 

        if (in_array($extension,$pics)) 
        { 
         $target = "".FILES."/".FUP_PICS."/"; 
        } 

        if (in_array($extension,$docs)) 
        { 
         $target = "".FILES."/".FUP_DOCS."/"; 
        } 

        if (in_array($extension,$misc)) 
        { 
         $target = "".FILES."/".FUP_MISC."/"; 
        } 

        $target = $target.$base; 

        $allowed_extensions = array(".bmp", ".csv", ".doc", "docx", ".gif", ".htm", "html", 
               ".jpg", ".JPG", "jpeg", "JPEG", ".odt", ".pdf", ".php", ".pkt", ".png", ".ppt", "pptx", 
               ".rtf", ".sql", ".txt", ".xls", "xlsx", ".zip" 
               ); 

        if(in_array($extension,$allowed_extensions)) 
        { 
         $from = mysql_real_escape_string($_POST['from']); 
         $emailto = mysql_real_escape_string($_POST['emailto']); 
         $bcc = mysql_real_escape_string($_POST['bcc']); 
         $subject = htmlspecialchars($_POST['subject']); 
         $message = htmlspecialchars($_POST['message'], ENT_NOQUOTES); 
         $message1 = htmlspecialchars_decode($_POST['message'], ENT_QUOTES); 
         if ($sendhash == 'Y') 
         { 
          $message1 .= "\n\nThe following is your unique message ID: "; 
          $message1 .= $guid; 
          $message1 .= "\n\nAttachment has been scanned for viruses and is virus free."; 
          $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"; 
          $message1 .= "\n\nTo verify the validity of the message, click the link below or copy and paste it into your browser:"; 
          $message1 .= "\n\n "; 
          $message1 .= HTTP_PATH; 
          $message1 .= "verify.php?uid="; 
          $message1 .= urlencode($guid); 
          $message1 .= "\n\nPlease note, verification link expires on "; 
          $message1 .= $expirydate; 
         } 
         else 
         { 
         } 
         $message1 .= "\n\n--\nSent from Chris' Address Book"; 

         foreach ($file as $files) 
         { 
         $files = $temp_name; 
         $content = chunk_split(base64_encode(file_get_contents($file))); 
         $uid = md5(uniqid(time())); 

         $header = "From: ".$from."\r\n"; 
         $header .= "Reply-To: ".$from."\r\n"; 
         $header .= "Bcc: ".$bcc."\r\n"; 
         $header .= "MIME-Version: 1.0\r\n"; 

         $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n"; 
         $header .= "This is a multi-part message in MIME format. \r\n"; 

         $header .= "--".$uid."\r\n"; 
         $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n"; 
         $header .= $message1."\r\n"; 

         $header .= "--".$uid."\r\n"; 
         $header .= "Content-Type: ".$file_type."; name=\"".$file_name."\"\r\n"; 
         $header .= "Content-Transfer-Encoding: base64\r\n"; 
         $header .= "Content-Disposition: attachment; filename=\"".$file_name."\"\r\n\r\n"; 
         $header .= $content."\r\n"; 
         } 

         if ($_POST['emailto'] == '') 
         { 
          $sqlp=mysql_query("SELECT * FROM ".PERSON." JOIN contact on contact.personID = person.adbkid WHERE email1 = '$bcc' OR email2 = '$bcc'"); 
          $num=mysql_numrows($sqlp); 
         } 
         else 
         { 
          $sqlp=mysql_query("SELECT * FROM ".PERSON." JOIN contact on contact.personID = person.adbkid WHERE email1 = '$emailto' OR email2 = '$emailto'"); 
          $num=mysql_numrows($sqlp); 
         } 

         while ($row = mysql_fetch_array($sqlp)) 
         { 
          $adbkid = $row['adbkid']; 
          $id = $row['id']; 
         } 

          if ($num == 0) 
          { 
           $selfid = "EMAILSA"; 
          } 
          else 
          { 
           $selfid = $adbkid; 
          } 

         if (mail($emailto, $subject, "", $header)) 
         { 
          if ($sendhash == 'Y') 
          { 
           $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')"); 
           $lastid=mysql_insert_id(); 
           $sqlr=mysql_query("INSERT INTO ".HASH." (randhash) VALUES ('$guid')"); 
          } 
          else 
          { 
           $sql=mysql_query("INSERT INTO ".EMAILS." (emailfrom, emailto, bcc, subject, message, getthedate, gettime, fileatt, fileext, showinsearch, expireit, showinverify, wasviewed, personID) VALUES ('$from', '$emailto', '$bcc', '$subject', '$message', '$getthedate', '$gettime', '$file_name', '$extension', '$showinsearch', '$expireit', '$siv', '$wv', '$selfid')"); 
           $lastid=mysql_insert_id();       
          } 
          $sqlone = "INSERT INTO ".SENTFILES." (filename, filetype, fileext, filesize, filetempname, dateadded, timeadded, fileguid, sentmailid) VALUES ('$file_name', '$file_type', '$extension', '$file_size', '$temp_name', '$getthedate', '$gettime', '$guid', '$lastid')"; 
          $expire = mysql_query("UPDATE ".EMAILS." SET showinverify = 0 WHERE expireit < CURDATE()"); 
          if (!mysql_query($sqlone,$conn)) 
          { 
           die("Error: " . mysql_error()."."); 
          } 

          header ("refresh: 5; url=$url"); 
          include ("header.php"); 
          $ful = (move_uploaded_file($temp_name, $target)) ? "".$file_name." was uploaded to ".$target."" : "".$file_name.", was not uploaded. Please try a manual upload."; 
          echo "<title>Success sending email</title>"; 
          echo "Your message has been successfully sent. Message details have been added to the database.<p>$ful"; 
          include ("footer.php"); 
         } 
         else 
         { 
          header ("refresh: 5; url=$url"); 
          include ("header.php"); 
          echo "<title>Error sending email</title>"; 
          echo "There seems to be an error sending your email."; 
          include ("footer.php"); 
         } 
         exit; 
        } 
        else 
        { 
         header ("refresh: 5; url=attach.php"); 
         include ("header.php"); 
         echo "File type is not allowed. Please go <a href='attach.php'>back</a>"; 
         include ("footer.php"); 
         exit; 
        } 
       } 
      } 
     } 

我在跑的問題當我嘗試以這種方式發送電子郵件時,出現以下錯誤:

Warning: Invalid argument supplied for foreach() in /home/flattley/public_html/chriswilcox/cab/attach.php on line 150 

150行是這樣的:

foreach ($file as $files) 

我只是不知道我出錯的地方。如果我沒有foreach循環,那麼它不會發送,或者它只會發送第一個文件。

我知道我可以使用像phpmailer這樣的東西,但我希望能夠自己做到這一點。我只是無法看到我要出錯的地方。

我已經嘗試了我的本地apache在沒有郵寄它的腳本,它上傳和移動多個文件就好 - 就像它在我的主機上一樣。這是當我添加郵件選項,它都變成梨形。

你可以提供任何幫助將非常感激

+0

'$ files'變量中沒有什麼 –

回答

0

如果你在你的代碼回顧:

foreach ($_FILES['attachment']['name'] as $file) 

$file已經是一個單一的文件名。它不是一個數組。正如我所提到的php documentation

我看到,在您的代碼中,您嘗試獲取上傳文件的內容。要做到這一點,你可以這樣做:

foreach($_FILES['attachment']['tmp_name'] as $eachFile){ 
    $content = chunk_split(base64_encode(file_get_contents($file))); 
    $uid = md5(uniqid(time())); 

    $header = "From: ".$from."\r\n"; 
    $header .= "Reply-To: ".$from."\r\n"; 
    $header .= "Bcc: ".$bcc."\r\n"; 
    $header .= "MIME-Version: 1.0\r\n"; 

    $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n"; 
    $header .= "This is a multi-part message in MIME format. \r\n"; 

    $header .= "--".$uid."\r\n"; 
    $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n"; 
    $header .= $message1."\r\n"; 

    $header .= "--".$uid."\r\n"; 
    $header .= "Content-Type: ".$file_type."; name=\"".$file_name."\"\r\n"; 
    $header .= "Content-Transfer-Encoding: base64\r\n"; 
    $header .= "Content-Disposition: attachment; filename=\"".$file_name."\"\r\n\r\n"; 
    $header .= $content."\r\n"; 
} 
+0

嗨,我將如何在我需要它的上下文中使用它。我假設150行的foreach循環需要來自第一個foreach的信息來處理它。 – Chris

+0

你有太多的嵌套if。你需要對此進行分類。如果一次檢查一個,然後一次處理一個,如:檢查大小,如果可以,則執行另一個,直到滿足所有要求。下一步是,循環你的建立你的迴應。最後,迴應它。你會以更好的有組織的代碼結束。 – ariefbayu

+0

嗨ariefbayu,謝謝你試圖回答這個問題。我試過你的改變,但它說tmp_name是空的。另外,如果不是問題。我已檢查並雙重檢查他們,並不給我任何問題 – Chris