My resume is already present in resume folder with following formate

eg: 3chaitanya.doc = > [session_id+session_firstname]

i need to attach it to email without browsing it

<?php
session_start();
include('db.php');
$to = "company@gmail.com";
$fromEmail = $_SESSION['user_email'];
$fromName = "Job tile"; 
$subject = "Applying for job";
$message = "some text";

//path of my resume is
$path ="C:\wamp\www\jobportal-deb\resume\S_SESSION['user_id'].S_SESSION['first_name']";


/* GET File Variables */ 
$tmpName = $_FILES['attachment']['tmp_name']; 
$fileType = $_FILES['attachment']['type']; 
$fileName = $_FILES['attachment']['name']; 

echo $tmpName.'<br/>';
echo $fileType.'<br/>';
echo $fileName.'<br/>';

/* Start of headers */ 
$headers = "From: $fromName"; 

if (file($tmpName)) { 
  /* Reading file ('rb' = read binary)  */
  $file = fopen($tmpName,'rb'); 
  $data = fread($file,filesize($tmpName)); 
  fclose($file); 

  /* a boundary string */
  $randomVal = md5(time()); 
  $mimeBoundary = "==Multipart_Boundary_x{$randomVal}x"; 

  /* Header for File Attachment */
  $headers .= "\nMIME-Version: 1.0\n"; 
  $headers .= "Content-Type: multipart/mixed;\n" ;
  $headers .= " boundary=\"{$mimeBoundary}\""; 

  /* Multipart Boundary above message */
  $message = "This is a multi-part message in MIME format.\n\n" . 
  "--{$mimeBoundary}\n" . 
  "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . 
  "Content-Transfer-Encoding: 7bit\n\n" . 
  $message . "\n\n"; 

  /* Encoding file data */
  $data = chunk_split(base64_encode($data)); 

  /* Adding attchment-file to message*/
  $message .= "--{$mimeBoundary}\n" . 
  "Content-Type: {$fileType};\n" . 
  " name=\"{$fileName}\"\n" . 
  "Content-Transfer-Encoding: base64\n\n" . 
  $data . "\n\n" . 
  "--{$mimeBoundary}--\n"; 
} 

$flgchk = mail ("$to", "$subject", "$message", "$headers"); 

if($flgchk){
  echo "A email has been sent to: $to";
 }
else{
  echo "Error in Email sending";
}
?>

Dani AI

Generated

Nice that posted a working solution and thanks to for the pointer. The approach of attaching a stored file is fine, but a few practical correctness, reliability and security fixes will make it robust across servers and mail clients.

First, check the file correctly and use a safe display name. Using file() as a truthy check is fragile; prefer realpath() plus is_readable() and ensure the resolved path sits inside the resume folder (prevents path traversal). Also use basename() for the attachment filename and include a Content-Disposition: attachment; filename="..." header so mail clients show the right name. Detect the real MIME type (not just application/msword) with finfo when available.

$dir  = realpath(__DIR__ . '/resume');
$path = realpath($dir . DIRECTORY_SEPARATOR . $resumename);
if (!$path || strpos($path, $dir) !== 0 || !is_readable($path)) {
    // file missing or invalid path
}
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime  = finfo_file($finfo, $path);
finfo_close($finfo);
$filename = basename($path);

Second, delivery and config: on a local WAMP box PHP's mail() often needs SMTP configured in php.ini (or an MTA installed). mail() returning true only means PHP handed the message to the system MTA; it doesn't guarantee final delivery. For easier, more reliable SMTP+attachment handling (authenticated SMTP, proper headers, debug output) prefer a proven library (PHPMailer/SwiftMailer) in production.

Finally, harden storage and access: store resumes outside the webroot or prevent direct listing, validate the DB-stored filename against the logged user, check file permissions, log failures, and don't expose raw paths. Those small changes will fix edge cases, improve compatibility with different mail clients, and reduce security risk.

Recommended Answers

All 3 Replies

i have resume which is saved in resume folder ... i need to attach it mail on applying job. with out browse option

Solved
<?php
session_start();
include 'db.php';
include 'user_header.php';
include('function.php');
check_session($con);

$sql = mysqli_query($con,"select user_id,resume from user_personal_details where
user_id = '".$_SESSION['user_id']."'");

$row = mysqli_fetch_array($sql);

$resumename = $row['resume'];
/***************************************************/
$to = "company@gmail.com";
$fromEmail = $_SESSION['user_email'];
$fromName = "Job title"; 
$subject = "Applying for job";
$message = "some text";

$tmpName = "resume/$resumename"; 
$fileType = "application/msword";
$fileName = $tmpName;

//echo $tmpName.'<br/>';
//echo $fileType.'<br/>';
//echo $fileName.'<br/>';

/* Start of headers */ 
$headers = "From: $fromName"; 

if (file($tmpName)) { 
  /* Reading file ('rb' = read binary)  */
  $file = fopen($tmpName,'rb'); 
  $data = fread($file,filesize($tmpName)); 
  fclose($file); 

  /* a boundary string */
  $randomVal = md5(time()); 
  $mimeBoundary = "==Multipart_Boundary_x{$randomVal}x"; 

  /* Header for File Attachment */
  $headers .= "\nMIME-Version: 1.0\n"; 
  $headers .= "Content-Type: multipart/mixed;\n" ;
  $headers .= " boundary=\"{$mimeBoundary}\""; 

  /* Multipart Boundary above message */
  $message = "This is a multi-part message in MIME format.\n\n" . 
  "--{$mimeBoundary}\n" . 
  "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . 
  "Content-Transfer-Encoding: 7bit\n\n" . 
  $message . "\n\n"; 

  /* Encoding file data */
  $data = chunk_split(base64_encode($data)); 

  /* Adding attchment-file to message*/
  $message .= "--{$mimeBoundary}\n" . 
  "Content-Type: {$fileType};\n" . 
  " name=\"{$fileName}\"\n" . 
  "Content-Transfer-Encoding: base64\n\n" . 
  $data . "\n\n" . 
  "--{$mimeBoundary}--\n"; 
} 

$flgchk = mail ("$to", "$subject", "$message", "$headers"); 

if($flgchk){
  echo "A email has been sent to: $to";
 }
else{
  echo "Error in Email sending";
}
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.