5 Replies Latest reply on Oct 19, 2009 4:33 PM by Günter Schenk

    PHP forms submitted as an attachment rather than email?

    Guperman

      How do I get my PHP forms submitted as an attachment rather than email?

       

      Meaning, if I set up a form on my website and someone completes and submits it, how can I get the responses as an attachment rather than in email text?

       

      Much appreciated.

       

      G

        • 1. Re: PHP forms submitted as an attachment rather than email?
          Günter Schenk Level 4

          Hi there,

           

          that´s an interesting approach :-)

           

          The file (e.g. .txt or .pdf) you´d like to have attached will of course first have to be created and stored in a server directory of your choice, what could (at least in theory) be done with some custom PHP "write data to file" code that´s encapsulated in a Custom Trigger -- and this trigger would have to be "placed" (aka "priority")....

           

          1. after the Insert Record transaction, and

          2. before the Send Email trigger

           

          Cheers,

          Günter

          • 2. Re: PHP forms submitted as an attachment rather than email?
            Guperman Level 1

            Hi Gunter

             

            Thank you for your response.

             

            That makes sense. Now the question is HOW to write that code.

             

            Can you perhaps help me?

             

            Regards
            --

            • 3. Re: PHP forms submitted as an attachment rather than email?
              Günter Schenk Level 4

              Guperman wrote:

               

              Now the question is HOW to write that code.

               

              Can you perhaps help me?

              well, this can get complicated (or not) depending on what type of file (e.g. TXT or PDF) you wish to generate. At this point I´m not going to provide any ready-made code, but will be leaving the required research/development/testing basically up to you, hehe

               

              For a start I´d recommend to learn how to write TXT files, because that´s a whole lot easier than e.g. generating a PDF file. Googling for stuff like "php write file" reveals lots of tutorials, and IMO one pretty comprehensible tutorial for beginners is "php - file write" by tizag.com.

               

              Some hints though:

               

              1. instead of the tutorial´s static string examples (e.g. $stringData = "Bobby Bopper\n";) you´ll need to replace such a static string with either a PHP POST variable ($_POST['fieldname']) or ADDT´s corresponding transaction value.

               

              2. that tutorials also suggest a static value for the to-be-generated file, e.g. $myFile = "testFile.txt" -- but it´s also possible to mix in "dynamic" values in here, e.g. such as a PHP "current date/time" value

               

              Cheers,

              Günter

              • 4. Re: PHP forms submitted as an attachment rather than email?
                Guperman Level 1

                Hi Gunter

                 

                I have spent so much time on this now.

                 

                I have tried to convert the php to an attached .txt or .csv file.

                 

                Learnt a lot but still can not seem to get the form data to be submitted as an attached file.

                 

                Here are two of my attempts, please assist:

                 

                <?php

                 

                $email=$_REQUEST['email'];
                $firstName=$_REQUEST['firstName'];
                $lastName=$_REQUEST['lastName'];

                 

                //The Attachment
                $cr = "\n";
                $data = "Email" . ',' . "First Name" . ',' . "Last Name" . $cr;
                $data .= "$email" . ',' . "$firstName" . ',' . "$lastName" . $cr;
                $fp = fopen('reservationTest.csv','a');
                fwrite($fp,$data);
                fclose($fp);

                 

                // Mail to
                $email = "myemailaddress";

                 

                //subject
                $subject = "Test Budget reservation";

                 

                //Header
                $headers("Content-type: application/octet-stream");
                $headers("Content-Disposition: attachment; filename=reservationTest.csv");
                $headers("Pragma: no-cache");
                $headers("Expires: 0");

                 


                //Message
                $message = "".
                "Email: $email" . "\n" .
                "First Name: $firstName" . "\n" .
                "Last Name: $lastName";

                 

                mail($email, $subject, $message, $headers);

                 


                ?>

                 

                <html>

                <body>
                <table width="400" border="0" cellspacing="0" cellpadding="0">
                <tr>
                <td>
                <div align="center">Guperman Your Test Message Has Been Submitted</div>
                </td>
                </tr>
                </table>
                </body>
                </html>

                 

                EOD;
                echo "$theResults";

                 


                OR

                 

                 


                <?php

                 

                /* subject and email variables */

                 

                $email = $_POST['email'];
                $lastname = $_POST['lastname'];
                $firstname = $_POST['firstname'];

                 


                    $to = 'myemailaddress';
                    $emailSubject = 'Test Form';
                    $headers = "From: $email\n";

                 

                $message = "A new reservation test.\n

                 


                Last Name</b>: $lastname
                Name</b>: $firstname

                Email</b>: $email

                 

                 

                    mail($to,$emailSubject,$headers,$message);

                 

                //open the file and choose the mode
                $fh = fopen("reservationTest.txt", "a");
                fwrite($fh, $email);

                //close the file
                fclose($fh);

                 

                ?>


                <html>
                <body>
                <table width="400" border="0" cellspacing="0" cellpadding="0">
                <tr>
                <td>
                <div align="center">Guperman Your Test Message Has Been Submitted</div>
                </td>
                </tr>
                </table>
                </body>
                </html>

                 

                 

                On both accounts when I test these files I get the following types of messages.

                 

                Warning:  fopen(reservationTest.csv) [function.fopen]: failed to open stream: Permission denied in \\HOSTING\DFS\20\1\9\1\2028751191\user\sites\mywebsite.com\www\reservationTest.php on line 11

                Warning:  fwrite(): supplied argument is not a valid stream resource in \\HOSTING\DFS\20\1\9\1\2028751191\user\sites\mywebsite.com\www\reservationTest.php on line 12

                Warning:  fclose(): supplied argument is not a valid stream resource in \\HOSTING\DFS\20\1\9\1\2028751191\user\sites\mywebsite.com\www\reservationTest.php on line 13

                Fatal error:  Function name must be a string in \\HOSTING\DFS\20\1\9\1\2028751191\user\sites\mywebsite.com\www\reservationTest.php on line 22

                 

                And then to give the .php files permission I left click on the already uploaded files and try and set the permission to 777, but then I get a response like: Setting Access Properties failed for:

                 

                Your assistance is much appreciated. Regards

                • 5. Re: PHP forms submitted as an attachment rather than email?
                  Günter Schenk Level 4

                  -----------------

                  And then to give the .php files permission I left click on the already uploaded files and try and set the permission to 777

                  -----------------

                   

                  Changing the file permission alone will not help when the directory which contains this file lacks the proper permission. That said, please try with first setting the permission of the current working directory to. e.g. 777 as well and then try CHMOD´ing the file permission accordingly.

                   

                  BTW, I see that both 'reservationTest.php' as well as the to-be-generated files (e.g. 'reservationTest.csv') seem to be located in the 'www' root directory of your remote server. I honestly wouldn´t try doing that, not only because the server admin might - for obvious security reasons - not allow you to change the permission of the root directory, but also because it´s recommended to have such file related operations execute in an existing subdirectory of your choice, which usually can be CHMOD´ed according to your preference.

                   

                  Cheers,

                  Günter