How to extract inline images(not attachment) from an email using imap in PHP [closed]
Asked Answered
H

2

2

When i fetch email using imap in php, i get email body content but i m not able to extract inline images pasted and not attached in the email body.

Hamsun answered 22/2, 2013 at 12:30 Comment(1)
you should provide the corresponding code you use to fetch the emails (without the username and password included ). without that it is not possible to help you because it is not known what functions/library you use.Idonah
H
1

hey i think i got a solution

<?php
   $hostname = '{myserver/pop3/novalidate-cert}INBOX';
   $username = 'username';
   $password = 'password';

   /* try to connect */
   $inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Tiriyo: ' . imap_last_error());
   $msgno = 0; //message id
   $no_of_occurences = 0;
   $intStatic = 2;//to initialize the mail body section

   $decode = imap_fetchbody($mbox, $msgno , "");    
   $no_of_occurences = substr_count($decode,"Content-Transfer-Encoding: base64");//to get the no of images
   if($no_of_occurences > 0){
        for($i = 0; $i < $no_of_occurences; $i++){  
             $strChange =   strval($intStatic+$i);
             $decode = imap_fetchbody($mbox, $msgno , $strChange);//to get the base64 encoded string for the image
             $data = base64_decode($decode);
             $fName = time()."_".$strChange . '.gif';
             $file = $fName;
             $success = file_put_contents($file, $data);        //creates the physical image    
        }           
    }
    imap_close($inbox);
 ?>
Hamsun answered 25/2, 2013 at 8:53 Comment(0)
B
2

Search for image in the body of the email content.

Suppose your email body is $body then you could get the image url using preg_match. Use this preg_match expression to get the image source.

preg_match('/< *img[^>]*src *= *["\']?([^"\']*)/i', $body, $matches);
Belldame answered 22/2, 2013 at 12:40 Comment(0)
H
1

hey i think i got a solution

<?php
   $hostname = '{myserver/pop3/novalidate-cert}INBOX';
   $username = 'username';
   $password = 'password';

   /* try to connect */
   $inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Tiriyo: ' . imap_last_error());
   $msgno = 0; //message id
   $no_of_occurences = 0;
   $intStatic = 2;//to initialize the mail body section

   $decode = imap_fetchbody($mbox, $msgno , "");    
   $no_of_occurences = substr_count($decode,"Content-Transfer-Encoding: base64");//to get the no of images
   if($no_of_occurences > 0){
        for($i = 0; $i < $no_of_occurences; $i++){  
             $strChange =   strval($intStatic+$i);
             $decode = imap_fetchbody($mbox, $msgno , $strChange);//to get the base64 encoded string for the image
             $data = base64_decode($decode);
             $fName = time()."_".$strChange . '.gif';
             $file = $fName;
             $success = file_put_contents($file, $data);        //creates the physical image    
        }           
    }
    imap_close($inbox);
 ?>
Hamsun answered 25/2, 2013 at 8:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.