Attaching files

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

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.

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']= "[email protected]{$_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);

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?

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?

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

You don’t need Pear to send Mime mails:


$mime_boundary = "<<<--==+X[".md5(time())."]";
$headers = "From: My Name <[email protected]>
"; 
$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("[email protected]", "Subject", $message, $headers);

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

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="[email protected]" />
<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>

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>

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.

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 <[email protected]>
”;

and

$ok = mail("[email protected]", “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

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'] = [email][email protected][/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?