2010-07-14 124 views
2

我在服務器上創建了「上傳」文件夾,並且用戶在HTML表單上上傳的文件進入了該文件夾。 接下來,我嘗試從該文件夾中檢索它並將其附加到電子郵件中,但此部分不起作用。 這是我的PHP:使用PHP通過HTML表單發送電子郵件附件

<?php 
$name = $_POST['name']; 
$email = $_POST['email']; 
$form_subject = $_POST['form_subject']; 
$message = $_POST['message']; 


//File upload 

// Where the file is going to be placed 
$target_path = "uploads/"; 

/* Add the original filename to our target path. 
Result is "uploads/filename.extension" */ 
$target_path = $target_path . basename($_FILES['uploadedfile']['name']); 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { 
    echo "The file ". basename($_FILES['uploadedfile']['name']). 
    " has been uploaded"; 
} else{ 
    echo "There was an error uploading the file, please try again!"; 
} 

//End of file upload 


[email protected]$REMOTE_ADDR; 
$date=date("l, F j, Y, g:i a"); 

$to = "[email protected]"; 
$subject = "New data submitted!"; 
$body = "Here is the information submitted to 
www.polycysticliverdisease.com/html/pld_form.php 
from $ip on $date.\n\n 
--------------------------------\n\n 
name: $name \n\n 
email address: $email \n\n 
subject: $form_subject \n\n 
comment: $message"; 
//mail($to, $subject, $body); 

// Obtain file upload vars  
$fileatt  = $_FILES['uploadedfile']['tmp_name'];  
$fileatt_type = $_FILES['uploadedfile']['type'];  
$fileatt_name = $_FILES['uploadedfile']['name']; 

if (is_uploaded_file($fileatt)) {  
// Read the file to be attached ('rb' = read binary)  
$file = fopen($fileatt,'rb');  
$data = fread($file,filesize($fileatt));  
fclose($file); 

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

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

// Add a multipart boundary above the plain message  
$message = "This is a multi-part message in MIME format.\n\n" .  
      "--{$mime_boundary}\n" .  
      "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .  
      "Content-Transfer-Encoding: 7bit\n\n" .  
      $message . "\n\n"; 

// Base64 encode the file data  
$data = chunk_split(base64_encode($data)); 

// Add file attachment to the message  
$message .= "--{$mime_boundary}\n" .  
      "Content-Type: {$fileatt_type};\n" .  
      " name=\"{$fileatt_name}\"\n" .  
      "Content-Disposition: attachment;\n" .  
      " filename=\"{$fileatt_name}\"\n" .  
      "Content-Transfer-Encoding: base64\n\n" .  
      $data . "\n\n" .  
      "--{$mime_boundary}--\n";  
} 

// Send the message  
$ok = @mail($to, $subject, $body, $headers);  
if ($ok) {  
echo "<p>Mail sent! Yay PHP!</p>";  
} else {  
echo "<p>Mail could not be sent. Sorry!</p>";  
} 

?> 

這是我的HTML文件上傳控件:

<input type="submit" name="submit" class="submit" /> 
<input type="file" name="uploadedfile" class="upload" /> 

謝謝!

回答

2

我會使用一些郵件庫 - 也許Swiftmailer。自己編程不值得(如果你沒有爲了學習目的而做它)。

+0

非常感謝,非常感謝! 它工作得很漂亮。我愛Swiftmailer! 但是當我設立了一個機構我的電子郵件,我用文字和變量的組合: $ MESSAGE-> setBody('這裏是提交給 www.polycysticliverdisease.com/html/pld_form.php 信息$ ip on $ date。\ n \ n name:$ name \ n \ n 電子郵件地址:$ email \ n \ n 主題:$ form_subject \ n \ n 評論:$ form_message','text/html' ); 所有變量都被聲明並賦予適當的值。 但他們和'\ n \ n'都作爲文本的一部分出現在已發送的電子郵件中,即它們不被識別爲變量。 爲什麼? – vlevsha 2010-07-16 03:28:36

+0

@vlevsha,因爲您使用的是單引號。使用雙引號。 – Unicron 2010-07-16 07:34:05

+0

謝謝,我甚至在我看到您的答案之前做過。 :) 現在,我有一個更嚴重的問題: http://stackoverflow.com/questions/3290966/retrieving-a-file-name-to-attach-to-an-email-with-swiftmailer- and-php – vlevsha 2010-07-20 17:33:47

0

使用預建的郵件包,如SwiftMailerPHPMailer。兩者都可以輕鬆處理附件。

+0

謝謝!我選擇了SwiftMailer。 只是喜歡它! – vlevsha 2010-07-16 03:30:08