Jump to content
xisto Community
ginginca

Question About An Email Form

Recommended Posts

Hello fellow astahostians!

I made an email form for my site, where I want customers to be able to send me a file from their computer.

There's a number of fields (name, email etc.) which are processing through to my email just fine.

I want to be able to receive their attachment, either as an attachment to my email, or to FTP it to my server, whichever is more straight forward for me to do.

I have the form completed, and the field where they choose and select their file. Here's what I "don't" quite get ... what type of instructions to I need in the PHP, so their file ends up coming to me.

Here is part of my code:

if (isset($_POST['sendnow'])){$from = $_POST['fromemail'];$fromname = $_POST['fromname'];$frompaypalemail = $POST['frompaypalemail'];$file = $POST['file'];$tel = $_POST['tel'];$message = $_POST['message'];// extra info to add to message$address = gethostbyaddr($_SERVER['REMOTE_ADDR']);$ip = gethostbyname($_SERVER['REMOTE_ADDR']);$timestamp = time ();$filename =  $_SERVER['PHP_SELF'];$location = $_SERVER['SERVER_NAME'].$filename;// adding$messagewithinfo = "$message\n\n$fromemail\n\n$fromname\n\n$frompaypalemail\n\n$tel\n\n\n\nAdditional info:\nIP = $ip\nAddress = $address\nTime at server = $timestamp\nSent with this page = $location\n";mail($to, $subject, $messagewithinfo, "From: $fromname <$fromemail>\r\n" . "Reply-To: $from\r\n" . "X-Mailer: PHP/" . phpversion());echo("$confirm");}else{?><form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data" name="emailform">


My variable name for the attachment is called FILE.

So here's my question ... what do I add to this, to be able to receive the file? As I said ... whether it goes onto my server and I download it manually, or whether it is attached to an email ... doesn't matter either way.

Thank you for your help!

Gin

Share this post


Link to post
Share on other sites

I've had a look at attachments, and it seems horribly complicated. The best way might be to store it on the server. Keep in mind that you'll need some sort of limitation, or they'll use up all your space. I don't know if you already have that in your site, but if you don't you should add it.

I'd use some sort of code like the following (inserted beside the mail function in your script:

$filesplit = explode("\\", $file); //Split file to find extension$f = fopen("uploaded_$from_".time()."_".$filesplit[count($filesplit)-1], "xb") or die("Could not upload file."); //Create file with name "uploaded_person_time_filename.ext" and end if failedfwrite($f, implode("", file($file))); //Write file as string to new filefclose($f);//Close file
I have not tested the code, so if it doesn't work you can try to figure out the problem or you can tell me what's wrong. Good luck.

Share this post


Link to post
Share on other sites

I've had a look at attachments, and it seems horribly complicated. The best way might be to store it on the server. Keep in mind that you'll need some sort of limitation, or they'll use up all your space. I don't know if you already have that in your site, but if you don't you should add it.

 

I'd use some sort of code like the following (inserted beside the mail function in your script:

$filesplit = explode("\\", $file); //Split file to find extension$f = fopen("uploaded_$from_".time()."_".$filesplit[count($filesplit)-1], "xb") or die("Could not upload file."); //Create file with name "uploaded_person_time_filename.ext" and end if failedfwrite($f, implode("", file($file))); //Write file as string to new filefclose($f);//Close file
I have not tested the code, so if it doesn't work you can try to figure out the problem or you can tell me what's wrong. Good luck.

 


I own the server and there's a nice big hard drive in it. Besides, the files will be removed once I get notification they are there (they will pretty much be sending PDF's my way.)

 

I don't see any FTP account vars. BTW I noticed on FTP.net, the FTP_PUT command. Does THAT work in this case?

 

 

Here's the script from their site:

 

<?php$file = 'somefile.txt';$remote_file = 'readme.txt';// set up basic connection$conn_id = ftp_connect($ftp_server);// login with username and password$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);// upload a fileif (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {echo "successfully uploaded $file\n";} else {echo "There was a problem while uploading $file\n";}// close the connectionftp_close($conn_id);?>

I don't know how I would put this into my existing mail script, and if I can point to specific FOLDER, or what that line $remote_file = 'readme.txt'; has to do with it.

Share this post


Link to post
Share on other sites

The way i prefer to do this kind of stuff is to first store the submited file to a specific folder at the server with the correct permissions, if it is a *nix system you must set this folder to be 777 or 775 both works fine, if it is a win system you must set this folder to be read/write, then send a notification email to any email account you use, and finally use any ftp client to your upload folder to check the submitted file.

Something like this will work:

if($_FILES['file']['size'] == 0) {	die("Error... Any file was submitted"	. "<br /><br /><a href=\"java script:history.back();\">Go back and try again</a>" );	exit();}else {	$nomfile=$_FILES["file"]["name"];	move_uploaded_file($_FILES['file']['tmp_name'], "your_folder_name/" . $nomfile);	$file = $_SERVER["DOCUMENT_ROOT"] . "/your_folder_name/" . $nomfile;	@chmod($file, 0755); // is enough if you chmod 644}
Other considerations you can use are to limit the size of the submitted files, its file type, etc.

You can see more info related to this at the php.net manual for file-upload functionallity

Best regards,

Share this post


Link to post
Share on other sites

Hope these links help ya ;)

 

