2013-03-12 51 views
0

我正在嘗試處理電子郵件以響應提交。該電子郵件應抓取先前上傳的文件並將其作爲附件加入。現在有兩個問題發生。PHP郵件()附件錯誤和損壞的文件

  1. 當進程運行發送消息時,我在頁面上收到錯誤消息。的錯誤是: 警告:文件大小()[function.filesize]:STAT在/home/avantjob/public_html/portal/profilemenu.php失敗./example.pdf上管線186

警告:的fopen( ./example.pdf)[function.fopen]:無法打開流:187上的/home/avantjob/public_html/portal/profilemenu.php中沒有這樣的文件或目錄

警告:fread()期望參數1是布爾在/ home/avantjob/public_html/portal/profofilemenu.php在線188

警告:fclose()期望參數1是資源,/ home/avantjob/public_html/porta中給出的布爾值l/profilemenu.php 189行

儘管接收到這些消息,電子郵件正在發送,並且附件被包括在內,無論文件被損壞/截斷並且不會打開。

的代碼如下:

//.Class for processing mail 
class AttachmentEmail { 
private $from = '[email protected]'; 
private $from_name = 'AVANT Portal'; 
private $reply_to = '[email protected]'; 
private $to = ''; 
private $subject = ''; 
private $message = ''; 
private $attachment = ''; 
private $attachment_filename = ''; 

public function __construct($to, $subject, $message, $attachment = '', $attachment_filename = '') 
    { 
    $this -> to = $to; 
    $this -> subject = $subject; 
    $this -> message = $message; 
    $this -> attachment = $attachment; 
    $this -> attachment_filename = $attachment_filename; 
    } 

public function getMimeType($file) 
{ 
// MIME types array 
require_once('mimetypes.php'); 
$extension = end(explode('.', $file)); 
return $mimeTypes[$extension]; // return the array value 
} 

public function mail() { 
    if (!empty($this -> attachment)) { 
     $filename = empty($this -> attachment_filename) ? basename($this -> attachment) : $this -> attachment_filename ; 
     $path = dirname($this -> attachment); 
     $mailto = $this -> to; 
     $from_mail = $this -> from; 
     $from_name = $this -> from_name; 
     $replyto = $this -> reply_to; 
     $subject = $this -> subject; 
     $message = $this -> message; 

     $file = $path.'/'.$filename; 
     $mime_type = $this->getMimeType($file); 
     $file_size = filesize($file); 
     $handle = fopen($file, "r"); 
     $content = fread($handle, $file_size); 
     fclose($handle); 
     $content = chunk_split(base64_encode($content)); 
     $uid = md5(uniqid(time())); 
     $name = basename($file); 
     $header = "From: ".$from_name." <".$from_mail.">\r\n"; 
     $header .= "Reply-To: ".$replyto."\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-type:text/plain; charset=iso-8859-1\r\n"; 
     $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n"; 
     $header .= $message."\r\n\r\n"; 
     $header .= "--".$uid."\r\n"; 
     $header .= "Content-Type: ".mime_type."; name=\"".$filename."\"\r\n"; 
     $header .= "Content-Transfer-Encoding: base64\r\n"; 
     $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n"; 
     $header .= $content."\r\n\r\n"; 
     $header .= "--".$uid."--"; 

     if (mail($mailto, $subject, "", $header)) { 
      return true; 
     } else { 
      return false; 
     } 
    } else { 
     $header = "From: ".($this -> from_name)." <".($this -> from).">\r\n"; 
     $header .= "Reply-To: ".($this -> reply_to)."\r\n"; 

     if (mail($this -> to, $this -> subject, $this -> message, $header)) { 
      return true; 
     } else { 
      return false; 
     } 

    } 
} 
} 

// Display links to each profile sub-program 

if (isset($_POST['submit'])) 
    { 
    $queryname = "SELECT FNAME, LNAME, MNAME, JOBWANT, PAYWANT, PAYCODE, RESUMEFILE FROM APP WHERE ID_NUM = '" . $_SESSION['IDNUM'] . "'"; 
    $namedata = mysqli_query($dbc, $queryname); 
    $namerow = mysqli_fetch_array($namedata); 
    $fname = $namerow['FNAME']; 
    $mname = $namerow['MNAME']; 
    $lname = $namerow['LNAME']; 
    $jobwant = $namerow['JOBWANT']; 
    $paywant = $namerow['PAYWANT']; 
    $paycode = $namerow['PAYCODE']; 
    $resumefile = $namerow['RESUMEFILE']; 
    $msg = "This message was sent in response to a completed application on the AVANT Portal. \n". 
     " \n". 
     "The application information is listed below. \n". 
     " \n". 
     "Name: $fname $mname $lname \n". 
     "Desired Job: $jobwant \n". 
     "Pay Rate: $paywant per $paycode \n". 
     "Please review this candidate as soon as possible. \n". 
     " \n". 
     "The resume can be viewed at www.avant.jobs/portal/uploads/" . $resumefile . "\n". 
     " \n"; 
    $subject = "AVANT Portal Application - " . $fname . " " . $lname; 
    $sendit = new AttachmentEmail('[email protected]', $subject, $msg, $resumefile); 
    $sendit -> mail(); 

,你可以提供任何幫助將不勝感激。謝謝。

+1

您確定您的文件系統根目錄中有'/ uploads'目錄嗎? – jeroen 2013-03-12 16:57:17

+0

是的。該文件作爲應用程序進程的一部分進行上傳,並且將該筆記發送給內部用戶並附上上傳的文件,以便快速查看並通知已完成的應用程序。該文件存在,並且可以通過電子郵件中列出的鏈接進行訪問,但附件版本已損壞。 – KDG 2013-03-12 16:58:21

+3

如果文件可以通過鏈接訪問,我猜'uploads文件夾在你的'/ home/avantjob/public_html /'文件夾中,而不是操作系統的根文件夾。 – jeroen 2013-03-12 17:00:21

回答

1

您有一個錯誤 - 文件不存在。檢查錯誤消息中的路徑(包含統計失敗的路徑)並將其替換爲正確的路徑,最好修復錯誤處理以檢查文件是否存在,如果不存在則返回錯誤。

+0

該文章的文件名已更改。名稱已更正。 – KDG 2013-03-12 17:06:44

+0

@KDG無論如何,這是同樣的問題。錯誤消息告訴您正在嘗試使用的文件不存在於您希望它存在的位置。 – eis 2013-03-12 17:07:26

+0

我知道文件存在於目錄中。有沒有不同的方式指向fopen中的位置? – KDG 2013-03-12 17:12:15