attached file in email phpmailer
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 PH开发者_StackOverflow中文版Pmailer 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.
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...
Try this answer, it seems there is a problem attaching the body, which somehow corrupts the attachments, I guess Add attachment through PHPMailer
精彩评论