2011-05-06 120 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的功能是可用,或使用简单的注释代码...