Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Jul 2002
    Location
    51° 03' -78" N -114° 05' 72" W
    Posts
    617
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Sending an image as attachment with php mail

    Hello All,

    I have a form that allows a user to enter there name email address subject and text area for comments I attached a spot to upload an image to be attached to the email. Yet I am having trouble with the image showing up on the other end in the email as a .dat file (phpXuBK7g.dat)??? here is some of the code that does this

    Code:
    <form enctype="multipart/form-data" method="post" action="http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'].'">
    Filename:
    <br />
    <input type="file" name="filename" size="30" />
    <input type="submit" name="submit" />
    </form>
    PHP Code:
    mosMail($_POST['*request_email'], $_POST['*request_name'], email@email.com', Submission'$body,'','',$_FILES['filename']['tmp_name'
    mosMail calls phpMail and sends the attachment which works.


    the other questions I have are follows?
    If I do
    PHP Code:
        echo "<PRE>";
        
    print_r($_POST);
        echo 
    "</PRE>"
    nothin shows up for the input type file? (I thought it was in FF and not IE but now it doesnt show in FF either)? Wondering if anyone can explain why its not showing up?

    If I do
    PHP Code:
        echo "<PRE>";
        
    print_r($_FILE);
        echo 
    "</PRE>"
    then I get information like

    PHP Code:
    Array
    (
        [
    oFile1] => Array
            (
                [
    name] => image8.jpg
                
    [type] => image/pjpeg
                
    [tmp_name] => /tmp/phprVw2s6
                
    [error] => 0
                
    [size] => 40241
            
    )


    so I know its there but if I do

    PHP Code:
    echo "<img src=\"".$_FILE['oFile1']['tmp_name']."/".$_FILE['oFile1']['name']."\"> 
    nothing shows up?

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,092
    Thanks
    2
    Thanked 322 Times in 314 Posts
    I am not directly familiar with mosMail, but the only thing you are passing to it is the temp name - $_FILES['filename']['tmp_name']. It is probably doing the best it can to use the passed name and since this does not have an extension, it is probably adding a default .dat

    If one of the parameters in the mosMail function call is the "name" to give the attachment, than this would allow you to give it the name you want. If not, than you probably need to copy/move_uploaded_file the file into a variable or a file within your web space so that you can give it the name you want.

    As to your other questions -

    The the $_POST array specifically does not contain anything from a type="file" form field, that is the purpose of the $_FILES array. Nothing is showing up because it is not supposed to. If something did appear at some point in your trials, it was probably because the form was not coded correctly.

    As soon as your upload form processing script finishes running, the $_FILES variables and the temp uploaded file are destroyed. So, it is not possible to put a reference to the $_FILES array into an <img src="..." tag. Even if you output this tag from your upload form processing script, the browser will fetch the image some time after the current script finishes running on the server. Another problem that will prevent this from working is that the URL in the <img src="...." tag must refer to a web accessible location so that the browser can fetch it. The default location for the temp uploaded file is not a public accessible location.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #3
    Regular Coder
    Join Date
    Jul 2002
    Location
    51° 03' -78" N -114° 05' 72" W
    Posts
    617
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hello,

    Thankyou for the reply.

    Ok well using the following

    $uploaddir = 'uploads/';
    $uploadfile = $uploaddir . basename($_FILES['filename']['name']);

    move_uploaded_file($_FILES['filename']['tmp_name'],$uploadfile);

    makes sense however I didnt want to actually store the images on the server just push them through the email, Is there a way of setting it to session or just a variable or do I have to purge all files in the directory everytime?

    here is the attached info for mosMail as you can see there is no name parameter for the attachment...
    Code:
    /**
    * Mail function (uses phpMailer)
    * @param string From e-mail address
    * @param string From name
    * @param string/array Recipient e-mail address(es)
    * @param string E-mail subject
    * @param string Message body
    * @param boolean false = plain text, true = HTML
    * @param string/array CC e-mail address(es)
    * @param string/array BCC e-mail address(es)
    * @param string/array Attachment file name(s)
    * @param string/array Reply-to e-mail address
    * @param string/array Reply-to name
    */
    Again thanks for the info!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •