How do i display or decode gmail inline image or message using imap in php

  email, gmail, imap, php

I’m working on the php script How do i display or decode gmail inline image or message using imap in php, currerntly it is showing decode code instead of showing inline images?

How I could check the encoding of the message or display inline images using imap_fetchbody (been doing this for about two hours, so lost) and then decode it properly.

Here My Code

 /* Connecting Gmail server with IMAP */
                                $connection = imap_open('{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX', "$email", "$password") or die('Cannot connect to Gmail: ' . imap_last_error());

                                /* Search Emails having the specified keyword in the email subject */
                                $emailData = imap_search($connection, 'ALL');



                                if (! empty($emailData)) {
                                    foreach ($emailData as $emailIdent) {


                                        $z++;

                                        $overview = imap_fetch_overview($connection, $emailIdent, 0);
                                        $message = imap_fetchbody($connection, $emailIdent, 1);
                                        $messageExcerpt = substr($message, 0, 150);
                                        $partialMessage = trim(quoted_printable_decode($messageExcerpt));
                                        $date = date("d F, Y", strtotime($overview[0]->date));

                                        //$email_number = imap_msgno($connection,$uid);

                                        $structure = imap_fetchstructure($connection, $emailIdent);

                                        $attachments = array();

                                        /* if any attachments found... */
                                        if(isset($structure->parts) && count($structure->parts))
                                        {
                                            for($i = 0; $i < count($structure->parts); $i++)
                                            {
                                                $attachments[$i] = array(
                                                    'is_attachment' => false,
                                                    'filename' => '',
                                                    'name' => '',
                                                    'attachment' => ''
                                                );

                                                if($structure->parts[$i]->ifdparameters)
                                                {
                                                    foreach($structure->parts[$i]->dparameters as $object)
                                                    {
                                                        if(strtolower($object->attribute) == 'filename')
                                                        {
                                                            $attachments[$i]['is_attachment'] = true;
                                                            $attachments[$i]['filename'] = $object->value;
                                                        }
                                                    }
                                                }

                                                if($structure->parts[$i]->ifparameters)
                                                {
                                                    foreach($structure->parts[$i]->parameters as $object)
                                                    {
                                                        if(strtolower($object->attribute) == 'name')
                                                        {
                                                            $attachments[$i]['is_attachment'] = true;
                                                            $attachments[$i]['name'] = $object->value;
                                                        }
                                                    }
                                                }

                                                if($attachments[$i]['is_attachment'])
                                                {
                                                    $attachments[$i]['attachment'] = imap_fetchbody($connection, $emailIdent, $i+1);

                                                    /* 3 = BASE64 encoding */
                                                    if($structure->parts[$i]->encoding == 3)
                                                    {
                                                        $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
                                                    }
                                                    /* 4 = QUOTED-PRINTABLE encoding */
                                                    elseif($structure->parts[$i]->encoding == 4)
                                                    {
                                                        $attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
                                                    }
                                                }
                                            }
                                        }

                                        /* iterate through each attachment and save it */
                                        foreach($attachments as $attachment)
                                        {
                                            if($attachment['is_attachment'] == 1)
                                            {
                                                $filename = $attachment['name'];
                                                if(empty($filename)) $filename = $attachment['filename'];

                                                if(empty($filename)) $filename = time() . ".dat";
                                                $folder = "attachment";
                                                if(!is_dir($folder))
                                                {
                                                    mkdir($folder);
                                                }
                                                $fp = fopen("./". $folder ."/". $emailIdent . "-" . $filename, "w+");
                                                fwrite($fp, $attachment['attachment']);
                                                fclose($fp);
                                            }
                                        }

Currently Message Example

enter image description here
enter image description here

Source: Ask PHP

LEAVE A COMMENT