Form with file attachment : Whats the php code to allow submit with no attachment?

Soldato
Joined
4 Feb 2004
Posts
13,430
Location
Écosse
I've added a "Contact" form to a webpage.

It has a "File Upload" field which allows the user to browse their PC for a file and once the "Submit" button is hit, emails the "Contact" details they have filled in along with the file attachment.

Now, I had a bit of trouble getting an Office 2007 .docx file to be accepted but eventually found a list of MIME types relating to Office 2007 and added the variable in for the .docx file type.

However, I would also like a user who has no file they want to upload, simply fill in the other details and have it work that way too when submit is pressed.

But I don't know how to achieve this. At the moment it tells me "Wrong type of file attached" if I leave the file upload feel empty.

I am a complete novice to .php and I dunno what I'm looking for, I assume there is a variable I have to add to the code in the same place I added the one for the .docx file, is that right?.

Don't laff, but I tried :-

Code:
$strresume_type=="none"

Am I along the right sort of lines thinking its something like that or way off the mark?.

It's being sent directly to an email address, its not going into a database on my server btw, if that makes any difference?.

As I said, php newbie, so any assistance appreciated. :cool:
 
Just check to see if a file was actually uploaded with the form

PHP:
if(is_uploaded_file($_FILES['name_of_file_field']['tmp_name']))
{
// Send email with attachment
}
else
{
// Send email without attachment
}
 
To be honest, I don't think I'm dropping the above code into the code I have here in the correct place.

Any ideas where it should go?. Should I put it in after the //SEND MAIL part of the code which is near the bottom of the page? :-

Code:
    // SEND MAIL 
        
       @mail($to, $subject, $message, $headers); 
    

     fclose($fp); 

    echo '<font style="font-family:Verdana, Arial; font-size:11px; color:#333333; font-weight:bold">Attachment has been sent Successfully.<br /></font>'; 
} 
else 
    { 
        echo '<font style="font-family:Verdana, Arial; font-size:11px; color:#F3363F; font-weight:bold">Wrong file format. Mail was not sent.</font>'; 
        //echo "<script>window.location.href='careers.html';</script>"; 
    }
 
Lets have a look at the whole thing, there's got to be a bit further up the code where you're attaching the uploaded.
 
No worries m8, here it is.

It's a free php code that is floating around the web in various places, I thought it would be just the ticket but as I said I know nothing about php, only XHTML and CSS. :(

Here's the full code :-

