PDA

View Full Version : PHP Forum (send email / save file - need help!)


lennydiego
05-03-2003, 10:56 PM
Hi everyone,

I am currently building a rather simple form and I was wondering if someone could help me write or find a script to do what I need.

Here is what I am trying to accomplish. I have a form with about 50 fields, and when someone clicks submit I need two things to happen. I need the information sent by e-mail to a designated (specified in the script) e-mail address and a copy to be saved on the server. (So that our windows clients can later access it via a samba share.

I am not a php programmer but I believe the top can be done, but I also need a couple more things to happen. I need to be able to setup how the email is sent. It will be containing a lot of information (im sure this possible as well!). Basically I just want it to display the information that was written in the fields neatly so someone can read it. And, the good news is, the e-mail that is produced is exactly what I need saved on the server. I just need it to use one of the text fields (variable) as the file name, I am hoping I can save them as .txt files or even .html files.

If anyone could provide any assistance I would be greatful!

Thanks,
- Lenny

missing-score
05-03-2003, 11:01 PM
Right, the e-mail wont be a problem. but before I go any further, are you using a mysql database?

lennydiego
05-03-2003, 11:09 PM
Hi, no im not using a mysql database..

I was hoping the php script could just save the file locally on the system into a specified directory?

The server is running redhat linux..

silky
05-03-2003, 11:14 PM
Lenny, when you get an answer to this, would you let know and I am in exactly the same situation, the differece is i AM using mysql and changed servers and dont program, i had paid someone to do it for me. I will be off and on this site trying to work, please email me at terry@yourhouston.net when you get an answer to this question.

missing-score
05-03-2003, 11:19 PM
OK:

Sending mail is easy:



$message = "Your Message here";

$to = "email@address"; // Commas to separate multiple addresses.

$subj = "Message Subject";

$header = "From: your@email.com";



Writing to file (ensure the dir is CHMODDED to 777)



$filename = "email";
$filename .= preg_replace("/\\./", time(), microtime()); // Give an id
$filename .= ".html";

chmod(touch($filename), 777);

$file = fopen($filename, "w");

fputs($message, $file); // Ensure $message is a var

fclose($file);



That should work, I think. :rolleyes:

lennydiego
05-03-2003, 11:31 PM
Hi,
I hate to be a bother, but PHP is completely new to me. I was wondering if you could show me how to implement this into a simple form? I know you are probably really busy - but I do appreciate the help a lot!

Will I be able to make both those actions with one click of a submit button?

(The form pasted below isn't the one I will be using, I have about 50 fields in the form I am building but here is a quick example:)

< form method="POST" action="--WEBBOT-SELF--" >
< p >Item 1 is a <input type="text" name="T1" size="20" >< /p >
< p >Item 2 is a < input type="text" name="T2" size="20" >< /p >
< p >Item 3 is a < input type="text" name="T3" size="20" >< /p >
< p >< input type="submit" value="Submit" name="B1" >< input type="reset" value="Reset" name="B2" >< /p >
< /form >

I wasn't sure if this form would compile the html as written so I put spaces in between the codes.


Your help is very very much appreciated!!!!!

- Lenny

missing-score
05-03-2003, 11:37 PM
Although I dont need to know, what are you trying to do?

Mainly so it will give me a better idea of how you need the data outputted. What are the 50 inputs for, and what do they do?

lennydiego
05-04-2003, 12:13 AM
That's the thing (as im sure you already can tell! Im not a programmer). I am actually a support technician and I am trying to build this to manage hardware changes. It is really simple but it will definitley do the trick.

Basically, everytime we deliver or sell a new piece of hardware we have to fill out this paper form and it gets copied and then stored in two places. The first, it goes to a manager who has to key it into a program, and then second into a filing cabinet for archives.

This way we have soft copy archives (better for searching) and and e-mail goes directly to the manager and he/she can just copy and paste the information into the database program. (I cant touch the database, so this is thebest solution at this time).

thanks,
- Lenny

lennydiego
05-04-2003, 12:16 AM
This is how I would like the output to look (for both the email and the file that is saved)

It will look something like this, but not totally as Im sure the form will change as I go.


Transaction Number: (input text variable)
Date :
Completed By:
Completed For:

Hardware Serial Number:
Hardware Make:
Hardware Model:

etc.. the list goes on and on...

thanks again!
- Lenny

missing-score
05-04-2003, 10:35 AM
Firstly, set all your form fields names as what they are, so:

<input type="text" name="Transaction Number" /> and so on....

Then, would do this. (Ensure you have <form method="post">)



<?php

$output = "Hardware/Software Transaction\n\n"; // Change this, but ensure it ends with \n\n.

foreach($_POST as $key => $value)
{
$output .= $key.": ".$value."\n"; // Ensure this ends with at least one \n.
}

/* That foreach loop finds the values of all the POST vars and adds them to the output. */

$output .= "\nThankyou for ordering!!!\n"; // You can add a final message at the end. remember \n is a new line

// Now set up e-mail...

$to = "anemail@address.com";
$from = "youremail@....com";
$subject = "Hardware/Software Transaction";
$message = $output;
$header = "From:".$from."\n";

if(mail($to, $subject, $message, $header))
{
echo "Message was sent to ".$to;
}
else
{
echo "Message sending failed";
}

$ext = ".html"; // File extension

$filename = "transaction".$_POST['Transaction Number'].$ext; // Assuming transaction number will be unique.

if(!file_exists($filename))
{
chmod(touch($filename), 777);
echo "File Was Created";
$file = fopen($filename, "w");
if(fputs($output, $file))
{
echo "File data was written";
}
fclose($file);
}
else
{
echo "The File Exists, it will not be created.";
}

?>


Hopefully that will work for you. If you need anything more explaining, just ask :thumbsup:

silky
05-04-2003, 07:34 PM
I have even had support at my host try and they cant seem to get my html pages to connect to the database. They say they fix it and it works and then i go there and it doesn't -- it was working fine before I moved to another server. Ive gotten from simple codes to rather elaborate codes and none work. Im still waiting for the right code. (www.yourhouston.net/editlisting.php)

Thanks

lennydiego
05-05-2003, 01:07 AM
thanks for the help!!

I will reply once im finished to let you know how it goes

thanks again,

- lenny