Search code examples
phphtmlshowsubject

PHP subject line generated from Option tag in html file, won't show in email


I'm creating an application form which gets certain fields, such as there person's first name, last name, email, department, etc. and submits that information into email form. And everything works perfectly, but I want the department that the user selects to show in the subject line of the email. I made that portion into an option, instead of having the user type it in and use $_POST in my PHP email file. I'm sure it is minor but I am not sure on how it should be done. Are there any suggestions? Thanks in advance

----------------------------Here is the HTML Code-------------------------

    <body style="padding:3px; margin:0px;" bgcolor="#FFFFFF"> 
    <center>
    <table cellpadding="0" cellspacing="0" border="0" width="440"> 

        <tr><td style="height:10px"></td></tr> 
        <tr> 
          <td colspan="2" style="text-align:justify; line-height:15px;" class="body"> 

          <form name="frm" method="POST" action="php file" enctype="multipart/form-data"> 
          <center>
          <table cellpadding="0" cellspacing="0" border="0" width="100%"> 
              .........
              .........
              .........
            <tr><td style="height:3px"></td></tr>
            <tr>
            <td width = "23%" class="body">Departments</td>
            <td width = "3%" class ="body">:</td>
            <td width="74%">
                <select>

     <option value="departments" name = "departments">Engineering</option>
     <option value="departments" name = "departments">Marketing</option>
     <option value="departments" name = "departments">PR/Social</option>
     <option value="departments" name = "departments">Media</option>
                </select>
            </td>
            </tr>

-------------------PHP--------------------------

       <?php

if(!empty($_FILES['resume_name']['name'])){

    $file_name=$_FILES['resume_name']['name'];
    $temp_name=$_FILES['resume_name']['tmp_name'];
    $file_type=$_FILES['resume_name']['type'];
    $file_size=$_FILES['resume_name']['size'];

    $base=basename($file_name);
    $extension= pathinfo($base, PATHINFO_EXTENSION);

    $allowed_ext=array("doc","docx","pdf","zip","jpeg","jpg","txt");

    if (in_array($extension,$allowed_ext)){
    $from=$_POST['email'];
    $fname=$_POST['first_name'];


    $lname=$_POST['last_name'];
        $to="[email protected]";
        $subject= $departments;
        $credentials=$_POST['qualifications'];

        $message= 

        '           The person that contacted you is: '.$fname.' '.$lname.'
                E-mail: '.$from.' 
                Subject: '$subject'
                Qualifications: '.$credentials.'

        |---------END MESSAGE----------|';


        $file=$temp_name;
        $content = chunk_split(base64_encode(file_get_contents($file)));
        //$content = chunk_split(base64_encode(file_get_contents($content)));
        $uid=md5(uniqid(time()));


        $header =  "From:   ". $from. "\r\n";
        $header .= "Reply-To:   ". $replyto. "\r\n";
        $header .= "MIME-Version: 1.0\r\n";

        $header .="Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";


        $header .= "--".$uid."\r\n";
        $header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
        $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
        $header .= $message."\r\n\r\n";
        $header .= "--".$uid."\r\n";

        $header .= "--".$uid."\r\n";
        $header .= "Content-type:   ".$file_type."; name=\"".$file_name."\"\r\n";
        $header .= "Content-Transfer-Encoding: base64\r\n";
        $header .= "Content-Disposition: resume_name; file_name=\"".$file_name."\"";
        $header .= $content."\r\n\r\n";

        if(@mail($to, $subject, $message, $header)){
                echo "Success";
                }
                exit();
    ?>

Solution

  • You should set up value for your options, like this:

    <option value="Engineering" name = "departments">Engineering</option>
    <option value="Marketing" name = "departments">Marketing</option>
    ...
    

    Than in your PHP put this:

    $subject = $_POST['departments'];
    

    This isnt best solution but it will work.

    Better would be to define somewhere in PHP file (config) array with available departments $departments = array(1 => 'department1', 2 => 'department2'), in html template make foreach loop above array of departments set value to key in array and in email script use $deparments[intval($_POST['departments'])]