2011-05-06 117 views
1

我想開發一個在線申請工作功能。我已經寫了下面的代碼來上傳簡歷,但是如果用戶點擊「申請」,他會發送上傳的簡歷作爲電子郵件附件。我使用phpmailer並已成功發送電子郵件,但它沒有附加文件。附件中的電子郵件phpmailer

這裏是我的代碼上傳文件:

if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0) 
{ 

    $fileName = $_FILES['userfile']['name']; 
    $tmpName = $_FILES['userfile']['tmp_name']; 
    $fileSize = $_FILES['userfile']['size']; 
    $fileType = $_FILES['userfile']['type']; 

    $fp  = fopen($tmpName, 'r'); 
    $content = fread($fp, filesize($tmpName)); 
    $content = addslashes($content); 
    fclose($fp); 

    if(!get_magic_quotes_gpc()) 
    { 
     $fileName = addslashes($fileName); 
    } 
    $connect=mysql_connect("localhost","root","") or die("Couldnt connect"); 
    mysql_select_db("dbname") or die("Couldnt DB"); 

    $query = "INSERT INTO upload (name, size, type, content) ". 
     "VALUES ('$fileName', '$fileSize', '$fileType', '$content')"; 
     mysql_query($query) or die('Error, query failed'); 

    echo "<br>File $fileName uploaded<br>"; 

    $uploads_dir = 'uploads/'; 
    move_uploaded_file($tmpName, "$uploads_dir/$fileName"); 

} 

這是PHPMailer的代碼:

$mail->AddAttachment("uploads"."/".$fileName); 

我創建「上傳」文件夾,我在其中得到所有上傳的文件。但是我沒有把文件上傳爲電子郵件附件。

+0

這個問題可能與郵件代碼,我們不能在這裏看到。至少,嘗試在'$ mail-> AddAttachement'之前添加一行,如'echo「File $ fileName exists:」。(file_exists(「uploads /".$ fileName)?」yes「:」no「);' – 2011-05-06 19:55:34

+1

請嘗試查看如何發佈代碼。前面4個空格,並添加一些空白/縮進,不要將所有內容添加到一行,真的很難閱讀。 '提示:'PHPMailer的代碼是正確的,我有它的工作。嘗試使用附件的絕對路徑而不是相對路徑。 – 2011-05-06 19:59:13

+0

奧利弗,我加了回聲線,如你所說,得到「文件存在:是不能訪問文件:上傳/」消息。我收到了電子郵件,但沒有附件。 – user695575 2011-05-06 20:03:51

回答

0

什麼是您的PHP版本?我在class.phpmailer.php類中找到了一些東西。 搜索這個功能:私有函數EncodeFile($路徑,$編碼= '的base64')

這就是:

private function EncodeFile($path, $encoding = 'base64') { 
try { 
    if (!is_readable($path)) { 
    throw new phpmailerException($this->Lang('file_open') . $path, self::STOP_CONTINUE); 
    } 
    if (function_exists('get_magic_quotes')) { 
    function get_magic_quotes() { 
     return false; 
    } 
    } 
    if (PHP_VERSION < 6) { 
    $magic_quotes = get_magic_quotes_runtime(); 
    set_magic_quotes_runtime(0); 
    } 
    $file_buffer = file_get_contents($path); 
    $file_buffer = $this->EncodeString($file_buffer, $encoding); 
    if (PHP_VERSION < 6) { set_magic_quotes_runtime($magic_quotes); } 

    return $file_buffer; 
} catch (Exception $e) { 
    $this->SetError($e->getMessage()); 
    return ''; 
} 

}

正如你可以看到這個功能是檢查你PHP_VERSION。如果它低於6(PhpVersion 6將被刪除magic_codes功能!!!),並嘗試使用一些magic_quotes函數女巫是棄用在Php5或上限。

所以......你能解決這個問題,檢查與* function_exists完成PHPMailer的源代碼* 這是* * magic_quotes的功能是可用,或使用簡單的註釋代碼...