email form with attachment

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mandocraig
Forum Newbie
Posts: 2
Joined: Thu Apr 22, 2004 9:08 am

email form with attachment

Post by mandocraig »

hello! this is my first post here.

i've read the PHP manual at http://www.php.net and understood about 1% of it.

my minimal experience with HTML has been that the best way to learn is to just play around with it. i'd like to find the code that would allow me to create a form with an attachment and can be e-mailed.

can someone post the PHP code that would send a form with three fields:
1) text1, 2) text2, and 3) file upload to my e-mail address? from there i figure i can play around and modify it to my needs and hopefully learn something about PHP.

cheers,

craig
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Try using search.php and search for "email file attachments" for more comments, ideas and code.
mandocraig
Forum Newbie
Posts: 2
Joined: Thu Apr 22, 2004 9:08 am

Post by mandocraig »

JAM,

thanks. i did try the search function and other forums as well. nowhere have i found the barebones code for this sort of thing. most codes are so complex it's impossible to dissect out the basics. i know this can't be as complicated as it seems . . .
User avatar
code_monkey
Forum Newbie
Posts: 16
Joined: Tue Jul 08, 2003 6:13 am
Location: UK
Contact:

Post by code_monkey »

Ok, hopefully this stuff all makes sense, bit basic but it should work, right first off on the page where your form is you want something like this

Code: Select all

<!--application form-->
		<script language="JavaScript">
		<!--
		// start validation

		function isEmail(elm)
			{
			if (elm.value.indexOf("@") + "" != "-1" &&
				elm.value.indexOf(".") + "" != "-1" &&
				elm.value != "")
			return true;
			else return false;
			}

		function isFilled(elm) {
			if (elm.value == "" ||
				elm.value == null)
			return false;
			else return true;
			}

		function isReady(form) {

    		if (isEmail(form.EmailAddress) == false) {
				alert("Please enter a correct email address.");
				form.EmailAddress.focus();
				return false;
				}


		}
		// end validation
		//-->
		</script>

		<form method="post" action="email-attachment.php" enctype="multipart/form-data" onsubmit="return isReady(this)">
		<input type="hidden" name="MAX_FILE_SIZE" value="2048000">
		<table border="0" cellpadding="0" cellspacing="2">
		<tr>
			<td align="left" class="text" valign="top">
			Your email address:
			</td>
			<td align="left" valign="top">
			<input type="text" name="EmailAddress" size="44" value="">
			</td>
		</tr>
		<tr>
			<td align="left" class="text" valign="top">
			Please attach your file:
			</td>
			<td align="left" valign="top">
			<input type="file" name="attachment" value="" size="24">
			</td>
		</tr>
			<td>&nbsp;</td>
			<td><input type="submit" name="submit" value="send"></td>
		</table>
The javascript there just validates the email address.

On the page recieving the forms contents and send the email, you want something a bit like this.

Code: Select all

$message = stripslashes ("Some text");

	//add From: header 
	$headers = "From: $EmailAddress\r\n"; 

	//specify MIME version 1.0 
	$headers .= "MIME-Version: 1.0\r\n"; 

	//unique boundary 
	$boundary = uniqid("HTMLDEMO"); 

	//tell e-mail client this e-mail contains//alternate versions 
	$headers .= "Content-Type: multipart/mixed" . 
		   "; boundary = $boundary\r\n\r\n"; 

	//message to people with clients who don't 
	//understand MIME 
	$headers .= "This is a MIME encoded message.\r\n\r\n"; 

	//plain text version of message 
	$headers .= "--$boundary\r\n" . 
	   "Content-Type: text/plain; charset=ISO-8859-1\r\n" . 
	   "Content-Transfer-Encoding: base64\r\n\r\n"; 
	$headers .= chunk_split(base64_encode($message)); 

	// get attachment file details from the post
	
	$file_array = $_FILES[attachment];
	$file_type = $file_array[type];
	$filename = $file_array[tmp_name];

	// read file into a variable
	
	$fd = fopen ($filename, "r");
	$file = fread ($fd, filesize ($filename));
	fclose ($fd);

	$filename = $file_array[name];

	// encode file for the email
	
	$data = chunk_split(base64_encode($file)); 

	//File Attachment
	
	$headers .= "--{$boundary}\n" .  
	                  "Content-Type: {$file_type};\n" .  
	                  " name="{$filename}"\n" .  
	                  "Content-Transfer-Encoding: base64\n\n" .  
	                 $data . "\n\n" .  
	                  "--{$boundary}--\n";  

	// send email
	                  
	$success = mail("recipient@somenet.co.uk", "Email with attachment", "", $headers);
and that should send an email with an attachment, you'll need to play around with the $message variable and set it to your own text and of course update the recipient@somenet.co.uk to your email address, but that should do it.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Alternatively, you can use the excellent phpmailer.
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

hehe, i remember reading this post ages ago,
no sweat guys, someone solved it
Post Reply