Advanced Blue Voda Form Processor - Freeware

 

HotScripts - All possible scripts that would do your attachment job.

Advanced Scripts - Again .. all possible scripts that would do your attachment job.

 

And finally this is what i found close to what you want .. You can view the specific forum topic >>HERE<< .. and follow up through the rest of the post if you run into further problems.

 

<? /* Mailer with Attachments */  $action = $_REQUEST['action']; global $action;  function showForm() { ?>    <form enctype="multipart/form-data" name="send" method="post" action="<?=$_SERVER['PHP_SELF']?>">   <input type="hidden" name="action" value="send" />   <input type="hidden" name="MAX_FILE_SIZE" value="10000000" /> 	<p>Recipient Name:  	Recipient Email: <input name="to_email" size="50" /> 	From Name:  <input name="from_name" size="50" /> 	From Email:  <input name="from_email" size="50" /> 	Subject:  <input name="subject" size="50" /> 	Message: <textarea name="body" rows="10" cols="50"> 	Attachment: <input type="file" name="attachment" size="50" /> 	<input type="submit" value="Send Email" />    <? }  function sendMail() {   if (!isset ($_POST['to_email'])) { //Oops, forgot your email addy! 	die ("<p>Oops!  You forgot to fill out the email address! Click on the back arrow to go back");   }   else { 	$to_name = stripslashes($_POST['to_name']); 	$from_name = stripslashes($_POST['from_name']); 	$subject = stripslashes($_POST['subject']); 	$body = stripslashes($_POST['body']); 	$to_email = $_POST['to_email']; 	$attachment = $_FILES['attachment']['tmp_name']; 	$attachment_name = $_FILES['attachment']['name'];  	if (is_uploaded_file($attachment)) { //Do we have a file uploaded? 	  $fp = fopen($attachment, "rb"); //Open it 	  $data = fread($fp, filesize($attachment)); //Read it 	  $data = chunk_split(base64_encode($data)); //Chunk it up and encode it as base64 so it can emailed 		fclose($fp); 	} 	//Let's start our headers 	$headers = "From: $from_name<" . $_POST['from_email'] . ">n"; 	$headers .= "Reply-To: <" . $_POST['from_email'] . ">n";  	$headers .= "(anti-spam-mime-version:) 1.0n"; 	$headers .= "(anti-spam-content-type:) multipart/related; type="multipart/alternative"; boundary="----=MIME_BOUNDRY_main_message"n";  	$headers .= "X-Sender: $from_name<" . $_POST['from_email'] . ">n"; 	$headers .= "X-Mailer: PHP4n"; 	$headers .= "X-Priority: 3n"; //1 = Urgent, 3 = Normal 	$headers .= "Return-Path: <" . $_POST['from_email'] . ">n";  	$headers .= "This is a multi-part message in MIME format.n"; 	$headers .= "------=MIME_BOUNDRY_main_message n";  	$headers .= "(anti-spam-content-type:) multipart/alternative; boundary="----=MIME_BOUNDRY_message_parts"n";  	 	$message = "------=MIME_BOUNDRY_message_partsn"; 	$message .= "(anti-spam-content-type:) text/plain; charset="iso-8859-1"n";  	$message .= "Content-Transfer-Encoding: quoted-printablen";  	$message .= "n";  	/* Add our message, in this case it's plain text.  You could also add HTML by changing the Content-Type to text/html */ 	$message .= "$bodyn"; 	$message .= "n";  	$message .= "------=MIME_BOUNDRY_message_parts--n";  	$message .= "n";  	$message .= "------=MIME_BOUNDRY_main_messagen";  	$message .= "(anti-spam-content-type:) application/octet-stream;ntname="" . $attachment_name . ""n"; 	$message .= "Content-Transfer-Encoding: base64n"; 	$message .= "Content-Disposition: attachment;ntfilename="" . $attachment_name . ""nn"; 	$message .= $data; //The base64 encoded message 	$message .= "n";  	$message .= "------=MIME_BOUNDRY_main_message--n";   	// send the message 	mail("$to_name<$to_email>", $subject, $message, $headers);  	print "Mail sent.  Thank you for using the MyNewName5333 Mailer.";   } }  print <<< EOT <?xml version="1.0" encoding="iso-8859-1"?>  EOT; ?>  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 	"http://forums.xisto.com/no_longer_exists/; <html xmlns="http://forums.xisto.com/no_longer_exists/; xml:lang="en" lang="en">   <head> 	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 	<style="css" type="text/css"> 	  <!-- 	  body { 		margin: 0px; 		font-family: Verdana, Arial, Helvetica, sans-serif; 		font-size: 12px; 	  } 	  a {color: #0000ff} 	  --> 	</style>   </head>   <body>  <? switch ($action) {   case "send": 	sendMail(); 	showForm(); 	break;   default: 	showForm(); } ?>    </body> </html>
I am no programming guru, but hope this helps ;)

 

Regards

Dhanesh.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.