-1

i have written a php code which saves the data into database and also sends email to the client from user attaching the file like resume, below is the code which to save the data into database and also to email the form fields to client now can anyone help me to attach the file while sending the mail.

<?php
include_once "dbconnection.php";
if(isset($_FILES['file']['name'])){
$ext = end(explode('.', $_FILES['file']['name']));
$ext;
$target = "careers";
$il = $_FILES['file']['name'];
$target = $target . $_FILES['file']['name']; 

if(move_uploaded_file($_FILES['file']['tmp_name'], $target)) 
{
} 
else
{
 }
}
if(isset($_POST["role"])){
  $role=$_POST["role"];} else {$role="";}  

if(isset($_POST["fname"])){
$fname=$_POST["fname"];} else {$fname="";}

if(isset($_POST["lname"])){
$lname=$_POST["lname"];} else {$lname="";}

if(isset($_POST["city"])){
    $city=$_POST["city"];} else {$city="";}

if(isset($_POST["email"])){
    $email=$_POST["email"];} else {$email="";}

if(isset($_POST["cntctno"])){
    $cntctno=$_POST["cntctno"];} else {$cntctno="";}

if(isset($_POST["basicqualific"])){
    $basicqualific=$_POST["basicqualific"];} else {$basicqualific="";}


if(isset($_POST["postqualific"])){
    $postqualific=$_POST["postqualific"];} else {$postqualific="";}

if(isset($_POST["resumeheadline"])){
     $resumeheadline=$_POST["resumeheadline"];} else {$resumeheadline="";}

if(isset($_POST["expyears"])){
     $expyears=$_POST["expyears"];} else {$expyears="";}

if(isset($_POST["expmonths"])){
     $expmonths=$_POST["expmonths"];} else {$expmonths="";}

if(isset($_POST["currsalary"])){
     $currsalary=$_POST["currsalary"];} else {$currsalary="";}

if(isset($_POST["expsalary"])){
     $expsalary=$_POST["expsalary"];} else {$expsalary="";}

if(isset($_POST["curremploy"])){
     $curremploy=$_POST["curremploy"];} else {$curremploy="";}

if(isset($_POST["jobtitle"])){
     $jobtitle=$_POST["jobtitle"];} else {$jobtitle="";}

if(isset($_POST["preflocation"])){
     $preflocation=$_POST["preflocation"];} else {$preflocation="";}

if(isset($_FILES["file"])){
     $file=$target;} else {echo "not set";}
$sql="INSERT INTO  careers (role, fname, lname, city, email, cntctno, basicqualific, postqualific, resumeheadline, expyears, expmonths, currsalary, expsalary, curremploy, jobtitle, preflocation, image)
    VALUES ('$role', '$fname', '$lname', '$city', '$email', '$cntctno', '$basicqualific', '$postqualific', '$resumeheadline', '$expyears', '$expmonths', '$currsalary', '$expsalary', '$curremploy', '$jobtitle', '$preflocation', '$il')";


 $to = "xxxxxr@gmail.com";

 $subject = "Contact mail through website from ".$fname." ".$lname;

 $from = "website@xxxx.in";

 $message =
 "
 Role: ".$role.
 "
 Name: ".$fname." ".$lname.
 "
 Email: ". $email.
 "
 Phone: ".$cntctno.
 "
 City: ".$city.
 "
 Service: ".$service.
 "
 Basic Qualification: ".$basicqualific.
 "
 Post Qualification: ".$postqualific.
 "
 Resume Headline: ".$resumeheadline.
 "
 Experience in years: ".$expyears.
 "
 Experience in months: ".$expmonths.
 "
 Current Salary:".$currsalary.
 "
 Expected Salary: ".$expsalary.
 "
 Current Employer:".$curremploy.
 "
 Job Title: ".$jobtitle.
 "

 Preffered Location: ".$preflocation;

 $headers = "From:" ."xxxxx - " . $from;

 mail($to,$subject,$message,$headers);



 if (!mysqli_query($con, $sql))

 {

 echo " Sorry for the inconvenience, please insert again. Error: ".mysqli_error($con);

 } else {

   echo "Thank you for showing your interest in us. A member of our team will contact you shortly. ";

 }
 ?>`
niyas
  • 1
  • 1
  • You've tagged phpmailer but are you actually using it? I don't think this is the proper usage. https://github.com/Synchro/PHPMailer – dcclassics Jun 05 '14 at 14:52
  • You are also BEGGING for an injection attack by accessing the $_POST variable and not filtering/sanitizing it before you insert directly to your target database. see: http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php?rq=1 – VikingBlooded Jun 05 '14 at 14:59

1 Answers1

0

I recommend popular PHPMailer class:

$mailer = new PHPMailer();
$mailer->From = "john@doe.com";
$mailer->FromName = "John Doe";
$mailer->AddReplyTo("john@doe.com", "John Doe");
$mailer->AddAddress("recipient@gmail.com");
$mailer->AddAttachment($path_to_file, $filename_visible_to_recipient);        
$mailer->Body = "Hello world";     

if($mailer->Send()) {
    // success
}else{
    // something wrong happened
    echo $mailer->ErrorInfo;
}
Peter
  • 16,453
  • 8
  • 51
  • 77