Search code examples
phpimagegmailimap

get embedded image from gmail with imap php


I have managed to display my emails using IMAP php library , without using any extra framework. the only problem I got is that I can not get embedded images. I can see the structure of my email with imap_fetchstrucure , that my image is somewhere in

[1] => stdClass Object ( [type] => 5 [encoding] => 3 [ifsubtype] => 1 [subtype] => JPEG [ifdescription] => 0 [ifid] => 1 [id] => <[email protected]> [bytes] => 2668 [ifdisposition] => 0 [ifdparameters] => 0 [ifparameters] => 1 [parameters] => Array ( [0] => stdClass Object ( [attribute] => name [value] => image002.jpg ) )

[email protected], how can I get/show this image from Gmail? is there any standart URL to get the image www.gmail.com/image002.jpg ?


Solution

  • After searching and researching , found the solution , I will not get in detail how to read the body, But in this example I have in the 4th email and 3rd part a .jpg image embedded :

        <?php
        $mbox = imap_open($hostname,$username,$password) 
                or die('Cannot connect to Gmail: ' .       imap_last_error());
        $message=imap_fetchbody($mbox, 4,3);
        $decoded_data = base64_decode($message);
        file_put_contents('image002.jpg',$decoded_data); 
    ?>
        <img src="image002.jpg">  
     <?php
    

    you have to save it first and then you can show it. Could not find a way to show it directly.