PDA

View Full Version : attaching files



opel
July 10th, 2004, 05:23 PM
I'm creating a form that will mail in php and I want the users to be able to attach a ".doc" filea before they send the e-mail.

can any one point me in the direction of a script? I tried looking on hotscripts but I don't know what category it would come under and the search didnt come up with anything i wanted.

thanks

Hans Kilian
July 10th, 2004, 07:13 PM
I don't know of a finished script. But it shouldn't be too hard to code.

First you need to create an HTML form that'll allow you to write a message and upload a file.

The you have to create a script that'll take the uploaded file and create a multipart MIME email with the message and the file.

ol4pr0
July 11th, 2004, 01:03 AM
Something like this maby ?

sending mime mail, you will need PEAR for this www.pear.php.net i believe it is.


set_time_limit(1200);
require("Mail.php");
$headers['From']= "support@{$_SERVER['SERVER_NAME']}";
$headers['Subject'] ="Something ----";
$to = ($_REQUEST['email']);
$mime= new Mail_mime
$text = "Thank you for whatever '".$_POST['formfieldname']."'";
$mime->setTXTBody($text);
$html='<html><body>Something for you to experiment with</body></html> ';
$mime->setHTMLBody($html);
$file='/path/to/sometextfile.txt';
$mime->addAttchement($file, 'plain/text');
$header=$mime->headers($headers);
$body=$mime->get();
$message=& Mail::Factory('mail');
$message->send($to,$headers,$body);

opel
July 11th, 2004, 12:18 PM
Ok I did my best to understand the manual but let me get this right.

PEAR is a wizard/bundle program that contains lots of PHP scripts?

I have downloadeded but told me I have to insert a address into my "include path" which I dont know where that is as I'm running a WAMP testing server?

opel
July 11th, 2004, 12:22 PM
Do I install pear on the site I would like to use the resuable PHP files on and then look for an "atttach file" script like ol4pr0 has suggested?

ol4pr0
July 11th, 2004, 12:25 PM
If you want to send file with mail, you will need mime for as far as i know. its easyer to install the pear from the prompt.
shell>cd /dir/of/pear.bat
shell>run pear.bat

Hans Kilian
July 11th, 2004, 03:10 PM
You don't need Pear to send Mime mails:


$mime_boundary = "<<<--==+X[".md5(time())."]";
$headers = "From: My Name <myname@mydomain.com>\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed;\r\n";
$headers .= " boundary=\"".$mime_boundary."\"";
$message = "This is a multi-part message in MIME format.\r\n";
$message .= "\r\n";
$message .= "--".$mime_boundary."\r\n";
$message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n";
$message .= "Content-Transfer-Encoding: 7bit\r\n";
$message .= "\r\n";
$message .= "This is the text part of the message. \r\n";
$message .= "Here is the attached file: \r\n";
$message .= "--".$mime_boundary."\r\n";
$message .= "Content-Type: application/octet-stream;\r\n";
$message .= " name=\"" . $_FILES['userfile']['name'] . "\"\r\n";
$message .= "Content-Transfer-Encoding: quoted-printable\r\n";
$message .= "Content-Disposition: attachment;\r\n";
$message .= " filename=\"" . $_FILES['userfile']['name'] . "\"\r\n";
$message .= "\r\n";

// Now for the actual file which should be sent base64 encoded:
$handle = fopen($_FILES['userfile']['tmp_name'], 'rb');
$file_content = fread($handle,filesize($_FILES['userfile']['tmp_name']));
fclose($handle);
$encoded = chunk_split(base64_encode($file_content));
$message .= $encoded;
$message .= "\r\n";
$message .= "--".$mime_boundary."\r\n";

// Finally send the mail
$ok = mail("you@yourdomain.com", "Subject", $message, $headers);


This script assumes that you name the input file field 'userfile' in your HTML.

opel
July 11th, 2004, 07:13 PM
so my html form would look like this?


<form enctype="multipart/form-data" action="userfile.php" method="post" name="cv form">
<input name="recipient" type="hidden" value="contact@discogunk.co.uk" />
<input name="subject" type="hidden" value="Ultraforce C.V.enquiry" />
<table width="482" height="423" border="0">
<tr>
<td>First Name</td>
<td colspan="2"><input name="first name" type="text" size="25" maxlength="25" /></td>
</tr>
<td>Surname</td>
<td colspan="2"><input name="surname" type="text" size="25" maxlength="25" /></td>
</tr>
<tr>
<td>Your Address</td>
<td colspan="2"><textarea cols="38" rows="3" name="Address"></textarea></td>
</tr>
<tr>
<td>Your Postcode</td>
<td colspan="2"> <input type="text" name="Postcode" size="9" /></td>
</tr>
<tr>
<td>Your E-mail</td>
<td colspan="2"><input type="text" name="email" size="50"/></td>
</tr>
<input type="hidden" name="required" value"Email" />
<tr>
<td>Your Telephone Number</td>
<td colspan="2"><input type="text" name="Telephone" size="50"/></td>
</tr>
<tr>
<td>Optional Message</td><td colspan="2"><textarea cols="38" rows="5" name="Message"></textarea></td></tr>
<tr>
<td>Onlive C.V. address</td>
<td colspan="2"><input type="text" name="online c.v." size="50"/></td>
</tr>
<tr>
</tr>
<tr><td>&nbsp;</td>
<td colspan="2" class="bottom"><input type="reset" name="Reset" value="Reset form" />
<input type="submit" name="Submit" value="Send form" /></td>
</tr>
</table>
</form>

opel
July 11th, 2004, 07:26 PM
Let me get this clear in my head.

I would create my form in HTML as usual, I am using the mailer script/tutorial from this site.

Then in the text field I want users to be able to attach a file to I put the following code:


<td>Attach C.V.</td>
<td><input type="hidden" name="userfile" /><br />
<input name="userfile"; type="file"/>
</td>

Hans Kilian
July 11th, 2004, 07:51 PM
You'd put


<form action="sendcv.php" enctype="multipart/form-data" method="post">
<table><tr>
<td>Attach C.V.</td>
<td><input name="userfile" type="file" /></td>
</tr></table>
</form>

No need for the hidden field. Also be sure to have enctype="multipart/form-data" in your form-tag.

opel
July 12th, 2004, 06:53 AM
thanks I inserted the script at the top of my form page and saved it as a php. then changed my form enctype to "multipart/form-data". and method as POST. Also changed

$headers = "From: My Name <myname@mydomain.com>\r\n";

and

$ok = mail("you@yourdomain.com", "Subject", $message, $headers);

so that they included my e-mail address

Now my server has gone down I am waiting for it to go back up to test. Will let you know how I get on.

Thanks

opel
July 13th, 2004, 03:45 PM
Ok I've got this script so far that will use and external mimemail.php script to excecute:

if I include this script:


<?php $mailVars['to'] = $_POST['email'];
$mailVars['toname'] = $_POST['name'];
$mailVars['from'] = yourname@yourdomain.com;
$mailVars['fromname'] = "andrew";
$mailVars['subject'] = "Subject of the message you want to send";
// message
$mailVars['html'] = '<div style="font-family: Verdana, Arial, Helvetica:
11px; color: #0000CC;">Hello Name<br>
<br>
Thank you for filling out the form<br>
<br>
All the best, You<br></div>
';

include_once("mimemail.php");
if (mimemail($mailVars)){
//anything you want to do upon succesful message
} else {
// when sending went wrong;
}

?>

schould I put this at the top of my form page and "name" each input field to correspond with the variable in the php e.g. to, toname, from, from name. although I want to keep to constant?