# Attaching files

**URL:** <https://forum.kirupa.com/t/attaching-files/57149>\
**Category:** Uncategorized\
**Created:** [July 10, 2004, 8:23pm UTC](https://forum.kirupa.com/t/attaching-files/57149 "2004-07-10T20:23:18Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![opel](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/opel/32/199_2.png) [@opel](https://forum.kirupa.com/u/opel)\
**Post date:** [July 10, 2004, 8:23pm UTC](https://forum.kirupa.com/t/attaching-files/57149/1 "2004-07-10T20:23:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 10, 2004, 10:13pm UTC](https://forum.kirupa.com/t/attaching-files/57149/2 "2004-07-10T22:13:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 11, 2004, 4:03am UTC](https://forum.kirupa.com/t/attaching-files/57149/3 "2004-07-11T04:03:54Z")

</div>

Something like this maby ?

sending mime mail, you will need PEAR for this [www.pear.php.net](http://www.pear.php.net) i believe it is.

```php

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);

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 11, 2004, 3:18pm UTC](https://forum.kirupa.com/t/attaching-files/57149/4 "2004-07-11T15:18:53Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 11, 2004, 3:22pm UTC](https://forum.kirupa.com/t/attaching-files/57149/5 "2004-07-11T15:22:01Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 11, 2004, 3:25pm UTC](https://forum.kirupa.com/t/attaching-files/57149/6 "2004-07-11T15:25:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 11, 2004, 6:10pm UTC](https://forum.kirupa.com/t/attaching-files/57149/7 "2004-07-11T18:10:48Z")

</div>

You don’t need Pear to send Mime mails:

```php

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

// 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 .= "
";
$message .= "--".$mime_boundary."
";

// 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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 11, 2004, 10:13pm UTC](https://forum.kirupa.com/t/attaching-files/57149/8 "2004-07-11T22:13:51Z")

</div>

so my html form would look like this?

```auto
<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>

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 11, 2004, 10:26pm UTC](https://forum.kirupa.com/t/attaching-files/57149/9 "2004-07-11T22:26:10Z")

</div>

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:

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

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 11, 2004, 10:51pm UTC](https://forum.kirupa.com/t/attaching-files/57149/10 "2004-07-11T22:51:12Z")

</div>

You’d put

```auto

<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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 12, 2004, 9:53am UTC](https://forum.kirupa.com/t/attaching-files/57149/11 "2004-07-12T09:53:17Z")

</div>

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](mailto:myname@mydomain.com)\>  
”;

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 13, 2004, 6:45pm UTC](https://forum.kirupa.com/t/attaching-files/57149/12 "2004-07-13T18:45:44Z")

</div>

Ok I’ve got this script so far that will use and external mimemail.php script to excecute:

if I include this script:

```php
<?php $mailVars['to'] = $_POST['email'];
  $mailVars['toname'] = $_POST['name'];
  $mailVars['from'] = [email]yourname@yourdomain.com[/email];
  $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?
