tags:

views:

43

answers:

2

i am making a mail script with attachment. following ar my php script.

            $filename=$_FILES["file1"]["name"]; 

$filetype=$_FILES["file1"]["type"]; 
 $filesize=$_FILES["file1"]["size"]; 
$filetemp=$_FILES["file1"]["tmp_name"]; 
      if($filetype=="application/octet-stream" or $filetype=="text/plain" or $filetype=="application/msword") 
{ 
        $msg='Name :'.$_POST['s_name'].'<br>';
     $msg.='Date Of Birth :'.$_POST['s_dob'].'<br>';
      $msg.='Gendere :'.$_POST['s_gender'].'<br>';
      $msg.='Qualification :'.$_POST['s_quli'].'<br>';
         if($_POST['s_exp']!="") {
       $msg.='Exp. (No of years) :'.$_POST['s_exp'].'<br>';
         }
           if($_POST['ctc']!=""){
              $msg.='Current CTC :'.$_POST['ctc'].'<br>';
           }
               $msg.='Address :'.$_POST['addr1'].'<br>';
               if($_POST['addr2']!=""){
                 $msg.='Alternate address:'.$_POST['addr2'].'<br>';
                   }
               $msg.='City :'.$_POST['s_city'].'<br>';
            $msg.='State :'.$_POST['s_state'].'<br>';
               $msg.='Country :'.$_POST['s_country'].'<br>';
             $msg.='Mobile No.:'.$_POST['s_mobile'].'<br>';
                     if($_POST['s_phone']!=""){
                      $msg.='Phone no.:'.$_POST['s_phone'].'<br>';
                      }
                    if($_POST['s_resume'] !="")
               {
                     $msg.=$_POST['s_resume'].'<br>';
                     }
                           $fp = fopen($file1, "r"); 
                $file = fread($fp, $filesize); 

                         $file = chunk_split(base64_encode($file1)); 
                         $num = md5(time()); 

    //Normal headers 

                $headers  = "From: Aim-4-jobs<Aim-4-jobs>\r\n"; 
                      $headers  .= "MIME-Version: 1.0\r\n"; 
                       $headers  .= "Content-Type: multipart/mixed; "; 
                      $headers  .= "boundary=".$num."\r\n"; 
                       $headers  .= "--$num\r\n"; 

    // This two steps to help avoid spam    

                $headers .= "Message-ID: <".$now." TheSystem@".$_SERVER['SERVER_NAME'].">\r\n"; 
                $headers .= "X-Mailer: PHP v".phpversion()."\r\n";          

    // With message 

                   $headers .= "Content-Type: text/html; charset=iso-8859-1\r\n"; 
                       $headers .= "Content-Transfer-Encoding: 8bit\r\n"; 
                  $headers .= "".$message."\n"; 
                         $headers .= "--".$num."\n";  

               // Attachment headers 
                 $to="[email protected],[email protected]";
                        $headers  .= "Content-Type:".$file1_type." "; 
                  $headers  .= "name=\"".$file1_name."\"r\n"; 
                                  $headers  .= "Content-Transfer-Encoding: base64\r\n"; 
                          $headers  .= "Content-Disposition: attachment; "; 
                         $headers  .= "filename=\"".$file1_name."\"\r\n\n"; 
                        $headers  .= "".$file."\r\n"; 
                       $headers  .= "--".$num."--"; 
                            mail($to,$subject,$msg,$headers);

Html script

                <form name="candidate" method="post" enctype="multipart/form-data" action="thanks.php"><table width="466" height="132" border="0" cellpadding="3" cellspacing="2">
                        <tr>
                          <th colspan="3" align="left" valign="middle" class="spa" scope="col"><p align="left" class="style29"><a name="apply" id="apply"></a>CANDIDATES REGISTRATION</p></th>
                          <th width="43%" align="left" valign="middle" class="spa style30" scope="col"> * <span class="style28">= Required </span></th>
                        </tr>
                        <tr>
                          <td width="34%" align="left" valign="middle" class="style1"><p>Full Name <span class="style30">* </span></p></td>
                          <td width="4%" align="left" valign="middle" class="style1">:</td>
                          <td colspan="2" align="left" valign="middle" class="style1"><input name="s_name" type="text" id="your full name" size="35" maxlength="50" /></td>
                        </tr>
                        <tr>
                          <td align="left" valign="middle" class="style1"><p>Date of Birth <span class="style30">*</span> </p></td>
                          <td align="left" valign="middle" class="style1">:</td>
                          <td colspan="2" align="left" valign="middle" class="style1"><input name="s_dob" type="text" id="your full name9" size="35" maxlength="50" /></td>
                        </tr>
                        <tr>
                          <td align="left" valign="middle" class="style1"><p>Gender <span class="style30">* </span></p></td>
                          <td align="left" valign="middle" class="style1"> :</td>
                          <td colspan="2" align="left" valign="middle" class="style1"><input name="s_gender" type="text" id="your full name10" size="35" maxlength="50" /></td>
                        </tr>
                        <tr>
                          <td align="left" valign="middle" class="style1"><p>Qualification<span class="style30"> *</span> </p></td>
                          <td align="left" valign="middle" class="style1">:</td>
                          <td colspan="2" align="left" valign="middle" class="style1"><input name="s_quli" type="text" id="your full name11" size="35" maxlength="50" /></td>
                        </tr>
                        <tr>
                          <td height="18" align="left" valign="middle" class="style1"><p>Exp. (No of years)</p></td>
                          <td align="left" valign="middle" class="style1">:</td>
                          <td colspan="2" align="left" valign="middle" class="style1"><input name="s_exp" type="text" id="your full name12" size="35" maxlength="50" /></td>
                        </tr>
                        <tr>
                          <td height="18" align="left" valign="middle" class="style1"><p>Current CTC</p></td>
                          <td align="left" valign="middle" class="style1">:</td>
                          <td colspan="2" align="left" valign="middle" class="style1"><input name="ctc" type="text" id="your full name13" size="35" maxlength="50" /></td>
                        </tr>
                        <tr>
                          <td height="18" align="left" valign="middle" class="style1"><p>Address Line 1<span class="style30">*</span></p></td>
                          <td align="left" valign="middle" class="style1">:</td>
                          <td colspan="2" align="left" valign="middle" class="style1"><input name="addr1" type="text" id="your full name15" size="35" maxlength="50" /></td>
                        </tr>
                        <tr>
                          <td height="18" align="left" valign="middle" class="style1"><p>Address Line 2</p></td>
                          <td align="left" valign="middle" class="style1">:</td>
                          <td colspan="2" align="left" valign="middle" class="style1"><input name="addr2" type="text" id="your full name16" size="35" maxlength="50" /></td>
                        </tr>
                        <tr>
                          <td height="18" align="left" valign="middle" class="style1"><p>City <span class="style30">*</span> </p></td>
                          <td align="left" valign="middle" class="style1">:</td>
                          <td colspan="2" align="left" valign="middle" class="style1"><input name="s_city" type="text" id="your full name17" size="35" maxlength="50" /></td>
                        </tr>
                        <tr>
                          <td height="18" align="left" valign="middle" class="style1"><p>State<span class="style30"> * </span></p></td>
                          <td align="left" valign="middle" class="style1">:</td>
                          <td colspan="2" align="left" valign="middle" class="style1"><input name="s_state" type="text" id="your full name18" size="35" maxlength="50" /></td>
                        </tr>
                        <tr>
                          <td height="18" align="left" valign="middle" class="style1"><p>Country <span class="style30">*</span> </p></td>
                          <td align="left" valign="middle" class="style1">:</td>
                          <td colspan="2" align="left" valign="middle" class="style1"><input name="s_country" type="text" id="your full name19" size="35" maxlength="50" /></td>
                        </tr>
                        <tr>
                          <td height="18" align="left" valign="middle" class="style1"><p>Phone</p></td>
                          <td align="left" valign="middle" class="style1">:</td>
                          <td colspan="2" align="left" valign="middle" class="style1"><input name="s_phone" type="text" id="your full name20" size="35" maxlength="50" /></td>
                        </tr>
                        <tr>
                          <td height="18" align="left" valign="middle" class="style1"><p>Mobile<span class="style30"> *</span> </p></td>
                          <td align="left" valign="middle" class="style1">:</td>
                          <td colspan="2" align="left" valign="middle" class="style1"><input name="s_mobile" type="text" id="your full name21" size="35" maxlength="12" /></td>
                        </tr>
                        <tr>
                          <td height="18" align="left" valign="middle" class="style1"><p>Paste Your Resume</p></td>
                          <td align="left" valign="middle" class="style1">:</td>
                          <td colspan="2" align="left" valign="middle" class="style1"><textarea name="s_resume" cols="27" rows="6" id="s_resume"></textarea></td>
                        </tr>
                        <tr>
                          <td height="18" align="left" valign="middle" class="style1"><p>Attach Your Resume <span class="style30">*</span> </p></td>
                          <td align="left" valign="middle" class="style1">:</td>
                          <td colspan="2" align="left" valign="middle" class="style1">
                              <label>
                              <input type="file" name="file1" id="file1" />
                              </label>                              </td>
                        </tr>
                        <tr>
                          <td height="18" align="left" valign="middle" class="style1">&nbsp;</td>
                          <td align="left" valign="middle" class="style1">&nbsp;</td>
                          <td colspan="2" align="left" valign="middle" class="style1">
                              <label>
                              <input type="submit" name="candidates" id="candidates" value="Submit"  onclick="return validation();"/>
                              </label>                              </td>
                        </tr>
                    </table>
                    </form>

but it gives error: Warning: fread(): supplied argument is not a valid stream resource in C:\Program Files\xampp\htdocs\aim4job\thanks.php on line 62

A: 

You wrote $fp = fopen($file1, "r");. $file1 is not set.

How about moving the temporary file from the tmp folder to a tmp folder of your own then read it from there? See http://php.net/manual/en/function.move-uploaded-file.php

move_uploaded_file($filename, 'tmp/'.basename($filename));

// attach and send mail

unlink('tmp/'.basename($filename));
thephpdeveloper
+1  A: 

To fix this one error, you can change

$fp = fopen($file1, "r")

to

$fp = fopen($filetemp, "r");

Constructing a MIME mail like that is just crazy though. I'm sure you will find many more bugs there. Use an existing library, like PHPMailer or Zend_Mail.

Lukáš Lalinský
+1: use an existing library. Really.
Max