Code:
<?php 
      
                      
       $strname=ucfirst($_REQUEST["strname"]); 
    $straddress=ucfirst($_REQUEST["straddress"]); 
    $strcity=ucfirst($_REQUEST["strcity"]); 
    $strstate=ucfirst($_REQUEST["strstate"]);   
          
        
       $phone=$_REQUEST["strno"]; 
       if($phone != ""){ $strno=$phone; } else { $strno="-"; }    
        
    $stremail=$_REQUEST["stremail"];    
    $strcomments=ucfirst($_REQUEST["strcomments"]); 

    
    $strresume_name=$_FILES["strresume"]["name"]; 
    $strresume_type=$_FILES["strresume"]["type"]; 
    $strresume_size=$_FILES["strresume"]["size"]; 
    $strresume_temp=$_FILES["strresume"]["tmp_name"]; 


        
    if($strresume_type=="application/octet-stream" or $strresume_type=="text/plain" or $strresume_type=="application/msword" or $strresume_type=="application/vnd.openxmlformats-officedocument.wordprocessingml.document") 
    { 
    
        $message= ' 
    
    
            <table cellspacing="0" cellpadding="8" border="0" width="400"> 
            <tr> 
                <td colspan="2"></td> 
            </tr> 
            <tr bgcolor="#eeeeee"> 
                <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;"><strong>Name</strong></td> 
                <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'.$strname.'</td> 
            </tr> 
            <tr><td colspan="2" style="padding:0px;"><img src="images/whitespace.gif" alt="" width="100%" height="1" /></td></tr> 
            <tr bgcolor="#eeeeee"> 
              <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;"><strong>Address</strong></td> 
              <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'.$straddress.'</td> 
              </tr> 
            <tr><td colspan="2" style="padding:0px;"><img src="images/whitespace.gif" alt="" width="100%" height="1" /></td></tr> 
            <tr bgcolor="#eeeeee"> 
              <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;"><strong>City</strong></td> 
              <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'.$strcity.'</td> 
              </tr> 
              <tr><td colspan="2" style="padding:0px;"><img src="images/whitespace.gif" alt="" width="100%" height="1" /></td></tr> 
            <tr bgcolor="#eeeeee"> 
              <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;"><strong>State</strong></td> 
              <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'.$strstate.'</td> 
              </tr> 
              <tr><td colspan="2" style="padding:0px;"><img src="images/whitespace.gif" alt="" width="100%" height="1" /></td></tr> 
            <tr bgcolor="#eeeeee"> 
              <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;"><strong>Contact No.</strong></td> 
              <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'.$strno.'</td> 
              </tr> 
            <tr><td colspan="2" style="padding:0px;"><img src="images/whitespace.gif" alt="" width="100%" height="1" /></td></tr> 
            <tr bgcolor="#eeeeee"> 
                <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;"><strong>Email</strong></td> 
                <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'.$stremail.'</td> 
            </tr> 
            <tr><td colspan="2" style="padding:0px;"><img src="images/whitespace.gif" alt="" width="100%" height="1" /></td></tr> 
            
            <tr bgcolor="#eeeeee"> 
                <td colspan="2" style="font-family:Verdana, Arial; font-size:11px; color:#333333;"><strong>Comments</strong></td> 
            </tr>                
            <tr bgcolor="#eeeeee"> 
                <td colspan="2" style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'.$strcomments.'</td> 
            </tr>                
                                    
            <tr><td colspan="2" style="padding:0px;"><img src="images/whitespace.gif" alt="" width="100%" height="1" /></td></tr> 
         </table> 


            

'; 

    // MAIL SUBJECT 

    $subject = "CV Submitted"; 
    
    // TO MAIL ADDRESS 
    
    
    $to="[email protected]"; 

/* 
    // MAIL HEADERS 
                        
    $headers  = "MIME-Version: 1.0\n"; 
    $headers .= "Content-type: text/html; charset=iso-8859-1\n"; 
    $headers .= "From: Name <[email protected]>\n"; 

*/ 
  


    // MAIL HEADERS with attachment 

    $fp = fopen($strresume_temp, "rb"); 
    $file = fread($fp, $strresume_size); 

    $file = chunk_split(base64_encode($file)); 
    $num = md5(time()); 
    
        //Normal headers 

    $headers  = "From: Gary<[email protected]>\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: <".gettimeofday()." 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 

    $headers  .= "Content-Type:".$strresume_type." "; 
       $headers  .= "name=\"".$strresume_name."\"r\n"; 
       $headers  .= "Content-Transfer-Encoding: base64\r\n"; 
       $headers  .= "Content-Disposition: attachment; "; 
       $headers  .= "filename=\"".$strresume_name."\"\r\n\n"; 
       $headers  .= "".$file."\r\n"; 
       $headers  .= "--".$num."--"; 

    
    
    // SEND MAIL 
        
       @mail($to, $subject, $message, $headers); 
    

     fclose($fp); 

    echo '<font style="font-family:Verdana, Arial; font-size:11px; color:#333333; font-weight:bold">Attachment has been sent Successfully.<br /></font>'; 
} 
else 
    { 
        echo '<font style="font-family:Verdana, Arial; font-size:11px; color:#F3363F; font-weight:bold">Wrong file format. Mail was not sent.</font>'; 
        //echo "<script>window.location.href='careers.html';</script>"; 
    } 

?>
 
Forgot to add, here's the HTML code that goes with it :-

Code:
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 


<title>Untitled Document</title> 
</head> 

