I just analyzed your script and had a little experiment on it then i was able to upload it to our host and make it work flowlessly. Thanks Dude! "Power is gained by sharing knowledge, not hoarding it."..
@Unifylink well the code uses a form to post the file. Meaning it posts to the server that the php is running on. So it uploads to your own server. Then you use move_uploaded_file to move it to another location on your server.
Perfect, this is better than the last video. For all who can't make the script work, look out for extra white spaces - they are key problems when writing headers. Thanks optikalefx - good job
Hi Maaaan :) i just want to tell you one and only one thing { Thanks } :D you solved my problems withing 10 Min and i really apologize for sending you some files by error cuz i didn't changed the receiving email address thanks again and a big salute from MOROCCO :D
@madfun83 this is saying it doesn't have permission to write in that directory. Make sure the permissions in the folder are set to something writable, set it to 777 for now to test
1) I'm not sure, but it would be easy to add a check to make sure the $_FILES array is empty or ful lfirst 2) Check your $_FILES array to see what you have available. Worst case scenario your server is doing that on its own and you just have to do an end(explode("/")) to get the last part.
That's not an error, thats part of the email boundary. That means that you have a quote in the wrong place causing the email to think this is part of your message. Check your quotes 5 times.
Hi Sean, one thing I noticed (and probably you noticed also because I think I sent you 2 images LOL) you have to change the email addresses in the script before uploading to your own web servers. We also have to change the server file path to fit our web servers. I have a sneaky suspicion that's what's causing some of the other troubles.
@madfun83 This is a very efficient method for coding PHP, i have to admit, one the better codes out there, just remenber to listen to instructions carefully, a little tip; $server_file = "./uploads/$path_parts[basename]"; look at the ./uploads, it has a dot before it telling the server to go straight to this directory. so the attatchments load.
Thanks for your help. I do have one other question. This code uses pre built buttons. How would i dot it if the buttons i wanted to use were buttons i created
Sean it works. this is best code ive seen so far. I noticed though the email attachments name is server full path ie. /home/hope/public_html/wp-content/uploads/files/box.txt. Any clue why or how to have it just show a filename? thanks
I am only getting a /tmp/randomnumber file as my attachment. How can I get the script to provide the users filename and the extension so I can open the attachment?
i recd. this error 3 times in one mail: MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="==Multipart_Boundary_x1bb1d42caf7aab906ba252c24913e007x" what might have gone wrong?
Great job on the script! How would you modify it to send multiple files using only one input box/browse button on the form? The user would then hold down CTRL to select multiple files.
Got everything working awesome, I can upload 2 or more files, but how do you make the attachments optional? Currently, if I have 2 file browse things and upload only one file, I get an internal server error when submitting. I do however, get the email with the attachment and the other attachment is blank. Am I possibly just doing something wrong?
@madfun83 If the you are SURE this file is there, then check the permissions and make sure they are readable. Just set it to 777 for now and then check
Thank you very much, This has helped me so much. I need your assistance on how I can do the same while taking attachment files from the same server directory and not from HTML form anymore.
@optikalefxx Yea, that is pretty finicky. I have a question that I'm trying to figure out with this, if you wanted to send these out through Google apps, so using Gmail's email servers, how much harder would it be to add the authentication information, port 587, TLS encryption and the username and password? I'm assuming an include statement with a config file, but how would you call it in this instance since it's using php mail function, it'll always try and send locally first, correct?
The message isn't being sent. Try double checking your quotes and all your variables to see where you might have missed a part of it. Commas are also a place to check, try to match the video.
@toddzy2007 When you leave the fields blank/empty does it attach a dummy file? Even I put NULL and don't attach a file, some reason it attaches a dummy file. I have not figure it out why. Any idea?
Having issues with multiple attachments.Line 11 and line 43 are using "foreach" and "for", so I assumed I could add a new line in the form: or even an attach3. Problem is unless I put something into all the input fields, the script crashes, and throws a PHP error "fopen() [function.fopen]: Filename cannot be empty"
Really thanks for the script. Its works for multiple attachments. Sorry about asking on your busy things. I am just getting struggle to add one input text box inside and how to receive the information as well into that mail.
You're gonna make that text input, give it a name attribute, and then receive the data as $_POST['whatever you named it']; Then, where we add the text part of the message, (or html part) use that variable.
@optikalefxx ok, so just look at an email that was sent using Google apps, and grab the rest of the header information from there. Sounds easy enough, lol, yea right. Ok, I'll give it a whirl. Thanks for the great TuT.
I have a bunch of lines that go on like that. i did create my path to the upload folder. I was wondering if you could tell me why i'm getting these errors. Thanks
Hi do you have any tutorials that can say how to track a users actions like what they downloaded Also how to keep the info they put in forms to stay there for the session?
Great Tutorial!!! can you please tell how to add from email while receiving, also if I want add more fields like full name, email so that can be done complete form with attach file option many thanks!!!
Wow, Thank you so much, It works great, quick questions, how would you also send this email with an HTML body, I want to add a (table) with a couple of (H1)s and with the images attach
I've found that it's a good idea to use PHP_EOL rather than when setting headers, boundaries, etc. Servers seem to enjoy it moreso. I could be incorrect and it might not matter, but this has worked for my mailing script I've used for years. If I'm incorrect please learn me something :)
all perfect, only a little big problem, when i send the mail, the attached is empy (0 bytes). If i chek the remote folder (permission 777) is empty. Can you help me? I know that this video is old but i need it please! Thanks!
Hi! It worked great several times without any problem, But now, it sends me an email with a very large large large code and the attachment doesn´t show up. And I didn´t change the code. Do you have any clue of what could happen? Thanks!
Two things: 1) What happens when you don't attach any file or just one? Have you tested? In my case it gives the wrong extension error message. So it thinks something has been attached. 2) Also my attachments have the whole server path in their file name instead of just file name. Any advice?
@optikalefxx thanks for the info. i did have the folder permissions set to 777 which didnt work. It is working now. Instead of using all the sub folders i just place my uploads folder direct and changed my code to this uploads/$path_parts[basename]. I probably needed to set all those folders to 777 is that correct? anyhow i got it to work. Thanks alot for your help.
Thanks for such an excellent tutorial, but have a doubt...I am testing it in local environment and using this code for moving it to server $server_file = "localhost:81/glambuds/uploads/$path_parts[basename]"; but getting a blank file with right extension...what am I doing wrong...??
Somehow I am getting this error...Notice: Array to string conversion in E:\xampp\htdocs\glambuds\attach.php on line 59 & 61 which is $message .= " name=\"$files\" ";...can you please look at it...thanks in advance
I'm having problems with the directories, I'm not working on a local machine, when I use the website full location to the folder I get a "move_uploaded_file" error. I have the permission of that folder to 777.
Where does this code go exactly? In the form that the user is filling out, or the form that the data is sent to? I'm not understanding which form you're writing on....?
So this example is 1 file which is both the client and the server, because the form submits to itself. But in a real world situation you should point your form a PHP file that does the work
If you need a drop in script then you aren't learning much. If you watch both tutorials you should be able to know where to modify the name. The point wasn't to give some code out, it was to teach.
This code didn't work for me just for one reason: the boundary for each attachment must be placed at the beginning of each encoded file (base64_encode) and because my mail server is Microsoft Exchange 2013. For other mail servers this code works fine without changes. Just pay attention to the position the boundaries must have within a MIME multi-part message: www.qcode.co.uk/mime-multi-part-messages/
Hi friend.I am using your mail code for my projects now i need SMTP Authentication along with this..i searched google but could not get the right part.How can we do this?
@TheChewyWun is that you i keep getting email from? lol. Yea its too late now haha everyones using the script. Its ok, i made a smart folder to catch all those in my inbox. You know people aren't having that issue - they are typing the script wrong, everyone seems to get the spacing off, and rightly so, its gotta be so exact
Okay, so I copied and pasted your file and it works perfectly from WampServer, but NOT when I upload it to my www webhost, so I am thinking that I need to specify the upload location in my webhost. I noticed on your tutorial, at: 5:07, you specify a location, but I don't see that on the "copy and paste" script. Please tell me what I need to do, so it will work on my web server--like it works on my WampServer.
Nevermind, it works from the www webhost. I guess they have to be really small files, like pictures around 500kb or less. How can I increase the size of the file size?
i am getting extension not allowed for $allowedExtensions = array("pdf","doc","docx","gif","jpeg","jpg","png","rtf","txt",""); $files = array(); //loop through all the files foreach($_FILES as $name=>$file){ // define some variables $file_name = $file['name']; $temp_name = chunk_split(base64_encode(file_get_contents($_FILES["file"]["tmp_name"]))); //check if this file type is allowed $path_parts = pathinfo($file_name); $ext = $path_parts['extension']; if(in_array($ext, $allowedExtensions)) { die("extension not allowed"); } how to resolve this brother
Hi Optikalefx thank you so much for this tuto, the best by far i found. It works perfectly my only issue is on line 18 and the die. It kill the page instead of echo an error message on the same page. could anyone help please?
Hello, mate. Following > and your tutorial, I managed to attach files to image, but "header" is not working. The content just pops the "message". Can you help me, please?
Sorry. I copied the sample gist.github.com/optikalefx/5149537 It did not work. The only changes made were to the email from and to. I tried both an AOL email and a google email. It claims to have sent it but I never received them. I use the mail() function all the time without any problems as a basic mailing system.
A lot of tutorials out there are unclear, and mainly just copy paste snippets without explanation. Thanks for this clear (and working!) tutorial.
I just analyzed your script and had a little experiment on it then i was able to upload it to our host and make it work flowlessly. Thanks Dude!
"Power is gained by sharing knowledge, not hoarding it."..
@Unifylink well the code uses a form to post the file. Meaning it posts to the server that the php is running on. So it uploads to your own server. Then you use move_uploaded_file to move it to another location on your server.
Perfect, this is better than the last video. For all who can't make the script work, look out for extra white spaces - they are key problems when writing headers. Thanks optikalefx - good job
Hi Maaaan :) i just want to tell you one and only one thing { Thanks } :D you solved my problems withing 10 Min and i really apologize for sending you some files by error cuz i didn't changed the receiving email address thanks again and a big salute from MOROCCO :D
No prob! glad it helped. I get about 100 of those test emails a month. Oh why did I not change my email address. Haha
:D there is always a price to pay
@TheChewyWun I'm pretty sure those are just more headers. SO you would look up which headers you need, and then include them in the main header part
Is there a way to do this with just one input element where a customer can select multiple files at a time to be uploaded?
@madfun83 this is saying it doesn't have permission to write in that directory. Make sure the permissions in the folder are set to something writable, set it to 777 for now to test
Absolute life and business saver!
Best TuT I found by far, by the way.
Thank you, man. You made my day!
1) I'm not sure, but it would be easy to add a check to make sure the $_FILES array is empty or ful lfirst
2) Check your $_FILES array to see what you have available. Worst case scenario your server is doing that on its own and you just have to do an end(explode("/")) to get the last part.
That's not an error, thats part of the email boundary. That means that you have a quote in the wrong place causing the email to think this is part of your message. Check your quotes 5 times.
Hi Sean, one thing I noticed (and probably you noticed also because I think I sent you 2 images LOL) you have to change the email addresses in the script before uploading to your own web servers. We also have to change the server file path to fit our web servers. I have a sneaky suspicion that's what's causing some of the other troubles.
This is absolutely fantastic! Thank you!
Just wow and Big thank you !! I was struggling for this . Thanks a lot
@madfun83 This is a very efficient method for coding PHP, i have to admit, one the better codes out there, just remenber to listen to instructions carefully, a little tip;
$server_file = "./uploads/$path_parts[basename]";
look at the ./uploads, it has a dot before it telling the server to go straight to this directory. so the attatchments load.
Thanks for your help. I do have one other question. This code uses pre built buttons. How would i dot it if the buttons i wanted to use were buttons i created
Sean it works. this is best code ive seen so far. I noticed though the email attachments name is server full path ie. /home/hope/public_html/wp-content/uploads/files/box.txt. Any clue why or how to have it just show a filename?
thanks
I am only getting a /tmp/randomnumber file as my attachment. How can I get the script to provide the users filename and the extension so I can open the attachment?
Great tutorial even for the beginners. It would be even better if you explained how to define max upload file size above 2 MB.
i recd. this error 3 times in one mail:
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="==Multipart_Boundary_x1bb1d42caf7aab906ba252c24913e007x"
what might have gone wrong?
thanks a lot man!!! it was a life saver. works like a charm.
Great job on the script! How would you modify it to send multiple files using only one input box/browse button on the form? The user would then hold down CTRL to select multiple files.
Got everything working awesome, I can upload 2 or more files, but how do you make the attachments optional? Currently, if I have 2 file browse things and upload only one file, I get an internal server error when submitting. I do however, get the email with the attachment and the other attachment is blank.
Am I possibly just doing something wrong?
This is a very cool video!
Thank you so much!
@madfun83 If the you are SURE this file is there, then check the permissions and make sure they are readable. Just set it to 777 for now and then check
Ok, I finally get the dummy file send working. I just needed an IF Statement in Loop 2.
Thank you very much, This has helped me so much.
I need your assistance on how I can do the same while taking attachment files from the same server directory and not from HTML form anymore.
@optikalefxx Yea, that is pretty finicky. I have a question that I'm trying to figure out with this, if you wanted to send these out through Google apps, so using Gmail's email servers, how much harder would it be to add the authentication information, port 587, TLS encryption and the username and password? I'm assuming an include statement with a config file, but how would you call it in this instance since it's using php mail function, it'll always try and send locally first, correct?
The message isn't being sent. Try double checking your quotes and all your variables to see where you might have missed a part of it. Commas are also a place to check, try to match the video.
usually the name isn't the path, debug your $_FILES array to make sure you're grabbing the right key that has just the name.
after alooooooot of strugeling it worked thank you sooo much this will be very usefull
thank you so much it's working great and its easy for a beginner like me its the only helpful video i found here on youtube
@toddzy2007 When you leave the fields blank/empty does it attach a dummy file?
Even I put NULL and don't attach a file, some reason it attaches a dummy file. I have not figure it out why. Any idea?
What is the IDE you have used in this tutorial?
Thank you very much
very nice man. Thank you
Having issues with multiple attachments.Line 11 and line 43 are using "foreach" and "for", so I assumed I could add a new line in the form:
or even an attach3. Problem is unless I put something into all the input fields, the script crashes, and throws a PHP error "fopen() [function.fopen]: Filename cannot be empty"
Thanks man. This helps me a lot!
Thanx Its a proper solution for send mail with attachment
thanx a lot
Really thanks for the script. Its works for multiple attachments.
Sorry about asking on your busy things. I am just getting struggle to add one input text box inside and how to receive the information as well into that mail.
You're gonna make that text input, give it a name attribute, and then receive the data as $_POST['whatever you named it'];
Then, where we add the text part of the message, (or html part) use that variable.
optikalefx
Thanks for your kind info. Its able to receive the text fields info. Thanks. And very much appreciated.
@optikalefxx ok, so just look at an email that was sent using Google apps, and grab the rest of the header information from there. Sounds easy enough, lol, yea right. Ok, I'll give it a whirl. Thanks for the great TuT.
I have a bunch of lines that go on like that. i did create my path to the upload folder. I was wondering if you could tell me why i'm getting these errors. Thanks
Hi do you have any tutorials that can say how to track a users actions like what they downloaded
Also how to keep the info they put in forms to stay there for the session?
Great Tutorial!!!
can you please tell how to add from email while receiving, also if I want add more fields like full name, email so that can be done complete form with attach file option
many thanks!!!
"JeSuS .. sorry for religious people" hahahaha! you're great keep making content!
Thanks a lot, work for me.
@toddzy2007 what about you want to allow a file with any extensions. I don't want limit to certain extension?
Wow, Thank you so much, It works great, quick questions, how would you also send this email with an HTML body,
I want to add a (table) with a couple of (H1)s and with the images attach
It does send an email with the broken jpeg attachments.
I've found that it's a good idea to use PHP_EOL rather than
when setting headers, boundaries, etc. Servers seem to enjoy it moreso. I could be incorrect and it might not matter, but this has worked for my mailing script I've used for years. If I'm incorrect please learn me something :)
why do i get my email twice (2 times the same file in 1 email, im using 1 single file to upload btw)
Thanks so much for this. it was really helpful.
woops. sorry for repost
all perfect, only a little big problem, when i send the mail, the attached is empy (0 bytes). If i chek the remote folder (permission 777) is empty. Can you help me? I know that this video is old but i need it please! Thanks!
Hi!
It worked great several times without any problem,
But now, it sends me an email with a very large large large code and the attachment doesn´t show up. And I didn´t change the code.
Do you have any clue of what could happen?
Thanks!
Big thanks!!
Thank you very much ! :)
Two things:
1) What happens when you don't attach any file or just one? Have you tested? In my case it gives the wrong extension error message. So it thinks something has been attached.
2) Also my attachments have the whole server path in their file name instead of just file name.
Any advice?
thanks man,
your script help me alot,
Thank you very much helped out so much! exactly what i was looking for! : )
Thanks man!
@optikalefxx thanks for the info. i did have the folder permissions set to 777 which didnt work. It is working now. Instead of using all the sub folders i just place my uploads folder direct and changed my code to this uploads/$path_parts[basename]. I probably needed to set all those folders to 777 is that correct? anyhow i got it to work. Thanks alot for your help.
If i receive the e-mail it doesnt put an extension behind the filename!!! PLZ help
Have a problem. My script wont send file, uploads them to a folder and says "extension not allowed". any ideas?
Nice Tutorial , Thank you :)
help with this error
mail(): Multiple or malformed newlines found in additional_header in /home/scmsoahh/public_html/hirensir/attach.php on line 51
Thanks for such an excellent tutorial, but have a doubt...I am testing it in local environment and using this code for moving it to server
$server_file = "localhost:81/glambuds/uploads/$path_parts[basename]";
but getting a blank file with right extension...what am I doing wrong...??
I'm not sure what syntax that file path is in, but you need the path you use to be a reference to the tmp_name from the $_FILES array of the upload.
Somehow I am getting this error...Notice: Array to string conversion in E:\xampp\htdocs\glambuds\attach.php on line 59 & 61 which is $message .= " name=\"$files\"
";...can you please look at it...thanks in advance
I'm having problems with the directories, I'm not working on a local machine, when I use the website full location to the folder I get a "move_uploaded_file" error. I have the permission of that folder to 777.
Where does this code go exactly? In the form that the user is filling out, or the form that the data is sent to? I'm not understanding which form you're writing on....?
So this example is 1 file which is both the client and the server, because the form submits to itself. But in a real world situation you should point your form a PHP file that does the work
If you need a drop in script then you aren't learning much. If you watch both tutorials you should be able to know where to modify the name. The point wasn't to give some code out, it was to teach.
oooooooh yeah it works
very clear
and very good
Thanks a lot :)
Really good video but how do i add a text field?
This code didn't work for me just for one reason: the boundary for each attachment must be placed at the beginning of each encoded file (base64_encode) and because my mail server is Microsoft Exchange 2013. For other mail servers this code works fine without changes. Just pay attention to the position the boundaries must have within a MIME multi-part message: www.qcode.co.uk/mime-multi-part-messages/
Hey man, so I have one problem, the images that are attached, end up being unusable and are only 165b or something pathetic. What is wrong here?
Thanks bro :)
Hi friend.I am using your mail code for my projects now i need SMTP Authentication along with this..i searched google but could not get the right part.How can we do this?
@TheChewyWun is that you i keep getting email from? lol. Yea its too late now haha everyones using the script. Its ok, i made a smart folder to catch all those in my inbox. You know people aren't having that issue - they are typing the script wrong, everyone seems to get the spacing off, and rightly so, its gotta be so exact
I did everything what you said my attachments come as "noname" and with size = 0. Why??????? Can anyone help me????
Hey, been trying to get this to work and its exactly what i need and i think i am very close but every time i hit submit this is what i get:
hey bro i appreciated what you did but can you upload video .. sending html table form with multiple attachment.
I can yea, but really you just include that HTML in the section where you say "hey this part is html" and you just make your html table as normal.
There is one bug in the code, if you don't attach a file some reason it attaches a dummy (empty file). Does anyone know how to fix it?
congratulate to you I am success to send email attachments file to show your tutorial
failed to open stream: No such file or directory in /home/tony/public_html/attach.php on line 25
Okay, so I copied and pasted your file and it works perfectly from WampServer, but NOT when I upload it to my www webhost, so I am thinking that I need to specify the upload location in my webhost. I noticed on your tutorial, at: 5:07, you specify a location, but I don't see that on the "copy and paste" script. Please tell me what I need to do, so it will work on my web server--like it works on my WampServer.
Nevermind, it works from the www webhost. I guess they have to be really small files, like pictures around 500kb or less. How can I increase the size of the file size?
Jose Balboa max_file_size in php.ini
i have same problem , you have any soultion for that ???
Getting the "extension not allowed" error no matter what
i am getting extension not allowed for $allowedExtensions = array("pdf","doc","docx","gif","jpeg","jpg","png","rtf","txt","");
$files = array();
//loop through all the files
foreach($_FILES as $name=>$file){
// define some variables
$file_name = $file['name'];
$temp_name = chunk_split(base64_encode(file_get_contents($_FILES["file"]["tmp_name"])));
//check if this file type is allowed
$path_parts = pathinfo($file_name);
$ext = $path_parts['extension'];
if(in_array($ext, $allowedExtensions)) {
die("extension not allowed");
}
how to resolve this brother
the sent files are damaged :/
hey, thx thx thx, I've been searching for then
thx :D
Hi Optikalefx thank you so much for this tuto, the best by far i found. It works perfectly my only issue is on line 18 and the die. It kill the page instead of echo an error message on the same page. could anyone help please?
the sent files are damaged
Hello, mate. Following > and your tutorial, I managed to attach files to image, but "header" is not working. The content just pops the "message". Can you help me, please?
Warning: move_uploaded_file
Sorry. I copied the sample gist.github.com/optikalefx/5149537
It did not work. The only changes made were to the email from and to. I tried both an AOL email and a google email. It claims to have sent it but I never received them. I use the mail() function all the time without any problems as a basic mailing system.
It does send an email with the broken jpeg attachments.