1

i simply make a form to submit job application. Everything working just fine but i want a little bit improvement where uploaded files also forwarded to my email. i want to make it easier for me to see the attachment files without copy-paste link from my email. Also i wonder if somehow i can make the subject email is 'name' ?

<?php
$mail = $_POST['email'];

$to = "yadayadayada";
$subject = "--";
$headers = "Job application <noreply@example.com>";

$message  = "Personal Info\n";
$message .= "\nFirst and Last Name: " . $_POST['name'];
$message .= "\nEmail: " . $_POST['email'];
$message .= "\nDOB: " . $_POST['dob'];
$message .= "\nTelephone: " . $_POST['phone'];
$message .= "\nGender: " . $_POST['gender'];


/* FILE UPLOAD */
if(isset($_FILES['fileupload'])){
$errors= array();
$file_name = $_FILES['fileupload']['name'];
$file_size =$_FILES['fileupload']['size'];
$file_tmp =$_FILES['fileupload']['tmp_name'];
$file_type=$_FILES['fileupload']['type'];
$file_ext=strtolower(end(explode('.',$_FILES['fileupload']['name'])));

$expensions= array("pdf","doc","docx", "jpeg", "gif", "png");
                          
$OriginalFilename = $FinalFilename = preg_replace('`[^a-z0-9-_.]`i','',$_FILES['fileupload']['name']); 
$FileCounter = 1; 
while (file_exists( 'upload_files/'.$FinalFilename ))
    $FinalFilename = $FileCounter++.'_'.$OriginalFilename; 

    if(in_array($file_ext,$expensions)=== false){
        $errors[]="Extension not allowed, please choose a .pdf, .doc, .docx, .jpeg, .png, .gif file.";
    }
    
    if($file_size > 15360){
        $errors[]='File size must be max 15000Kb';
    }
    if(empty($errors)==true){
        $message .= "\nResume: example.com/upload_files/".$FinalFilename;
    }else{
        $message .= "\nFile name: no files uploaded";
        }
    };
    /* end FILE UPLOAD */

    $message .= "\n\nWORK AVAILABILITY";
    $message .= "\nAre you available for work: " . $_POST['availability'];

    if (isset($_POST['minimum_salary_full_time']) && $_POST['minimum_salary_full_time'] != "")
        {
            $message .= "\nMinimum salary: " . $_POST['minimum_salary_full_time'];
            $message .= "\nHow soon would you be looking to start? " . $_POST['start_availability_full_time'];
            $message .= "\nAre you willing to work remotely? " . $_POST['remotely_full_time'];
        }
    if (isset($_POST['minimum_salary_part_time']) && $_POST['minimum_salary_part_time'] != "")
        {
            $message .= "\nMinimum salary: " . $_POST['minimum_salary_part_time'];
            $message .= "\nHow soon would you be looking to start? " . $_POST['start_availability_part_time'];
            $message .= "\nWhen you prefer to work? " . $_POST['day_preference_part_time'];
        }
    if (isset($_POST['fixed_rate_contract']) && $_POST['fixed_rate_contract'] != "")
        {
            $message .= "\nMinimum fixed rate: " . $_POST['fixed_rate_contract'];
            $message .= "\nMinimum hourly rate: " . $_POST['hourly_rate_contract'];
            $message .= "\nMinimum hours for a contract: " . $_POST['minimum_hours_conctract'];
            $message .= "\nAre you willing to work remotely? " . $_POST['remotely_contract'];
        }
                    
    $message .= "\n\nTerms and conditions accepted: " . $_POST['terms'];
                                            
    //Receive Variable
    $sentOk = mail($to,$subject,$message,$headers);
                    
    //Confirmation page
    $user = "$mail";
    $usersubject = "Thank You";
    $userheaders = "From: Job application <noreply@mydomain.com>";
    /*$usermessage = "Thank you for your time. Your application is successfully submitted.\n"; WITHOUT SUMMARY*/
                    
    //Confirmation page WITH  SUMMARY
    $usermessage = "Thank you for your time. Your application is successfully submitted. We will contact you shortly.\n\nBELOW A SUMMARY\n\n$message"; 
    mail($user,$usersubject,$usermessage,$userheaders);
?>

I try to add this it doesnt work

   }
    if(empty($errors)==true){
        move_uploaded_file($file_tmp,"upload_files/".$FinalFilename);
        $mail->addAttachment ("upload_files/".$FinalFilename);
    }else{
        $message .= "\nFile name: no files uploaded";
        }
    };
  • 1
    Have you looked at any of the tried and tested mail libraries, like PHPMailer, SwiftMailer or similar instead of using the low-level `mail()` function? They make it _much_ easier to do these kind of things, and makes it easier to configure smtp settings directly from your code (making the code more portable.) – M. Eriksson Mar 18 '21 at 15:21
  • You'll find a good answer using PHPMailer as well as an approach with php mail only here: https://stackoverflow.com/questions/12301358/send-attachments-with-php-mail – mikesp Mar 18 '21 at 15:24
  • It's hard to see if you handled the upload correctly without the code. For example you should also make sure to set the form enctype attribute to multipart/form-data. Make sure to follow this upload steps: http://www.tutorialspoint.com/php/php_file_uploading.htm – mikesp Mar 18 '21 at 15:35
  • [This would be a good place to start](https://github.com/PHPMailer/PHPMailer/blob/master/examples/send_file_upload.phps). – Synchro Mar 18 '21 at 15:48

0 Answers0