1

I want to develop an apply online for a job feature. I have written the following code to upload a resume, but if the user clicks "apply" he will want to send uploaded resume as an email attachment. I am using phpmailer and have sucessfully gotten to send email, but it doesn't have the file attached.

Here is my code to upload a file:

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");

}

And this is PHPmailer code:

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

I have created "uploads" folder and I am getting all uploaded files therein. But I'm not getting the file uploaded as an email attachment.

Nicolás Ozimica
  • 8,562
  • 5
  • 34
  • 49
user695575
  • 117
  • 2
  • 6
  • The issue could be with the mailer code, which we can't see here. At a minimum, try adding a line before `$mail->AddAttachement` like `echo "File $fileName exists: ".(file_exists("uploads/".$fileName) ? "yes" : "no");` – Oliver Emberton May 06 '11 at 19:55
  • 1
    Try looking on how to post a code please. 4 spaces up front and add some whitespace/indents and don't add all stuff on one line, really hard to read. `tip:` the code for PHPMailer is correct, I've got it working. Try using an absolute path for the attachment instead of a relative one. – Joshua - Pendo May 06 '11 at 19:59
  • Oliver, I added echo line as you said, got "File exists: yesCould not access file: uploads/ " message. I got email but doesnt have file attachment. – user695575 May 06 '11 at 20:03
  • PENDO, I used absolute path, but still not working. – user695575 May 06 '11 at 20:05
  • Permissions for the folder are right? (Read rights should be there aswell, not just the write rights) or perhaps an htaccess file that disallows getting the file? – Joshua - Pendo May 06 '11 at 20:18
  • I checked the file permission. But still not getting it. – user695575 May 06 '11 at 21:30
  • Just checking I read your code correctly - are you storing two copies of the uploaded file? (One within the SQL database and another within the "uploads" folder?) Also, are you aware that if two (or more) people upload files, with each file being called "resume.pdf" you are going to have a clash, as there will be two attempts to write the uploaded files to "uploads/resume.pdf"... – Luke Stevenson May 07 '11 at 09:51

2 Answers2

0

What is your PHP version? I found something in class.phpmailer.php class. Search this function: private function EncodeFile($path, $encoding = 'base64')

Here it is:

  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 '';
}

}

As you can see this function is checking your PHP_VERSION. If it is under 6 (PhpVersion 6 will be DELETED magic_codes functions!!!) and try to using some magic_quotes function witch are deprecated in Php5 or upper.

So...you can resolve this problem to complete the PhpMailer source code with *function_exists* with checks that that *magic_quotes* function are available or use simple to comment the code...

aBanhidy
  • 245
  • 1
  • 3
  • 10
0

Try this answer, it seems there is a problem attaching the body, which somehow corrupts the attachments, I guess Add attachment through PHPMailer

Community
  • 1
  • 1
Asped
  • 2,993
  • 3
  • 28
  • 51