Page 1 of 1

mailing form with attachment

Posted: Mon Apr 09, 2007 5:09 am
by tyrohansen
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


hello there, could someone please help me.  i have been trying to write this very good email attachment script. during online testing it brings a cgi timeout. message goes like this "CGI Timeout.   The specified CGI application exceeded the allowed time for processing. The server has deleted the process"
below is my code.

Code: Select all

<?php
        $name =$_POST["name"];
        $email = $_POST["email"];
        $age =$_POST["age"];
        $access =$_POST["access"];
        $sex =$_POST["sex"];
        $class =$_POST["class"];
        $body ="name: $name<br/>email: $email<br/> age: $age <br> sex: $sex <br/> class: $class";

        $to = "tyrohansen@yahoo.com,joesemag@yahoo.co.uk";
        $from = "<webmaster@ventug.com>";
        $subject = "Here is your attachment";


        $fileatt = $HTTP_POST_FILES["userfile"]["tmp_name"];
        $fileatttype = $HTTP_POST_FILES["userfile"]["type"];
        $fileattname = $HTTP_POST_FILES["userfile"]["name"];

        $file = fopen( $fileatt, 'rb' );
        $data = fread( $file, filesize( $fileatt ) );
        fclose( $file );
        $headers = "From: $from";

       //encoding
        $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}\"";

        $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" .
                $body . "\n\n";

        $data = chunk_split( base64_encode( $data ) );

        $message .= "--{$mime_boundary}\n" .
                 "Content-Type: {$fileatttype};\n" .
                 " name=\"{$fileattname}\"\n" .
                 "Content-Disposition: attachment;\n" .
                 " filename=\"{$fileattname}\"\n" .
                 "Content-Transfer-Encoding: base64\n\n" .
                 $data . "\n\n" .
                 "--{$mime_boundary}--\n";

                      //sending email
                   if( mail( $to, $subject, $message, $headers ) ) {

            echo "<p>The email was sent.</p>";

        }
        else {

            echo "<p>There was an error sending the file.</p> <br>$fileattname";

        }

?>
this script uploads a file and attaches it to web form to be mailed. please help. thank you


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Re: mailing form with attachment

Posted: Mon Apr 09, 2007 5:31 am
by lanasa
Probably not what you want to hear, but you'll need to modify your server configuration to increase your timeout setting.

If you are using a shared hosting provider, they have the timeout settings lower on purpose, to prevent someone's script from holding up scripts on other user's accounts.

Posted: Mon Apr 09, 2007 6:35 am
by Chris Corbyn
Try Swift Mailer. It's optimized for memory usage when sending attachments:

http://www.swiftmailer.org/
http://www.swiftmailer.org/wikidocs/v3/ ... ttachments
http://www.swiftmailer.org/wikidocs/v3/misc/caching (to get the memory down)

You can use set_time_limit() to stop timeouts.

Code: Select all

set_time_limit(500); //seconds

solution found

Posted: Tue Apr 10, 2007 2:36 pm
by tyrohansen
i was looking through the code error, then i decided to change the content type, it did not change much, then i read about 'concanating'
where 2 are put variable togather insteady of many lines, so i decided to remove the $body variable and replaced it with $message. and works very well. i have it working at ventug.com/test/
thank you very much