Attaching and Embedding Local Files

My Advanced Form2Mail Script (which you may wish to purchase) allows you to attach files to emails generated from forms as well as embedding images into HTML emails. To specify the files to be attached or embedded you specify a comma separated list of the locations of the files in the attach and/or embed parameters either in hidden fields in the form or within the include file that contains the definition for your email formatting. These filenames can be either specified relative to the location of the email script or can use the absolute address of the file on the server. If allow_url_fopen is on in your PHP implementation then files can also be referenced from other servers using their full URL (commencing http://)

Okay you want to allow your visitors to attach or embed files that are on their computer rather than on the server but the attach and embed fields specify filenames on the server. Does this mean that you can't use my script? No, it just means that we need to give our visitor a way to upload their file to the server temporarily so that it can be attached or embedded.

To do this requires two small changes to the way that we use the email script. First we need to make a couple of changes to the form as shown here in bold.

<form enctype="multipart/form-data" action="email.php" method="post">
<input type="hidden" name="include" value="attach.php" />
<input type="file" name="userfile" />


The field (or fields) with type="file" allow your visitors to select files from their computer and upload them to the server where they are passed to the email script along with the other input fields. The enctype (encryption type) parameter is normally optional on these forms but must be supplied when you upload files or the whole thing wont work.

The hidden include field references a file called attach.php which will call the code to load the name of the temporarily uploaded file into the attach or embed text fields as well as validating that the file actually exists. If we already have an include file then we don't need (and can't have) a second one we just add the following code into the existing include file instead of creating attach.php.

include "efopt.php";
$attach = load_file('userfile','');

If you want to embed rather than attach just change the $attach references to $embed. The load_file function can be used to append additional files to the $attach field by passing the existing content of $attach as the second parameter eg.
$attach = load_file('userfile2',$attach);
and similarly for $embed.

Note that your server must allow temporary file uploads (file_uploads = 1) for the above solution to work.
Allowing your visitors to send emails from your site with files attached means that your form can potentially be used to send viruses. Felgall Pty Ltd accepts no responsibility for any viruses sent using forms that use the above code. We recommend that you do not set up forms that allow your visitors to send files from their computer to a third party.


This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow