PHP Email Form With Attachments

Discussion in 'Web Design and Development' started by justinm, Jun 10, 2010.

  1. justinm macrumors member

    Joined:
    Aug 15, 2007
    Location:
    Orlando FL
    #1
    Hi Everyone-

    I'm working on a form for a website and am having troubles with a few things. One of the problems I'm having is getting a php form to actually send out the attachment. Everything will upload just fine into the temp folder, but the attachment never gets sent out. It just sits in the temp folder. The text along with the form is sent, just not the attachment.

    Here's the code for the script.

    Code:
    <?php ob_start();
    /**
     */
    /* "You Got Email", a PHP-FormToEmail: Script to send an Email From Data submitted on Form with an Attachments*/
    /* ========================================================================================*/
    /* Copyright (c) 2003 Chirag Ahmedabadi<chirag_kansara@yahoo.com>							*/
    /* http://.indianic.com, http://www.php-india.net                    					*/
    /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
    # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
    # OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
    # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
    # OTHER DEALINGS IN THE SOFTWARE.
    /* This script uses the PHP email class by   Brent R. Matzelle as given belwo				*/
    // phpmailer - PHP email class
    // Version 1.65, Created 08/09/2002
    // Class for sending email using either
    // sendmail, PHP mail(), or SMTP.  Methods are
    // based upon the standard AspEmail(tm) classes.
    // Author: Brent R. Matzelle <bmatzelle@yahoo.com>
    // License: LGPL, see LICENSE
    /*                                                                      				*/
    /* This program is free software. You can redistribute it and/or modify 				*/
    /* it under the terms of the GNU General Public License as published by 				*/
    /* the Free Software Foundation; either version 2 of the License.       				*/
    // / //////////////////////////////////////////////////
    // phpmailer - PHP email class
    // Version 1.65, Created 08/09/2002
    // Class for sending email using either
    // sendmail, PHP mail(), or SMTP.  Methods are
    // based upon the standard AspEmail(tm) classes.
    // Author: Brent R. Matzelle <bmatzelle@yahoo.com>
    // License: LGPL, see LICENSE
    // //////////////////////////////////////////////////
    
    // anti-spam measure
    if($_POST[misc]==4 or $_POST[misc]=="four" or $_POST[misc]=="for"){ 
    		//nifty
    }
    else{
    		//header( "Location: http://4rbbq.com?var=var&var=var" ) ;
    		//die();
    }
    
    $SendAddress = "(my email account)"; //YOUR EMAIL ADDRESS, where you want to receive the email
    //$SendAddress = "catch@bubbledvd.com"; //YOUR EMAIL ADDRESS, where you want to receive the email
    $mail_subject = "Data Submitted"; //Subject for email that you get
    $MyContents = "";
    $IgnoreFields[] = "DO_TEST,imageField_x"; //Add list of fields, variables, here separated by comma,to ignore/avoid them in sending by email 
    $PutPostData = 1; // set to 1,if want to receive the posted data in your email, if form action method is "POST"
    $PutGetData = 0; // set to 1,if want to receive the get data in your email, if form action method is "GET"
    $PutEnvData = 0; //set to 1, if you wish to receive environment data like, HOSTNAME,MACHTYPE,SHELL etc about the server on which you are running this script  
    $PutServerData = 0; //set to 1, if you wish to receive data of client machine like, REMOTE_ADDR,HTTP_USER_AGENT,REMOTE_PORT,etc  
    $PutCookieData = 0; //set to 1, if you wish to receive Cookies from client machine 
    $PutAllData = 0; //set to 1, if you wish to receive all data like posted, server, environment,Cookies etc, this would incluce all data in your email, do not prefer to use this if you are not sure on what you are going to get.
    $isThereAttachment = 1; //set 0 if no attachment is required
    // if you have file upload option on your form, you must provide absolutes path to some temp folder with chmod 777 on your  server where this script is lying to keep the upload file temporary before sending email, after sending email it would be deleted, keep start and trailing slash
    $TempFolder = "/home/content/s/m/o/smokingrivers/html/recipecontest/temp/";
    // Parameter for upload validation,
    $my_max_file_size = "20480000"; // # in bytes, for any type of file upload
    // you can added as many as allowed types here by comma, for example if you are going to allow upload Ms Office file, you can add like application/msword,application/x-excel etc
    $allowed_types = array("image/bmp", "image/gif", "image/pjpeg", "image/jpeg", "application/zip");
    $image_max_width = "3000"; //this validates height of image in case of image upload
    $image_max_height = "3000"; //this validates width of image in case of image upload
    $DeleteUploadFiles = "0"; // Do you wish to delete the uploaded files or wish to keep on server?.If you wish to keep the file set to 1 or set to 0
    // If you wish to send cc to some other email addresses, give here seperated by comma
    $SendCCTo[] = "";
    // if you wish to send BCC to some other user, add list by comma separated
    $SendBCCTo[] = "";
    // Following is the subject of email that you would receive
    $EmailFromAddress = "form@(domain).com";
    // Following is from anme for email sent
    $EmailFromName = "4R Recipe Contest";
    // following is the reply address that you wish to have in your send email
    $ReplyToAddress = "user@(domain).com";
    // Following is the reply name for email sent
    $ReplyToName = "";
    // Set following variable to 1, if you wish to receive email in html format, prefer this.
    $isHTMLMail = 1; //set 0 for text email
    // Following variable sets where your user will be sent after sending an email, give complete url if page is located on other folder or on another server, IF EMAIL IS SENT SUCESSFULLY
    $SendUserToSucess = "http://4rsmokehouse.com/recipecontest/index.php?wwwsuccess=1"; //it can be like "http://mydomain.com/Thanks.html"
    // Following variable sets where your user will be sent after sending an email, give complete url if page is located on other folder or on another server, IF WE CANT SENT DATA BY EMAIL
    $SendUserToFail = "http://(domain).com/index.php?wwwsuccess=0"; //it can be like "http://mydomain.com/fail.html"
    // Code to Format Your Data which is going to be send by an email, if you can, edit it
    $TableStart = "<table align='center' border='1'><Tr align='center'><td  colspan='4' align='center'><strong><HEADING></strong></td></TR>";
    $DataLoop = "<td><B><var_name>:-<b></td><td><var_value></td>";
    $TableEnd = "</table><BR><P>";
    // //////////////////////////////////////////////////
    // Function to validate uploded file
    function validate_upload($the_file)
    {
        //return true;
    		global $my_max_file_size, $image_max_width, $image_max_height, $allowed_types, $the_file_type, $registered_types;
    
    		if (isset($_FILES[$the_file]["name"]))
        { 
    				// if file size is greater than max file size defined
            if ($_FILES[$the_file]['size'] > $my_max_file_size) {
    					 return false; 
            }
    				// if file type is not allowed
            if (!in_array($_FILES[$the_file]['type'], $allowed_types)) {
    					 return false; 
    				}
            // if file uploaded is images then validate the height and widht of the file
            if (ereg("image", $_FILES[$the_file]['type']) && (in_array($_FILES[$the_file]['type'], $allowed_types)))
            {
                $size = GetImageSize($_FILES[$the_file]['tmp_name']);
                list($foo, $width, $bar, $height) = explode("\"", $size[3]);
                if ($width > $image_max_width) {
        					 return false;
    						}
                if ($height > $image_max_height){
        					 return false;
    						}
            } 
    				return true;
        } 
    		else {
    		}
    } 
    // End of validate upload function
    // fuction validate email address
    function validate_email($email)
    {
        $email = trim($email); # removes white space 
        if (!empty($email)): 
            // validate email address syntax
            if (preg_match('/^[a-z0-9\_\.]+@[a-z0-9\-]+\.[a-z]+\.?[a-z]{2,4}$/i', $email, $match)):
                return strtolower($match[0]); # valid! 
            endif;
            endif;
            return false; # NOT valid! 
        } 
        // end of function to validate email address
        // Function to validate and format the variables available
        function CheckNFormat($var, $value)
        {
            Global $IgnoreFields, $DataLoop; 
            // if varibale is not in the ignore list
            if (!in_array($var, $IgnoreFields))
            { 
                // format the display
                $ReplaceVarName = str_replace("<var_name>", $var, $DataLoop);
                $ReplaceVarValue = str_replace("<var_value>", $value, $DataLoop);
                $data = "<tr>" . $ReplaceVarName . $ReplaceVarValue . "</tr>";
                return $data;
            } 
            else
            {
                return false;
            } 
        } 
        // //////////////////////////////////////////////////
        function GetGlobals()
        {
            global $k , $TableStart, $DataLoop, $TableEnd ;
            $data = str_replace("<HEADING>", "All Global Data", $TableStart);
            while (list($key, $val) = each($GLOBALS))
            {
                $data .= CheckNFormat($key, $val);
            } 
            return $data;
        } 
        function GetPostData()
        {
            global $k , $TableStart, $DataLoop, $TableEnd ;
            $data = str_replace("<HEADING>", "Data Posted From 4R Recipe Contest", $TableStart);
            while (list($key, $val) = each($_POST))
            {
                if(strpos($key,"imageField")===false){
        						$ReplaceVarName = str_replace("<var_name>", $key, $DataLoop); 
                    // If data is an array like multiple list box
                    if (!is_array($val))
                    {
                        $val1 = $val;
                    } 
                    else
                    {
                        $val1 = "";
                        foreach($val as $res)
                        {
                            $val1 .= $comma . $res;
                            $comma = ",&nbsp";
                        } 
                    } 
                    $data .= CheckNFormat($key, $val1);
    						}
            } 
            return $data;
        } 
    		function GetFileData()
        {
    		
    		
    
        } 
        function GetGetData()
        {
            global $k , $TableStart, $DataLoop, $TableEnd ;
            $data = str_replace("<HEADING>", "Get Data of Form", $TableStart);
            while (list($key, $val) = each($_GET))
            {
                $ReplaceVarName = str_replace("<var_name>", $key, $DataLoop); 
                // If data is an array like multiple list box
                if (!is_array($val))
                {
                    $val1 = $val;
                } 
                else
                {
                    $val1 = "";
                    foreach($val as $res)
                    {
                        $val1 .= $comma . $res;
                        $comma = ",&nbsp";
                    } 
                } 
                $data .= CheckNFormat($key, $val1);
            } 
            return $data;
        } 
    
    
    (There's more, but it goes over the character limit of a post.)

    Thanks for the help!
     
  2. brisbaneguy29 macrumors 6502

    brisbaneguy29

    Joined:
    Nov 27, 2007
    Location:
    Brisbane
    #2
    I have used that very script on many of my websites, because it is so easy to set up, and it works every time. I have never had any issues with it sending the attachment ion the email.

    Looking at your code, I see you have modified it slightly, with the spam protection, and also my version of the code is slightly different at the start with the validate uploaded file.

    All I can think of is these slight changes are stuffing up the script. Have you tried putting it up there without these additions?
     
  3. justinm thread starter macrumors member

    Joined:
    Aug 15, 2007
    Location:
    Orlando FL
    #3
    Do you have a blank copy of that script? I can't seem to find a place to download it. I'll just redo it and give that a shot.

    Thanks!
     
  4. harpster macrumors regular

    Joined:
    Jan 26, 2010
    #4

Share This Page