#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2011
    Posts
    24
    Rep Power
    0

    PHP Form with Attach File


    I'm trying to get a PHP Form with an attachment.

    Everything else works but the attachement part.

    Can please anyone help me on what I am doing wrong?

    Thank you so much

    PHP Code:
    <?php
    if(isset($_POST['email'])) {
         
        
    // CHANGE THE TWO LINES BELOW
        
    $email_to "unlimitedwonders@gmail.com";
         
        
    $email_subject "Contributing to UnlimitedWonders";
         
         
        function 
    died($error) {
            
    // your error code can go here
            
    echo "We are very sorry, but there were error(s) found with the form you submitted. ";
            echo 
    "These errors appear below.<br /><br />";
            echo 
    $error."<br /><br />";
            echo 
    "Please go back and fix these errors.<br /><br />";
            die();
        }
         
        
    // validation expected data exists
        
    if(!isset($_POST['first_name']) ||
            !isset(
    $_POST['last_name']) ||
            !isset(
    $_POST['email']) ||
            !isset(
    $_POST['comments']) ||
            !isset(
    $_POST['uploaded_file'])) {
            
    died('We are sorry, but there appears to be a problem with the form you submitted.');      
        }
         
        
    $first_name $_POST['first_name']; // required
        
    $last_name $_POST['last_name']; // required
        
    $email_from $_POST['email']; // required
        
    $comments $_POST['comments']; // required
        
    $uploaded_file $_POST['uploaded_file']; // required


    //*** Attachment ***//  
    if($_FILES["uploaded_file"]["name"] != "")  
    {  
    $strFilesName $_FILES["uploaded_file"]["name"];  
    $strContent chunk_split(base64_encode(file_get_contents($_FILES["uploaded_file"]["tmp_name"])));  
    $strHeader .= "--".$strSid."\n";  
    $strHeader .= "Content-Type: application/octet-stream; name=\"".$strFilesName."\"\n";  
    $strHeader .= "Content-Transfer-Encoding: base64\n";  
    $strHeader .= "Content-Disposition: attachment; filename=\"".$strFilesName."\"\n\n";  
    $strHeader .= $strContent."\n\n";  





         
        
    $error_message "";
        
    $email_exp '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
      if(!
    preg_match($email_exp,$email_from)) {
        
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
      }
        
    $string_exp "/^[A-Za-z .'-]+$/";
      if(!
    preg_match($string_exp,$first_name)) {
        
    $error_message .= 'The First Name you entered does not appear to be valid.<br />';
      }
      if(!
    preg_match($string_exp,$last_name)) {
        
    $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
      }
      if(
    strlen($comments) < 2) {
        
    $error_message .= 'The Comments you entered do not appear to be valid.<br />';
      }
      if(
    strlen($error_message) > 0) {
        
    died($error_message);
      }
        
    $email_message "Form details below.\n\n";
         
        function 
    clean_string($string) {
          
    $bad = array("content-type","bcc:","to:","cc:","href");
          return 
    str_replace($bad,"",$string);
        }
         
        
    $email_message .= "First Name: ".clean_string($first_name)."\n";
        
    $email_message .= "Last Name: ".clean_string($last_name)."\n";
        
    $email_message .= "Email: ".clean_string($email_from)."\n";
        
    $email_message .= "Comments: ".clean_string($comments)."\n";
         
         
    // create email headers
    $headers 'From: '.$email_from."\r\n".
    'Reply-To: '.$email_from."\r\n" .
    'X-Mailer: PHP/' phpversion();
    @
    mail($email_to$email_subject$email_message$headers); 
    ?>
     
    <!-- place your own success html below -->
     
    Thank you!!!! 

    <?php
    }
    die();
    ?>
    Last edited by requinix; February 15th, 2013 at 12:49 PM.
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,237
    Rep Power
    9400
    That's heading on the right path but has much further to go before it will work.

    Grab yourself a copy of some emailing library, like PHPMailer, and use that to manage this. It's much easier than trying to write it all yourself.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2011
    Posts
    24
    Rep Power
    0
    Originally Posted by requinix
    That's heading on the right path but has much further to go before it will work.

    Grab yourself a copy of some emailing library, like PHPMailer, and use that to manage this. It's much easier than trying to write it all yourself.

    Thanks for your reply. I tried but I'm not able to make any of then work. I also tried to get some code from the web and wasn't able to make to work any code that send files.

    The code I attached to my original message works, except for sending the file. I think it would be easier to find out what I need to fix.

    Thanks again
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2011
    Posts
    24
    Rep Power
    0
    I also tried: But not able to make it work either.

    Code:
    <form name="filepost" method="post" action="file.php" enctype="multipart/form-data" id="file">
        <table width="300" border="0" cellspacing="0" cellpadding="0">
          <tr valign="bottom">
            <td height="20">Your Name:</td>
          </tr>
          <tr>
            <td><input name="from" type="text" id="from" size="30"></td>
          </tr>
          <tr valign="bottom">
            <td height="20">Your Email Address:</td>
          </tr>
          <tr>
            <td class="frmtxt2"><input name="emaila" type="text" id="emaila" size="30"></td>
          </tr>
          <tr>
            <td height="20" valign="bottom">RFQ File:</td>
          </tr>
          <tr valign="bottom">
            <td valign="bottom"><input name="filea" type="file" id="filea" size="16"></td>
          </tr>
          <tr>
            <td height="40" valign="middle"><input name="Reset2" type="reset" id="Reset2" value="Reset">
                <input name="Submit2" type="submit" value="Submit" onClick="return CheckData45()"></td>
          </tr>
        </table>
        </form>


    Code:
    <?php
     
    // request variables
    $from=$_REQUEST["from"];
    $emaila=$_REQUEST["emaila"];
    $filea=$_REQUEST["filea"];
     
    if ($filea)
    {
    function mail_attachment ($from , $to, $subject, $message, $attachment){
    	$fileatt = $attachment; // Path to the file
    	$fileatt_type = "application/octet-stream"; // File Type
        $start=	strrpos($attachment, '/') == -1 ? strrpos($attachment, '//') : strrpos($attachment, '/')+1;
    	$fileatt_name = substr($attachment, $start, strlen($attachment)); // Filename that will be used for the file as the attachment
     
    	$email_from = $from; // Who the email is from
        $subject = "New Attachment Message";
    	$email_subject =  $subject; // The Subject of the email
    	$email_txt = $message; // Message that the email has in it
     
    	$email_to = $to; // Who the email is to
     
    	$headers = "From: ".$email_from;
     
    	$file = fopen($fileatt,'rb');
    	$data = fread($file,filesize($fileatt));
    	fclose($file);
    	$msg_txt="\n\n You have recieved a new attachment message from $from";
    	$semi_rand = md5(time());
    	$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
    	$headers .= "\nMIME-Version: 1.0\n" .
                "Content-Type: multipart/mixed;\n" .
                " boundary=\"{$mime_boundary}\"";
    	$email_txt .= $msg_txt;
    	$email_message .= "This is a multi-part message in MIME format.\n\n" .
                    "--{$mime_boundary}\n" .
                    "Content-Type:text/html; charset=\"iso-8859-1\"\n" .
                   "Content-Transfer-Encoding: 7bit\n\n" .
    	$email_txt . "\n\n";
    	$data = chunk_split(base64_encode($data));
    	$email_message .= "--{$mime_boundary}\n" .
                      "Content-Type: {$fileatt_type};\n" .
                      " name=\"{$fileatt_name}\"\n" .
                      //"Content-Disposition: attachment;\n" .
                      //" filename=\"{$fileatt_name}\"\n" .
                      "Content-Transfer-Encoding: base64\n\n" .
                     $data . "\n\n" .
                      "--{$mime_boundary}--\n";
     
    	$ok = mail($email_to, $email_subject, $email_message, $headers);
     
    	if($ok)
    	{
    	echo "File Sent Successfully.";
    	unlink($attachment); // delete a file after attachment sent.
    	}
    	else
    	{
    		die("Sorry but the email could not be sent. Please go back and try again!");
    	}
    }
     
    move_uploaded_file($_FILES["filea"]["tmp_name"],'temp/'.basename($_FILES['filea']['name']));
    mail_attachment("$from", "youremailaddress@gmail.com", "subject", "message", ("temp/".$_FILES["filea"]["name"]));
    }
    ?>

IMN logo majestic logo threadwatch logo seochat tools logo