2015-07-11 183 views
0

我真的需要你的幫助。請在下面找到我的代碼:發送郵件附件通過PHP PEAR

if (isset($_FILES['file']) and $_FILES['file'] <> '') { // Jika email disertai attachment 
     $crlf = "\n"; 

     $headers['MIME-Version'] = '1.0'; 
     $headers['Content-Type'] = 'text/html; charset="UTF-8'; 
     $headers['Content-Transfer-Encoding'] = 'base64\r\n'; 
     $headers['Date'] = date('r', $_SERVER['REQUEST_TIME']); 
     $headers['Message-ID'] = $_SERVER['REQUEST_TIME'] . md5($_SERVER['REQUEST_TIME']) . '@' . $_SERVER['SERVER_NAME']; 
     $headers['From'] = "[email protected]"; 
     $headers['To']  = $to; 
     $headers['Subject'] = $subject; 

     $mime = new Mail_mime(array('eol' => $crlf)); 

     $mime->setHTMLBody($body); 
     foreach($_FILES['file']['tmp_name'] as $key => $value){ 
      $file_name = $_FILES['file']['name'][$key]; //nama file (tanpa path) 
      $tmp_name = $_FILES['file']['tmp_name'][$key]; //nama local temp file di server 
      $file_type = $_FILES['file']['type'][$key]; //tipe filenya (langsung detect MIMEnya) 

      $fp  = fopen($tmp_name, 'r'); 
      $content = fread($fp, filesize($tmp_name)); 
      $content = addslashes($content); 
      fclose($fp); 
      $data = chunk_split(base64_encode($content)); 

      //$mime->addAttachment($tmp_name, $file_type); 
      $mime->addAttachment($data, $file_type); 
     } 
      $body = $mime->get(); 
      $headers = $mime->headers($headers); 

我很成功發送我的HTML文本郵件。但是,沒有我上傳的附件。我的代碼出現了什麼問題?

謝謝。

+0

我已經與mime- $> addAttachment($ tmp_name的值,$嘗試file_type)並且所有附件都已成功發送。但是,文件名不會顯示爲真正的文件名。例如:php505.tmp。 –

回答

0

我在這裏看不到您的郵件功能?嘗試是這樣的:

上傳附件:

$max_allowed_file_size = 1250; // size in KB 
$allowed_extensions = array("jpg", "jpeg", "gif", "bmp", "png"); 
$upload_folder = './uploads/'; //<-- this folder must be writeable by the script 
$path_of_uploaded_file = $upload_folder . $name_of_uploaded_file; 

$tmp_path = $_FILES["uploaded_file"]["tmp_name"]; 
//Get the uploaded file information 
$name_of_uploaded_file = basename($_FILES['uploaded_file']['name']); 

//get the file extension of the file 
$type_of_uploaded_file = substr($name_of_uploaded_file, 
         strrpos($name_of_uploaded_file, '.') + 1); 

$size_of_uploaded_file = $_FILES["uploaded_file"]["size"]/2048; 

附件,標題的郵件,等

$message = new Mail_mime(); 
    $message->setHTMLBody($text); 
    $message->addAttachment($path_of_uploaded_file); 
    $body = $message->get(); 
    $extraheaders = array("From"=>$from, "Subject"=>$subject,"Reply-To"=>$visitor_email); 
    $headers = $message->headers($extraheaders); 
    $mail = Mail::factory("mail"); 
    $mail->send($to, $headers, $body); 
相關問題