How do I make form without attachment?

Discussion in 'PHP' started by peppy, Jun 18, 2007.

Thread Status:
Not open for further replies.
  1. #1
    I have gotten through most of my phases, I just have one small issue to be taken care of with the PHP form code. The following code below is for an attachment file. Can someone look at this code and let me know what the code is supposed to look like so it will work properly without an attachment so that it would work correctly as a standard form with the current required fields in play? I would really appreciate it.

    Thanks,
    Peppy


    <?php
    
    define("EMAIL_TO",            'email@biz.com');
    define("EMAIL_SUBJECT",     'New POC from %s' );
    define("EMAIL_FROM",                    'email@biz.com' );
    define("EMAIL_MAX_ATTACHMENT_SIZE",  '300000' );
    function error( $msg )
    {
        printf("<center><font color=red>%s</font><center><br />", $msg);   
    }
    
    function Attach( $message, $headers = null )
    {
        // Build your message from your posted form
        $message = nl2br( sprintf(
            "Name : %s\n".
            "Telephone : %s\n".
            "Email : %s\n".
            "%s",
            $_POST['Name'],
            $_POST['TelNo'],
            $_POST['Email'],
            $message
        ) );
        // start to add to headers
        $headers .= sprintf( "From: %s\n", EMAIL_FROM );
    
    
    
        // check to see if we are attaching
        if( is_uploaded_file( $_FILES['attach']['tmp_name'] ) )
    
    
    
        {
            // the content needs a boundary so that email clients know where to get data
            $boundary = sprintf( '==Multipart_Boundary_x%s', md5( time( ) ) ) ;
    
    
    
            // continue to build headers, adding the boundary for attached data
            $headers .= sprintf( 
                                "MIME-Version: 1.0\n".
                                "Content-Type: multipart/mixed;\n boundary=\"%s\"\n",
                                $boundary );
    
    
    
            // edit message to include data and boundaries
            $message = sprintf(
                                "This is a multi-part message in MIME format.\n\n".
                                "--%s\n".
                                "Content-Type: text/html; charset=\"iso-8859-1\"\n".
                                "Content-Transfer-Encoding: 7bit\n\n".
                                "%s\n\n".
                                "--%s\n".
                                "Content-Type: %s;\n name=\"%s\"\n".
                                "Content-Disposition: attachment;\n filename=\"%s\"\n".
                                "Content-Transfer-Encoding: base64\n\n".
                                "%s\n\n".
                                "--%s-\n",
                                $boundary,
                                $message,
                                $boundary,
                                $_FILES['attach']['type'],
                                $_FILES['attach']['name'],
                                $_FILES['attach']['name'],
                                chunk_split( base64_encode( file_get_contents( $_FILES['attach']['tmp_name'] ) ) ),
                                $boundary            
            );  
        }
    
    
    
        // send the original message with the file attached
        return mail( EMAIL_TO, sprintf( EMAIL_SUBJECT, $_SERVER['REMOTE_ADDR'] ), $message, $headers );
    }
    if( $_POST )
    {
        if( trim( $_POST['Name'] ) == "" )
        {
            error( 'Please enter a name' );            
        }
        elseif( trim( $_POST['TelNo']) == "" )
        {
            error( 'Please enter a valid phone number, containing only numeric characters' );    
        }
        elseif( trim( $_POST['Email'] ) == "" )
        {
            error( 'Please enter an email address' );    
        }
        elseif( trim( $_POST['MsgBody'] ) == "" )
        {
            error( 'Please enter a message body' );
        }
        elseif( Attach( $_POST['message'] ) )
        {
            printf( "<center><font color=blue>Your message was sent to %s</font></center>", EMAIL_TO );
        }   
        else
        {
            error( 'Failed to deliver your message' );
        }
    }
    ?>
    Code (markup):

     
    peppy, Jun 18, 2007 IP
  2. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #2
    It works with/without attachments. In your email, form, if you take out:
    <input type="file" name"attach" />
    Then, the script will just send a regular email.

    If you don't want attachments to be able to be sent at all, use this...
    <?php
    
    define("EMAIL_TO",            'email@biz.com');
    define("EMAIL_SUBJECT",     'New POC from %s' );
    define("EMAIL_FROM",                    'email@biz.com' );
    define("EMAIL_MAX_ATTACHMENT_SIZE",  '300000' );
    function error( $msg )
    {
        printf("<center><font color=red>%s</font><center><br />", $msg);   
    }
    
    function Attach( $message, $headers = null )
    {
        // Build your message from your posted form
        $message = nl2br( sprintf(
            "Name : %s\n".
            "Telephone : %s\n".
            "Email : %s\n".
            "%s",
            $_POST['Name'],
            $_POST['TelNo'],
            $_POST['Email'],
            $message
        ) );
        // start to add to headers
        $headers .= sprintf( "From: %s\n", EMAIL_FROM );
    
        // send the original message with the file attached
        return mail( EMAIL_TO, sprintf( EMAIL_SUBJECT, $_SERVER['REMOTE_ADDR'] ), $message, $headers );
    }
    if( $_POST )
    {
        if( trim( $_POST['Name'] ) == "" )
        {
            error( 'Please enter a name' );            
        }
        elseif( trim( $_POST['TelNo']) == "" )
        {
            error( 'Please enter a valid phone number, containing only numeric characters' );    
        }
        elseif( trim( $_POST['Email'] ) == "" )
        {
            error( 'Please enter an email address' );    
        }
        elseif( trim( $_POST['MsgBody'] ) == "" )
        {
            error( 'Please enter a message body' );
        }
        elseif( Attach( $_POST['message'] ) )
        {
            printf( "<center><font color=blue>Your message was sent to %s</font></center>", EMAIL_TO );
        }   
        else
        {
            error( 'Failed to deliver your message' );
        }
    }
    ?>
    PHP:
     
    krt, Jun 18, 2007 IP
  3. peppy

    peppy Active Member

    Messages:
    389
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    95
    #3
    This works somewhat, but in my email after someone submits the form, I get something like this:

    Name : JOE<br />
    Telephone : 5555555555<br />
    Email : <br />

    Is there anyway to get rid of those " <br /> " at the end of each item? Normally it doesn't do this when the script works correctly.

    Thanks,
    Peppy
     
    peppy, Jun 19, 2007 IP
  4. UnrealEd

    UnrealEd Peon

    Messages:
    148
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #4
    you have to set the mail Content-type to text/html, like this:
    $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
    PHP:
    you can just add the line above after:
    $header .= sprintf( "From: %s\n", EMAIL_FROM );
    PHP:
     
    UnrealEd, Jun 19, 2007 IP
  5. peppy

    peppy Active Member

    Messages:
    389
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    95
    #5
    Awsome UnrealEd! Thanks for figuring that out, it is very much appreciated by me for all the help from you and everyone.

    Cheers!
    Peppy
     
    peppy, Jun 19, 2007 IP
  6. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #6
    That or just take out the nl2br() in:
    $message = nl2br( sprintf(
            "Name : %s\n".
            "Telephone : %s\n".
            "Email : %s\n".
            "%s",
            $_POST['Name'],
            $_POST['TelNo'],
            $_POST['Email'],
            $message
        ) );
    PHP:
     
    krt, Jun 20, 2007 IP
Thread Status:
Not open for further replies.