How to extract inline images (not attachment) from email using imap in PHP

When I receive email using imap in php, I get the contents of the email body, but I cannot extract inline images that are inserted and not attached to the body of the email.

+2
source share
2 answers

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); ?> 
0
source

Search for images in the body of email content.

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

 preg_match('/< *img[^>]*src *= *["\']?([^"\']*)/i', $body, $matches); 
+2
source

Source: https://habr.com/ru/post/947137/


All Articles