Send Mail with attachment

Discussion in 'PHP' started by revathyelan, Jul 22, 2009.

  1. #1
    Hai , I want to send a mail with file attachment. To add a file using browse button i used the <input type=file> in html. After the selection a file, i have been pass that selected file path to another php file. Using GET method i can able to retrive the file name only, not full path. How can i pass full path of that selected file to php file?.... :roll:

    Thanks in advance,
    Revathy.
     
    revathyelan, Jul 22, 2009 IP
  2. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Two things are required when you're trying to submit a form with data
    1) The form must use the 'post' method.
    2) The form must have the enctype="multipart/form-data" attribute set

    You cannot send a file attachment via the GET method.

    And on the PHP side you use $_FILE (it gets saved to a temp location)

    Example

    html
    
    <form enctype="multipart/form-data" action="uploader.php" method="POST">
    <input type="hidden" name="MAX_FILE_SIZE" value="100000" />
    Choose a file to upload: <input name="uploadedfile" type="file" /><br />
    <input type="submit" value="Upload File" />
    </form>
    
    Code (markup):
    PHP
    
    $target_path = "uploads/";
    
    $target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 
    
    if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
        echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
        " has been uploaded";
    } else{
        echo "There was an error uploading the file, please try again!";
    }
    
    Code (markup):
     
    kblessinggr, Jul 22, 2009 IP
  3. revathyelan

    revathyelan Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hai Thank you very much. The above code is very useful for me. Thanks a lot. Now i can able to get the file and it uploaded to my server too. Using that target_path i tried to attach my file to send mail. But it has no content while i tried to open it after downloaded it from mail.

    I attached a small text file, and i tried to open it, it missed some content. My original text file size is 156bytes, but my downloded file from mail is 99bytes.

    And again i tried to send a mail with jpg file attachment. It downloads the same file but, while i tried to open, it shows no preview available.
     
    revathyelan, Jul 22, 2009 IP
  4. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Well the target path may need to be adjusted for your server's actual path, also the path being written to must be made writable (chmod 777 etc). The code I provided has nothing to do with mail.
     
    kblessinggr, Jul 22, 2009 IP
  5. revathyelan

    revathyelan Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Once again thank you for your reply. now I can able to send a mail with attachment. Thanks for your suggestions.:)
     
    revathyelan, Jul 23, 2009 IP
  6. revathyelan

    revathyelan Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Hai, Now i have a problem while sending mail with word document as attachments. I can only able to send a mail with image files (jpg/gif). But my need is to attach word document. Can you help please?

    My code for send jpg files is,

    $to = 'revathy@yahoo.com';

    $subject = 'PHP Mail Attachment Test';

    $bound_text = "jimmyP123";

    $bound = "--".$bound_text."\r\n";

    $bound_last = "--".$bound_text."--\r\n";

    $headers = "From: XX@yahoo.com\r\n";
    $headers .= "MIME-Version: 1.0\r\n"
    ."Content-Type: multipart/mixed; boundary=\"$bound_text\"";

    $message .= "If you can see this MIME than your client doesn't accept MIME types!\r\n"
    .$bound;

    $message .= "Content-Type: text/html; charset=\"iso-8859-1\"\r\n"
    ."Content-Transfer-Encoding: 7bit\r\n\r\n"
    .$bound;
    //."hey my <b>good</b> friend here is a picture of regal beagle\r\n"

    $file = file_get_contents($target_path);

    $message .= "Content-Type: image/jpg; name=\"attached.jpg\"\r\n"
    ."Content-Transfer-Encoding: base64\r\n"
    ."Content-disposition: attachment; file=\"attached.jpg\"\r\n"
    ."\r\n"
    .chunk_split(base64_encode($file))
    .$bound_last;


    if(mail($to, $subject, $message, $headers))
    {
    echo 'MAIL SENT';
    } else {
    echo 'MAIL FAILED';
    }

    ?>
     
    revathyelan, Jul 23, 2009 IP
  7. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Change your Content-Type and file name inside of the email header as appropiate. Word Docs use application/msword , you can find more here : http://www.iangraham.org/books/html4ed/appb/mimetype.html , you can likely just use a switch(condition variable) to change the mime type based on file extensions.

    You can also use $_FILES['uploadedfile']['name'] on the original uploaded file to grab the name of the file as the user uploaded it. Because right now your content-type is setup only as a file named attached.jpg.
     
    kblessinggr, Jul 23, 2009 IP
  8. revathyelan

    revathyelan Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Hi, Thank you very much. Now my problem is solved. thanks a lot...:)
     
    revathyelan, Jul 23, 2009 IP