PHP Mailer Form with File Attachment
April 23rd, 2008 | Posted in Goodies, Scripts |
For a long time I’ve used a contact form that I’ve 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.
Installation Instructions:
- You need to include (or require) the fns.php at the top of your page
- 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
Download
PHP Mailer Script (.zip, 8kb)

May 7th, 2008 at 7:23 pm
thank you best and nice tutarial.
May 9th, 2008 at 10:04 am
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. ;)
May 9th, 2008 at 10:41 am
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
May 9th, 2008 at 10:57 am
wow !
thanks for the fast response ;)
i will try it and tell you if i get it
:D
May 9th, 2008 at 11:22 am
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 ;)
May 10th, 2008 at 7:10 pm
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.
May 11th, 2008 at 10:30 pm
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.
May 12th, 2008 at 7:51 am
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.
May 13th, 2008 at 9:29 am
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
May 14th, 2008 at 5:08 pm
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
May 15th, 2008 at 4:42 am
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!!
May 15th, 2008 at 5:47 am
Elle,
You can ignore my previous post. I’ve figured it out myself. Thanks!
May 26th, 2008 at 4:14 pm
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 instruction:
1. You need to include (or require) the fns.php at the top of your page
2. Copy/paste the code within the #contactform div to where you want it to be on your page
3. Change the email address in the fns.php
i hope you can help me.
June 2nd, 2008 at 11:41 am
Thx for Great Script . but how can we stop Spamers .. is there any anti-spam or captcha Code to add to this script
June 2nd, 2008 at 6:39 pm
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
June 2nd, 2008 at 6:51 pm
Hi Omar,
Glad you you could use my script and thank you for updating and improving it :)
June 2nd, 2008 at 7:49 pm
i’m still up , i have tried to add code to show the ip sender . but i coudt get it to work . i dont know what wrong i’m doing.this the code i have included to show the ip sender witht he message
$message .= ‘IP Address: ‘.$_SERVER[’REMOTE_ADDR’].”\n”;i might just did not put it on right place .. could you help me here . Thx
June 2nd, 2008 at 8:49 pm
Add this just before the if statement for mail (in your script it shoud be around line 204 or 20):
$envs = array("HTTP_USER_AGENT", "REMOTE_ADDR", "REMOTE_HOST");
foreach ($envs as $env)
$message .= "$env: $_SERVER[$env]\n";
It works for me :)
HTH
June 3rd, 2008 at 5:00 am
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
June 3rd, 2008 at 1:59 pm
WELL DONE! This script is simply awesome.
June 9th, 2008 at 8:21 pm
Hi the form is great and just what I was looking for but is there anyway I can make it allow 3 attachments.
June 9th, 2008 at 9:52 pm
Thanks Rob. You should check out the phMailer script and how it allows multiple attachments.
June 9th, 2008 at 10:00 pm
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.
June 10th, 2008 at 12:10 am
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.
June 11th, 2008 at 12:21 am
Elle,
A simply elegant and delightful implementation.
Kudos to you! and thank you for sharing!!
-Ray
June 11th, 2008 at 4:33 am
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?
June 11th, 2008 at 4:28 pm
@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
June 12th, 2008 at 8:42 am
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?
June 15th, 2008 at 10:17 pm
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 (100caracters left)
Adaptation of a script found over the internet (http://devweb.jegalere.fr/category/javascript/ in French). Thanks to both of you.