Multiple Email Attachments with PHP

Поділитися
Вставка
  • Опубліковано 9 лис 2024

КОМЕНТАРІ • 112

  • @MrKuros
    @MrKuros 12 років тому

    A lot of tutorials out there are unclear, and mainly just copy paste snippets without explanation. Thanks for this clear (and working!) tutorial.

  • @josephdevera2910
    @josephdevera2910 9 років тому

    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."..

  • @optikalefx
    @optikalefx  13 років тому

    @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.

  • @agill4666
    @agill4666 9 років тому

    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

  • @SouhailRAZZOUK
    @SouhailRAZZOUK 11 років тому +1

    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

    • @optikalefx
      @optikalefx  11 років тому +1

      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

    • @SouhailRAZZOUK
      @SouhailRAZZOUK 11 років тому

      :D there is always a price to pay

  • @optikalefx
    @optikalefx  13 років тому

    @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

  • @smileysloth
    @smileysloth 9 років тому +4

    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?

  • @optikalefx
    @optikalefx  13 років тому

    @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

  • @coAdjointTom
    @coAdjointTom 11 років тому

    Absolute life and business saver!

  • @btjernig
    @btjernig 10 років тому

    Best TuT I found by far, by the way.

  • @h2mauricio
    @h2mauricio 9 років тому +1

    Thank you, man. You made my day!

  • @optikalefx
    @optikalefx  11 років тому

    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.

  • @optikalefx
    @optikalefx  11 років тому

    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.

  • @TheChewyWun
    @TheChewyWun 13 років тому

    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.

  • @garkanum
    @garkanum 9 років тому

    This is absolutely fantastic! Thank you!

  • @AmanSingh-mb8lx
    @AmanSingh-mb8lx 6 років тому

    Just wow and Big thank you !! I was struggling for this . Thanks a lot

  • @shoothiscene
    @shoothiscene 13 років тому

    @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.

  • @madfun83
    @madfun83 13 років тому

    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

  • @NormanBird
    @NormanBird 11 років тому

    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

  • @btjernig
    @btjernig 10 років тому +1

    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?

  • @nikoletatomasevic7058
    @nikoletatomasevic7058 7 років тому

    Great tutorial even for the beginners. It would be even better if you explained how to define max upload file size above 2 MB.

  • @ondeweb
    @ondeweb 12 років тому +1

    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?

  • @thebroketechtuber6614
    @thebroketechtuber6614 6 років тому

    thanks a lot man!!! it was a life saver. works like a charm.

  • @dwiddel
    @dwiddel 11 років тому

    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.

  • @joshualykkeberg7059
    @joshualykkeberg7059 8 років тому

    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?

  • @mikhailmiasnikov1914
    @mikhailmiasnikov1914 7 років тому

    This is a very cool video!
    Thank you so much!

  • @optikalefx
    @optikalefx  13 років тому

    @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

  • @310azad
    @310azad 12 років тому

    Ok, I finally get the dummy file send working. I just needed an IF Statement in Loop 2.

  • @minzarowlandmdee9988
    @minzarowlandmdee9988 4 роки тому

    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.

  • @TheChewyWun
    @TheChewyWun 13 років тому

    @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?

  • @optikalefx
    @optikalefx  11 років тому

    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.

  • @optikalefx
    @optikalefx  11 років тому

    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.

  • @100Awesome
    @100Awesome 8 років тому

    after alooooooot of strugeling it worked thank you sooo much this will be very usefull

  • @bolaraafat3350
    @bolaraafat3350 7 років тому

    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

  • @bslvr895
    @bslvr895 12 років тому

    @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?

  • @aboodsalami300
    @aboodsalami300 7 років тому

    What is the IDE you have used in this tutorial?
    Thank you very much

  • @rexhepkrasniqi
    @rexhepkrasniqi 9 років тому

    very nice man. Thank you

  • @stephenthompson2390
    @stephenthompson2390 9 років тому

    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"

  • @tyasaka
    @tyasaka 11 років тому

    Thanks man. This helps me a lot!

  • @pradipbhagat2389
    @pradipbhagat2389 7 років тому

    Thanx Its a proper solution for send mail with attachment
    thanx a lot

  • @mylaimadhu
    @mylaimadhu 10 років тому

    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.

    • @optikalefx
      @optikalefx  10 років тому

      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.

    • @mylaimadhu
      @mylaimadhu 10 років тому

      optikalefx
      Thanks for your kind info. Its able to receive the text fields info. Thanks. And very much appreciated.

  • @TheChewyWun
    @TheChewyWun 13 років тому

    @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.

  • @madfun83
    @madfun83 13 років тому

    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

  • @tmriddle2
    @tmriddle2 13 років тому

    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?

  • @JDMedia
    @JDMedia 7 років тому

    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!!!

  • @bonniethompson341
    @bonniethompson341 5 років тому

    "JeSuS .. sorry for religious people" hahahaha! you're great keep making content!

  • @josipmatic4732
    @josipmatic4732 9 років тому

    Thanks a lot, work for me.

  • @bslvr895
    @bslvr895 12 років тому

    @toddzy2007 what about you want to allow a file with any extensions. I don't want limit to certain extension?

  • @victorumana4944
    @victorumana4944 7 років тому

    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

  • @madfun83
    @madfun83 13 років тому

    It does send an email with the broken jpeg attachments.

  • @gurnzbot
    @gurnzbot 9 років тому

    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 :)

  • @100Awesome
    @100Awesome 8 років тому

    why do i get my email twice (2 times the same file in 1 email, im using 1 single file to upload btw)

  • @davidp1303
    @davidp1303 7 років тому

    Thanks so much for this. it was really helpful.

  • @MagicMushr0om
    @MagicMushr0om 13 років тому

    woops. sorry for repost

  • @Tenebor90
    @Tenebor90 12 років тому

    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!

  • @bisbireto303
    @bisbireto303 8 років тому

    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!

  • @thebaide3931
    @thebaide3931 8 років тому

    Big thanks!!

  • @TheLuksysVideos
    @TheLuksysVideos 8 років тому

    Thank you very much ! :)

  • @humanNumer1
    @humanNumer1 12 років тому

    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?

  • @drelouder
    @drelouder 8 років тому

    thanks man,
    your script help me alot,

  • @augustinesites
    @augustinesites 13 років тому

    Thank you very much helped out so much! exactly what i was looking for! : )

  • @humayunehtisham
    @humayunehtisham 7 років тому

    Thanks man!

  • @madfun83
    @madfun83 13 років тому

    @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.

  • @jkctech
    @jkctech 11 років тому

    If i receive the e-mail it doesnt put an extension behind the filename!!! PLZ help

  • @rakuuuu16
    @rakuuuu16 12 років тому

    Have a problem. My script wont send file, uploads them to a folder and says "extension not allowed". any ideas?

  • @AmrGamal878
    @AmrGamal878 13 років тому

    Nice Tutorial , Thank you :)

  • @amitlalvani4350
    @amitlalvani4350 8 років тому

    help with this error
    mail(): Multiple or malformed newlines found in additional_header in /home/scmsoahh/public_html/hirensir/attach.php on line 51

  • @drcinebuffhindi1061
    @drcinebuffhindi1061 10 років тому

    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...??

    • @optikalefx
      @optikalefx  10 років тому

      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.

    • @drcinebuffhindi1061
      @drcinebuffhindi1061 10 років тому

      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

  • @kupowns
    @kupowns 13 років тому

    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.

  • @scotthartman8390
    @scotthartman8390 10 років тому

    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....?

    • @optikalefx
      @optikalefx  10 років тому

      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

  • @optikalefx
    @optikalefx  11 років тому

    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.

  • @abdulrhmankhallil8254
    @abdulrhmankhallil8254 9 років тому

    oooooooh yeah it works
    very clear
    and very good
    Thanks a lot :)

  • @deferdie
    @deferdie 10 років тому

    Really good video but how do i add a text field?

  • @jgarciascr5
    @jgarciascr5 10 років тому +1

    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/

  • @marrto
    @marrto 12 років тому

    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?

  • @joetrujillo8453
    @joetrujillo8453 6 років тому

    Thanks bro :)

  • @MrKarthikeyan234
    @MrKarthikeyan234 12 років тому

    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?

  • @optikalefx
    @optikalefx  13 років тому

    @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

  • @megabulk
    @megabulk 10 років тому

    I did everything what you said my attachments come as "noname" and with size = 0. Why??????? Can anyone help me????

  • @madfun83
    @madfun83 13 років тому

    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:

  • @rohitya01
    @rohitya01 11 років тому

    hey bro i appreciated what you did but can you upload video .. sending html table form with multiple attachment.

    • @optikalefx
      @optikalefx  11 років тому

      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.

  • @bslvr895
    @bslvr895 12 років тому

    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?

  • @MonirulIslam-fc1xd
    @MonirulIslam-fc1xd 9 років тому

    congratulate to you I am success to send email attachments file to show your tutorial

  • @madfun83
    @madfun83 13 років тому

    failed to open stream: No such file or directory in /home/tony/public_html/attach.php on line 25

  • @jgbalboa
    @jgbalboa 9 років тому

    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.

    • @jgbalboa
      @jgbalboa 9 років тому

      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?

    • @optikalefx
      @optikalefx  9 років тому

      Jose Balboa max_file_size in php.ini

    • @bolaraafat3350
      @bolaraafat3350 7 років тому

      i have same problem , you have any soultion for that ???

  • @imaferryproductions7520
    @imaferryproductions7520 9 років тому

    Getting the "extension not allowed" error no matter what

  • @orthopedicsmeddocs5838
    @orthopedicsmeddocs5838 4 роки тому

    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

  • @MagicMushr0om
    @MagicMushr0om 13 років тому

    the sent files are damaged :/

  • @Haterino
    @Haterino 10 років тому

    hey, thx thx thx, I've been searching for then
    thx :D

  • @clarityhrsolutions8075
    @clarityhrsolutions8075 10 років тому

    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?

  • @MagicMushr0om
    @MagicMushr0om 13 років тому

    the sent files are damaged

  • @comercialconferencia487
    @comercialconferencia487 7 років тому

    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?

  • @madfun83
    @madfun83 13 років тому

    Warning: move_uploaded_file

  • @desmondotoole3038
    @desmondotoole3038 9 років тому

    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.

  • @madfun83
    @madfun83 13 років тому

    It does send an email with the broken jpeg attachments.