<body style="padding:3px; margin:0px;" bgcolor="#FFFFFF"> 
<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="careersuccess.php" enctype="multipart/form-data"> 
      <table cellpadding="0" cellspacing="0" border="0" width="100%"> 
          <tr> 
            <td width="23%" class="body"> Name</td> 
            <td width="3%" class="body">:</td> 
            <td width="74%"><input type="text" name="strname" class="textfield"></td> 
        </tr> 
        <tr><td style="height:3px"></td></tr> 
        <tr> 
            <td width="23%" class="body"> Address</td> 
            <td width="3%" class="body">:</td> 
            <td width="74%"><textarea cols="16" name="straddress"></textarea></td> 
        </tr> 
        <tr><td style="height:3px"></td></tr> 
        <tr> 
            <td width="23%" class="body"> City</td> 
            <td width="3%" class="body">:</td> 
            <td width="74%"><input type="text" name="strcity" class="textfield"></td> 
        </tr> 
        <tr><td style="height:3px"></td></tr> 
        <tr> 
            <td width="23%" class="body"> State</td> 
            <td width="3%" class="body">:</td> 
            <td width="74%"><input type="text" name="strstate" class="textfield"></td> 
        </tr> 
        <tr><td style="height:3px"></td></tr> 
        <tr> 
            <td width="23%" class="body"> Contact No</td> 
            <td width="3%" class="body">:</td> 
            <td width="74%"><input type="text" name="strno" class="textfield"></td> 
        </tr> 
        <tr><td style="height:3px"></td></tr> 
        <tr> 
            <td width="23%" class="body"> Email</td> 
            <td width="3%" class="body">:</td> 
            <td width="74%"><input type="text" name="stremail" class="textfield"></td> 
        </tr> 
        <tr><td style="height:3px"></td></tr> 
        <tr> 
            <td width="23%" class="body"> Comments</td> 
            <td width="3%" class="body">:</td> 
            <td width="74%"><textarea cols="16" name="strcomments"></textarea></td> 
        </tr> 
        <tr><td style="height:3px"></td></tr> 
        <tr> 
            <td width="23%" class="body"> Resume</td> 
            <td width="3%" class="body">:</td> 
            <td width="74%"><input type="file" name="strresume"></td> 
        </tr> 
        <tr><td style="height:10px"></td></tr> 
        <tr> 
            <td colspan="3" align="center"> 
                <input type="submit" value="Send" name="submit" onClick="return validate();"> <input type="reset" value="Reset" name="reset"> 
            </td> 
        </tr> 
        
      </table>    
     </form> 

</td> 
    </tr> 
    <tr> 
      <td colspan="2" align="center"> </td> 
  </tr> 
    </table> 
</body> 
</html>
 
Ouch, the headers on that are a real mess, have you modifed them or were they like that?

Download all the files from here:
http://www.phpguru.org/downloads/html.mime.mail/htmlMimeMail-2.5.2/

You need:
RFC822.php
htmlMimeMail.php
mimePart.php
smtp.php

all in the same folder as the file sending the mail. Download and have a look through the example php files they have on there as well. They've all the settings you need to do this really easily.

I started to modifiy your code so you can use the top bit and do away with the mailing code underneath once you've setup the htmlmimemail.

PHP:
// Set the variable uploaded to no
$uploaded = 0;
$strresume_type = "";
	
	if(is_uploaded_file($_FILES["strresume"]["tmp_name"]))
    {
		$strresume_name=$_FILES["strresume"]["name"]; 
    	$strresume_type=$_FILES["strresume"]["type"]; 
    	$strresume_size=$_FILES["strresume"]["size"]; 
    	$strresume_temp=$_FILES["strresume"]["tmp_name"];
		$uploaded = 1; // Set the variable uploaded to yes
	}// if(is_uploaded_file($_FILES["strresume"]["tmp_name"]))
	
// Set the mime type ok to no
	$mimeok = 0;
	
	if(($uploaded == 1) && ($strresume_type=="application/octet-stream" || $strresume_type=="text/plain" || $strresume_type=="application/msword" || $strresume_type=="application/vnd.openxmlformats-officedocument.wordprocessingml.document")) 
    {
		$mimeok = 1;// Set the mime type ok to yes
	}// if(($uploaded == 1) && ($strresume_type=="application/octet-stream" || $strresume_type=="text/plain" || $strresume_type=="application/msword" || $strresume_type=="application/vnd.openxmlformats-officedocument.wordprocessingml.document")) 
		
	$message= '*****';

// Check to see if the attachment was uploaded and is of correct format
if($mimeok == 1)
{
// Send message with attachment
}// if($mimeok == 1)
else
{
// Send message without attachment
}// else to if($mimeok == 1)
 
Perfect m8, many thanks for taking the time to help a complete idiot!! :p

I'm at work just now but I will give that a go first thing in the morning.

Cheers. :cool:
 
Strange, trying to download the php files from your linked page but if I Right Clcik and "Save Target As" they download as html files with a files size of 0 :-

php.jpg


If I try and open them direct from the web page, a blank page opens. :confused:
 
I can't explain why they've got a file size of 0, but if you open them in a browser, they will be blank as they're likely to be classes that don't output anything until accessed by another script.

Try using a decent OS to get them, maybe? :p
 
Back
Top Bottom