PHP Mail Attachment

Posted on 2007-07-22
Last Modified: 2008-02-01
Hi, i want to create a code in PHP for sending email with an attachment.
I will attach a license file with my own extension, i.e uoreg files.

      $user = $_SESSION["username"];
        $licenseName = $user."license.uoreg";
      $location = "./license/".$licenseName;
      $eol = "\n";

      $handle = fopen($location, "rb");
      $contents = fread($handle, filesize($location));
      $contents = chunk_split(base64_encode($contents));
      $fType = filetype($location);

      $emailAddr = "";
      $subject = "License file";


      $headers = 'MIME-Version: 1.0'.$eol;
      $headers .= "Content-Type: multipart/related; boundary=\"".$mime_boundary."\"".$eol;
      $msg = "";

      $msg = "--".$mime_boundary.$eol;
      $msg .= "Content-Type: application/octet-stream; name=\"".$licenseName."\"".$eol;
      $msg .= "Content-Transfer-Encoding: base64".$eol;
      $msg .= "Content-Disposition: attachment; filename=\"".$licenseName."\"".$eol.$eol;
      $msg .= $contents.$eol.$eol;

      //Setup for text OR html
      $msg .= "Content-Type: multipart/alternative".$eol;

      $msg .= "--".$mime_boundary."--".$eol.$eol;

      $ok = mail($emailAddr, $subject, $msg, $headers);

I manage to download a file, but when i try to open up the files with our software.
Our software can't read the files. Though I already open and read the files.
Can someone help me, pls?
Thanks in advance
Question by:cemplukcute2003
    LVL 51

    Expert Comment

    > I manage to download a file, but when i try to open up the files with our software.
    hmm, which files are you talking about?

    Author Comment

    The .uoreg files :)
    I can download it, but my software can't open it.
    LVL 51

    Expert Comment

    > .. download ..
    and what has this to do with mail and/or your php script?
    LVL 2

    Accepted Solution

    Why not try PHPMailer?

    Author Comment

    Yea, thanks dragonman. PHPMailer works just nice :D
    Sorry for the late reply, i was busy with final year project...:(
    Well, i think you know how's student's life :p
    LVL 2

    Expert Comment

    Know what you mean!
    Glad it all worked.

    Featured Post

    Live: Real-Time Solutions, Start Here

    Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

    Join & Write a Comment

    Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
    Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now