For a long time I have used a contact form that I have created, which is very simple. Recently I had to create a form that also allows for an attachment. The form I created is based on:
- Scott Lucht’s phMailer
- Advanced email in PHP by Kevin Yank on Site Point
- PHP: Sending Email (Text/HTML/Attachments) on webcheatsheet.com
In any case, here is my version of the PHP email attachment form. Simple to implement in your web page. Allows one file to be attached to the email. Performs validation of: name, email, subject and message. Also validates certain file types and maximum size. No need for PHP knowledge. The script is well commented. Form is styled using CSS.
This script is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation. This script is distributed in the hope that it will be useful, but without any warranty. I hope you find it useful.
Form Installation Instructions:
- You need to include (or require) the fns.php at the top of your page. Basically copy/paste the top and bottom of the index.php to your page (whatever is within the php tags) and adjust the path/to/fns.php
- Copy/paste the code within the #uploadform div to where you want it to be on your page
- Change the email address in the fns.php. There are a few other variables you can adjust if you wish around the same place as the email variable
Download
Update
A couple of changes to the script:
- The form now remembers values entered by user and pre-fills form fields if there is an error. If the page gets refreshed, the session data is destroyed.
- I added a variable to make file uploading required.

Comments
Hello Design Elle ;)
I have one question. First sorry for my english and my php knowlege ;)
but how can i make this form work without Fileupload requiered field ?
i just put the code for fileupload away. All I need is
Name: (requiered) *
E-Mail: (requiered) *
Subject: (requiered) *
Comment: (requiered) *
how did you get to this site?: (not requiered)
these are my fields I need, so the other field?s like "fileupload" or "phonenumber" i dont need, but it wont work when the fileupload field is missing in the code i think. I recivied an E-Mail but the Comment text is not sending, i only get the E-Mail from who is sending it and the Subject but the comment?s are missing.I hope you understand me. ;)
— Its ME , May 09, 2008
Here’s the version of the script that is just a contact form, with no file upload. You will need to change the two functions in fns.php to:
1. Change the phone field to “How did you get to this site?” Lines 14 and 15
2. Change the $phone var on line 38 according to what you specified in the form
3. Change the $phone var on line 43 accordingly.
Hope this helps
— Elle , May 09, 2008
wow !
thanks for the fast response ;)
i will try it and tell you if i get it
:D
— Its ME , May 09, 2008
Ok all things are fine now !
it works !!
i have make those changes you tell me....
thank you very much.... :D
Just one thing. When I fill out the Form and click on Send then comes the "Thank you" message. And when I refresh the Site (where the "thank you" message is there), then I get for every refresh I make the same E-Mail. That means I click 5 times refresh and i get 5 E-Mails. I just wants to report you this feature ;)
— Its ME , May 09, 2008
Wow! this is amazing script, I have a small doubt, how can I do to that instead of the file is sent by email you stay in a folder. I hope can help me, I very much appreciate your time and sorry for my basic knowlege about php.
— people , May 10, 2008
Finally a working and nice looking mailer with attachments, my week long searching has finally ended here.
There are just two things that could make this script better :
- When there is an error in one of the fields you get a nice error message which is cool but you have to fill out the fields again (or click back in the navigator) I am sure this is not hard to change, maybe just a javacript 'back' ...
- Can you please make a multi-file upload version, that would be so cool.
— Donovan , May 11, 2008
Hi Donovan,
I will look into your first suggestion. In regards to the second one, check out phMailer (the first link above), which has multiple file uploads.
— Elle , May 12, 2008
Hi, thanks for the script, the only problem is that if I fill in the form, but decide not to upload a file, when I recieve the email it doesn't show the information I typed in the form.
Is there a way round that?
I still need the upload feature, but I wanted to see what would happen if I didn't use it.
Cheers
Ants
— Ants , May 13, 2008
Hi Elle,
1st I would like to thank you for an excellent script. I think I've tried almost all scripts available and this one is the one that works great!
One question though. I've extended the form with several fields without any problems but I can't figure out how to get the values from checkboxes or "drop down selection lists" processed.
Thanks!!
— Xander , May 15, 2008
Good point and thank you for bringing it up-all fixed. Message is now formed before the script checks if a file is attached.
Thanks
— Elle , May 15, 2008
Elle,
You can ignore my previous post. I've figured it out myself. Thanks!
— Xander , May 15, 2008
Hi. Sorry, I'm new to php,
I'm trying to add a "file attachment" function to my form... but I have a hard time understanding the instructions
I hope you can help me.
— Jason , May 26, 2008
Thx for Great Script . but how can we stop Spamers .. is there any anti-spam or captcha Code to add to this script
— omar , June 02, 2008
well thats me again , i coudlt wait for your answer so i went and modfided the script by added the captcha from another script . and it works perfect this is a demo.
http://www.addustour.net/mail/
this if somone needs the script after modfieded to have anti-spam code.
http://www.addustour.net/mail.zip
— omar , June 02, 2008
Hi Omar,
Glad you you could use my script and thank you for updating and improving it :)
— Elle , June 02, 2008
Add this just before the if statement for mail (in your script it shoud be around line 204 or 20):
<code>
$envs = array("HTTP_USER_AGENT", "REMOTE_ADDR", "REMOTE_HOST");
foreach ($envs as $env)
$message .= "$env: $_SERVER[$env]\n";
</code>
It works for me :)
HTH
— Elle , June 02, 2008
i'm still up , i have tried to add code to show the ip sender . but i coudt get it to work . i don't know what wrong i'm doing.this the code i have included to show the ip sender witht he message
<code>$message .= "IP Address: ".$_SERVER['REMOTE_ADDR']."\n";</code>
i might just did not put it on right place .. could you help me here . Thx
— omar , June 03, 2008
Hey
Thx Elle , but i have found my mistake it was '' i ihad it like ` .it was too late when i was working with it and yes i have used it around line 204 . this the edited code .
//Show Sender Ip
$ip= "The Sender Ip Address Is: ".$_SERVER['REMOTE_ADDR']."\n";
$message .= "$ip: $_SERVER[ip]\n";
//End Sender IP
But who wants more info about the sender sure it better use your code but if u want just the sender ip then use my code .
I think i will Go a head and try make a language File to make it easy to transulate it.
Thx
— omar , June 03, 2008
Hi the form is great and just what I was looking for but is there anyway I can make it allow 3 attachments.
— Rob , June 09, 2008
Thanks Rob. You should check out the phMailer script and how it allows multiple attachments.
— Elle , June 09, 2008
I did have a look at phpMailer but I have styled up my form and was just was hoping that there was an easier way to update yours, but don't worry I will try and figure it out.
— Rob , June 09, 2008
Thanks a lot, I was looking for a simple solution and a premade script, since my PHP knowledge is very poor.
I haven't used your one, but from the description it looks like that's exacly what I need.
— Sunlust , June 10, 2008
Elle,
A simply elegant and delightful implementation.
Kudos to you! and thank you for sharing!!
-Ray
— Ray , June 11, 2008
Wow, I've been looking for this forever, thank you so much for sharing this.
Im trying to have the script be able to attach images.... I replaced the other extensions just to jpg, but when I attach a jpg I DO receive it, but its always only 46 kb, and only a little part of the image is shown and the rest is gray.
Do I have to change something else? I'm not very knowledgeable in PHP, but from looking at the code, I'm not sure what else to change?
— natalia , June 11, 2008
@Ray; Thanks
@Natalia: I've just checked the script with a .jpg file and had no trouble receiving the whole file.
One thing you should remember is to be aware that there are two extensions for this format: .jpg and .jpeg
— Elle , June 11, 2008
Elle,
Thank you so much for replying to me quickly, it means a lot!!!
Well, i ended up switching to another host, to see if it was the host's problem, and I think the free host I was using had an issue, because with the new free host, I can send images up to 400kb, 600kb, and they deliver fine.
By the way, do you have any thoughts on why the other free hosting place I had issues with?
— natalia , June 12, 2008
Thanks a lot for this amazing script. (Almost) All I needed, simple, etc.
As a plus, I've done a countdown text area in the form. For those interrested, code is the following :
//At the beginning of fns.php//
echo("");
echo("function limite(textarea, max)");
echo("{");
echo("if(textarea.value.length >= max)");
echo("{");
echo("textarea.value = textarea.value.substring(0,max);");
echo("}");
echo("var reste = max - textarea.value.length;");
echo("var affichage_reste = reste;");
echo("document.getElementById(\"max_desc\").innerHTML = affichage_reste;");
echo("}");
echo("");
//Where you put the text area//
Comments (100 caracters left)
Adaptation of a script found over the internet (http://devweb.jegalere.fr/category/javascript/ in French). Thanks to both of you.
— Catherine , June 15, 2008
Fantastic! This script is great, most important of all reliable! I have had problems trying to fix my script to work in both Mac and PC email clients and this works a treat! You've saved me one big headache!
Thanks again.
— Matt , July 15, 2008
Thanks for your script so short.
Please let me know how can I add another $TO contact.
THanks for all
Ercolla
— Ercolla , July 23, 2008
Ercolla,
You can use multiple emails with the $to variable. Just separate the emails with a comma (,)
HTH
— Elle , July 23, 2008
hi, i tried to implement the form, but when i do click to send the form the next message error appears
"Not Found
The requested document was not found on this server. "
i just added my email address, that the only change that i did on the files
anyone can know how to fix it?
— lukas , August 27, 2008
Hi
I'm getting the same problem as lukas, form worked ok until server company upgraded to PHP 5.
Apparently its an error on line 33 "Undefined variable: PHP_SELF"
my knowledge of php is very limited so how can this be fixed?
thanks in advance for help! : )
— paula , August 28, 2008
Lukas, I've sorted it!
you need to replace:
action=""
on line 33 of fns.php with:
action=""
the form then works on servers running php5
hope this helps! : )
— paula , August 28, 2008
insert the following between the quote marks after action on line 33 (hopefully it'll show the code this time!)
instead of
— paula , August 28, 2008
3rd time lucky?
$PHP_SELF;
needs to be replaced with
$_SERVER['PHP_SELF'];
on line 33
— paula , August 28, 2008
Hi Elle!
Your form is very nice, simple, elastic and expressive. I have a question. How it is possible to solve that the form let an automatic e-mail like response to form expletive?
If you want to make a little refreshing then do not hesitate ;)
THX,
Bitcause
— Bitcause , August 31, 2008
And I have one more question. The checkbox system does not give to group, if I want to make a checkbox-require. The $HTTP_POST_VARS is capable of this, but your system is $_POST. Is solving this possible?
THX
— Bitcause , August 31, 2008
Sorry for being away for a bit. I've updated the PHP_SELF.
@Bitcause: let me get back to you on this.
— Elle , September 17, 2008
Great looking script, but for some reason the attachment is not arriving with the e-mail. Have tried all sorts of attachments, including a 1kb .txt file, so it isn't a file size or type issue. Ideas?
— Padraic , September 18, 2008
Also, the script is sending two e-mails...
— Padraic , September 18, 2008
@Padraic: sorry for the delayed response. Have been a bit busy lately.
I've checked the script and it only sends one email - so, not sure what is happening on your side.
With text files, I found out that instead of attaching the file, the contents of the .txt file is added to the bottom of the email.
— Elle , October 01, 2008
Just to say thanks, I was needing a simple form submission to email with file attachment in php for a quick project and I didn't have time to code it myself.
The nice thing, it just worked.
— MattC , October 22, 2008
Thank you!Such a helpful form! I will certainly give you credit for this each time I use it for my clients. Thank you!
— Brandy , October 27, 2008
Thanks for the solution to my problem! However, I am having trouble integrating this into a static html page (not a website built on php).
Are you able to help me do this?
Thanks
— Craig , November 05, 2008
Craig,
The page can be static but you do need it to be a PHP page. If you look at the index page, you will see that php tags in two places. You can change all the rest of the page - but if you do not know php - I wouldn't touch whatever is in between the php tags.
HTH,
Elle
— Elle , November 05, 2008
Hi there
Just wanted to drop by and say thank you very much for this awesome script! I've been searching all morning for something like this and this has been by far the easiest one to use!
Thank you so much for providing such good script for free!
Jerome
— Jerome Danvers , December 18, 2008
hi elle;
got it!! after all searching about file upload script i think i already found the best!! thanks for this.. but i badly need with captcha to avoid email spam.. kindly modified the code with captcha... thanks for this so much! keep it up!
— joan , February 24, 2009