2012-08-15 97 views
0

我(最終)完成了一個php腳本,用戶可以上傳文件併發送給我,但我似乎無法使用他/她已存儲的FROM字段的電子郵件帳戶。電子郵件永遠不會發送給我...帶附件的PHP電子郵件(存儲的用戶名)

這是我的。我在登錄/註冊時將用戶的電子郵件存儲爲會話。

session_save_path('/home/users/web/...'); 
session_start(); 
$_SESSION['users'] = $email; 

然後,我有一個用戶頁面,這使人想起了會議,並顯示爲「身份登錄{$ _SESSION [‘用戶’]}」

然後,我創建了一個形式爲用戶信息上傳文件,並給我發電子郵件:「‘$ _ SESSION [‘用戶’]。’」

<form method="POST" action="testmail.php" enctype="multipart/form-data" class="form-vertical"> 
<input name="email" type="hidden" value="'.$_SESSION['users'].'" /> 

<span class="label label-info">Document Type</span><br> 
<input type="text" name="project" class="input-medium"style="width: 350px; height: 30px;" /><br><br> 

<span class="label label-info">Upload the Document</span><br><br> 
<input type="file" name="attachment[]"><br><br> 

<span class="label label-info">Brief Description of Document and Concern</span><br> 
<textarea name="description" style="width: 550px; height: 200px;">...</textarea> 

<input type="submit" class="btn btn-large btn-success"> 
</form> 

然而,當我忽略的價值這種形式只適用(值=‘TEST’,對例)。

這裏的電子郵件PHP腳本:

<?php 
    if($_POST || $_FILES) 
    { 
      // email fields: to, from, subject, and so on 
      // Here 
      $from = $_POST['email']; 
      $to = "[email protected]"; 
      $subject = $_POST['project']; 
      $message = "This is the message body and to it I will append the attachments."; 
      $headers = "From: $from"; 

      // boundary 
      $semi_rand = md5(time()); 
      $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 

      // headers for attachment 
      $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; 

      // multipart boundary 
      $message = "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n"."Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; 
      fixFilesArray($_FILES['attachment']); 
      foreach ($_FILES['attachment'] as $position => $file) 
      { 
        // should output array with indices name, type, tmp_name, error, size 
        $message .= "--{$mime_boundary}\n"; 
        $fp  = @fopen($file['tmp_name'],"rb"); 
        $data = @fread($fp,filesize($file['tmp_name'])); 
        @fclose($fp); 
       $data = chunk_split(base64_encode($data)); 
       $message .= "Content-Type: application/octet-stream; name=\"".$file['name']."\"\n"."Content-Description: ".$file['name']."\n" ."Content-Disposition: attachment;\n" . " filename=\"".$file['name']."\";size=".$file['size'].";\n"."Content-Transfer-Encoding: base64\n\n" . $data . "\n\n"; 
      } 
      $message .= "--{$mime_boundary}--"; 
      $returnpath = "-f" . $from; 
      $ok = @mail($to, $subject, $message, $headers, $returnpath); 
      if($ok){ return 1; } else { return 0; } 
    } 
    //This function will correct file array from $_FILES[[file][position]] to $_FILES[[position][file]] .. Very important 

    function fixFilesArray(&$files) 
    { 
      $names = array('name' => 1, 'type' => 1, 'tmp_name' => 1, 'error' => 1, 'size' => 1); 

      foreach ($files as $key => $part) { 
        // only deal with valid keys and multiple files 
        $key = (string) $key; 
        if (isset($names[$key]) && is_array($part)) { 
          foreach ($part as $position => $value) { 
            $files[$position][$key] = $value; 
          } 
          // remove old key reference 
          unset($files[$key]); 
        } 
      } 
    } 
    ?> 
+0

你缺少的PHP標籤 – Amyth 2012-08-15 05:26:35

回答

0

試試這個:

<input name="email" type="hidden" value="<?php echo $_SESSION['users'] ?>" /> 

在你的代碼缺少<?php ?>標籤而獲得的$_SESSION['users']值。 HTML不理解這個,所以你需要把它放在<?php ?>標籤和echo的值中。

+0

這工作就像一個魅力。非常感謝! – user1473497 2012-08-15 13:51:16

+0

不客氣:) – Amyth 2012-08-15 16:43:03

0

嘗試改變下面一行。

<input name="email" type="hidden" value="<?php echo $_SESSION['users'];?>" /> 

您必須設置電子郵件的值。

0

我認爲你應該做<input name="email" type="hidden" value="<?php echo $_SESSION['users']; ?>" />