Attachments in form mail

I am not sure this is the appropriate forum for this question but I’ll give it a shot and maybe someone can help :slight_smile:
What I would like to do is allow my members to submit their photos. I have a form created and it works fine except for the browse for a photo. You can browse and it appears to work but when I receive their email all it shows is the directory in which the file was in on their system. This is the code I have within the form;

Any suggestions on what I am doing wrong?

This is probably not the correct forum, no, maybe it will be moved to “Programming”.

How do you see yourself recieving these files? It looks to me like you need a File Upload script that will upload the files to temporary space on your server which you can then access.

There are many such scripts available freely at

Good luck, and let us know if you need further information.

  • wil

[quote]This is probably not the correct forum, no, maybe it will be
moved to “Programming”.



  • Jeff @ DreamHost
  • DH Discussion Forum Admin

This is the code I have within the form;

Any suggestions on what I am doing wrong?

First in the HTML, the FORM element must be like so:


[/code]This will cause the browser to send the file to the web server.

A script must be written specifically to recieve the file, too - is your formmail script capapble?

I don’t know how this is done in PHP, but it Perl its done easily enough with the CGI module. The filename the browser sends may or may not include the path on their disk, you would want to strip path information if present (or just name it yourself). To prevent someone from tampering with your site, you definitely need to run the filename through a filter and check if the file exists already.

I am using DreamHost’s form mail…all I really wanted was to receive a member’s picture through the email form so I can post it in the members profile. I am sure this would be a lot easier using a database but I’m really ignorant on that and I need my site completed ASAP so I will just have to manually input all the data. It seems there is a cgi script named “mail-attach.cgi” but how I can get that I have no idea.
I appreciate the help though :slight_smile: doesn’t mention file attachments. Anyways, here is a script you can use, mostly as an example, but it does work. Just change the necessary variables. This doesn’t require a database, and it uses a temporary file so you don’t have to worry about files either. Don’t forget to upload as ASCII and CHMOD it 755.

Additionally you can just cut & paste the form from the script into another page, and just change the action attribute to point to this script.


code released into public domain, provided as-is

use CGI;
use CGI::Carp qw(fatalsToBrowser);
use MIME::Base64;


my $CGI = new CGI;
my $script = $ENV{‘SCRIPT_URI’};
my $recipient = “Recipient <>”;
my $sender = “Sender <>”;
my $subject = ‘Profile Submission’;

my $sendmail = ‘|/usr/sbin/sendmail -t’;

print $CGI->header, $CGI->start_html;

if (&recieve_upload) {
print qq(

Upload successful, e-mail sent.

else {
print qq(

Upload unsuccessful.

else {

print $CGI->end_html;



sub recieve_upload {

my ($field, $value, %form);

retrieve form fields and trim whitespace

foreach $field (qw(filename name address)) {
$value = $CGI->param($field);
if (length $value > 0) {
for($value) {
s/\s+/ /g;
$form{$field} = (length $value > 0) ? $value : undef;

remove path from filename and make it safe.

for($form{‘filename’}) {

abort if wrong file extension

return undef unless $form{‘filename’} =~ /.jpe?g$/i;

my $boundary;
my @charset = (‘0’…‘9’, ‘A’…‘Z’, ‘a’…‘z’,’+’,’=’);
while (length $boundary < 24) { $boundary .= $charset[int(rand(64))] }

open($ph, $sendmail);

message headers

print $ph
qq(To: $recipient\n),
qq(From: $sender\n),
qq(Reply-to: $form{‘name’} <$form{‘address’}>\n),
qq(Subject: $subject\n),
qq(Mime-Version: 1.0\n),
qq(Content-Type: multipart/mixed; boundary="$boundary"\n),
qq(Content-Disposition: inline\n\n);

text/plain part

print $ph
qq(Content-Type: text/plain; charset=us-ascii\n),
qq(Content-Disposition: inline\n\n),
qq(Form submission from $form{‘name’} <$form{‘address’}>\n\n);

image part - skip if not uploaded

if ($fh = $CGI->upload(‘filename’)) {

print $ph
qq(Content-Type: image/jpeg\n),
qq(Content-Disposition: attachment; filename=$form{‘filename’}\n),
qq(Content-Transfer-Encoding: base64\n\n);

my $buf;
while (read($fh, $buf, 60*57)) {
print $ph encode_base64($buf);
else {
print $ph “No photograph was submitted.\n”;

print $ph

return 1;

sub upload_form {

output form - note the enctype attribute.

print <<HTML;

The following fields are required.


E-mail Address:


_HTML_ return; } ------------------------------------------------------------ [/code]