Home php IMAP and reading emails good and different using PHP imap_fetchbody

IMAP and reading emails good and different using PHP imap_fetchbody

Author

Date

Category

I never thought that something might add golovnyaka more than regular expressions. But no, it can make an attempt to write your own php client to retrieve mail from the box.

The problem: get letters via imap_fetchbody, we need to further determine the encoding.
we get a bedsheet

stdClass Object
(
[Type] = & gt; 1
[Encoding] = & gt; 0.
[Ifsubtype] = & gt; 1
[Subtype] = & gt; MIXED
[Ifdescription] = & gt; 0.
[Ifid] = & gt; 0.
[ifdisposition] = & gt; 0.
[ifdparameters] = & gt; 0.
[ifparameters] = & gt; 1
[Parameters] = & gt; Array.
  (
    [0] = & gt; Stdclass Object.
      (
        [attribute] = & gt; boundary
        [Value] = & gt; = -dsffdsfdsdsfsdf
      )
  )
[Parts] = & gt; Array.
  (
    [0] = & gt; Stdclass Object.
      (
        [Type] = & gt; 1
        [Encoding] = & gt; 0.
        [Ifsubtype] = & gt; 1
        [Subtype] = & gt; ALTERNATIVE
        [Ifdescription] = & gt; 0.
        [Ifid] = & gt; 0.
        [ifdisposition] = & gt; 0.
        [ifdparameters] = & gt; 0.
        [ifparameters] = & gt; 1
        [Parameters] = & gt; Array.
          (
            [0] = & gt; Stdclass Object.
              (
                [attribute] = & gt; boundary
                [Value] = & gt; = -ffdsfdsfdsfds
              )
          )
        [Parts] = & gt; Array.
          (
            [0] = & gt; Stdclass Object.
              (
                [Type] = & gt; 0.
                [Encoding] = & gt; 2.
                [Ifsubtype] = & gt; 1
                [Subtype] = & gt; PLAIN
                [Ifdescription] = & gt; 0.
                [Ifid] = & gt; 0.
                [Lines] = & gt; 1
                [bytes] = & gt; 74.
                [ifdisposition] = & gt; 0.
                [ifdparameters] = & gt; 0.
                [ifparameters] = & gt; 1
                [Parameters] = & gt; Array.
                  (
                    [0] = & gt; Stdclass Object.
                      (
                        [attribute] = & gt; Charset.
                        [Value] = & gt; UTF-8
                      )
                  )
              )
            [1] = & gt; Stdclass Object.
              (
                [Type] = & gt; 0.
                [Encoding] = & gt; 3.
                [Ifsubtype] = & gt; 1
                [Subtype] = & gt; HTML
                [Ifdescription] = & gt; 0.
                [Ifid] = & gt; 0.
                [Lines] = & gt; 3.
                [bytes] = & gt; 218.
                [ifdisposition] = & gt; 0.
                [ifdparameters] = & gt; 0.
                [ifparameters] = & gt; 1
                [Parameters] = & gt; Array.
                  (
                    [0] = & gt; Stdclass Object.
                      (
                        [attribute] = & gt; Charset.
                        [Value] = & gt; UTF-8
                      )
                  )
              )
          )
      )
    [1] = & gt; Stdclass Object.
      (
        [Type] = & gt; 3.
        [Encoding] = & gt; 3.
        [Ifsubtype] = & gt; 1
        [Subtype] = & gt; OCTET-STREAM
        [Ifdescription] = & gt; 0.
        [Ifid] = & gt; 1
        [Id] = & gt; 
[bytes] = & gt; 6850824.
        [ifdisposition] = & gt; 1
        [disposition] = & gt; attachment
        [ifdparameters] = & gt; 0.
        [ifparameters] = & gt; 1
        [Parameters] = & gt; Array.
          (
            [0] = & gt; Stdclass Object.
              (
                [attribute] = & gt; Name.
                [Value] = & gt; =? UTF-8? B? 0L / FDSFDSFDSFDSF =? =
              )
          )
      )
  )

)

We know that for correct body processing of the letter, we need to get encoding
But, if $ encoding = @ $ dsasdfs will work for some letters [1] - & gt; encoding; , then for others you will need $ encoding = @ $ dsasdfs [0] - & gt; encoding ; or for example $ encoding = @ $ dsasdfs [4] - & gt; encoding;
Since the number of objects is always different, and it depends, apparently, on the number of parts from which the letter consists (attachments, their number, the type of text of the letter, the type of investment and TP), then any attempts to predict how encoding us navigate to use

imap_utf8
imap_8bit
imap_binary.
Base64_Decode.

and some other functions, break in general the desire to do something.
Real Google for 5 hours did not give anything on my problem.

If someone really did the same garbage and decided this problem – I will be grateful for your help.


Answer 1

Apparently, too complicated question 🙂
I still found the answer
Thanks @makram
I will give the code here:

function getbody ($ UID, $ imap) {
$ Body = $ this- & gt; get_part ($ imap, $ uid, "text / html");
// If Html Body Is Empty, Try Getting Text Body
if ($ Body == "") {
  $ Body = $ this- & gt; get_part ($ imap, $ uid, "text / plain");
}
Return $ Body;
}
Function Get_Part ($ imap, $ UID, $ MimeType, $ STRUCTURE = FALSE, $ PartNumber = False) {
  If (! $ STRUCTURE) {
    $ STRUCTURE = IMAP_FETCHSTRUCTURE ($ IMAP, $ UID, FT_UID);
  }
  if ($ STRUCTURE) {
    if ($ mimeType == $ this- & gt; get_mime_type ($ structure)) {
      If (! $ partnumber) {
        $ PartNumber = 1;
      }
      $ text = imap_fetchbody ($ imap, $ uid, $ partnumber, ft_uid);
      Switch ($ STRUCTURE- & GT; Encoding) {
        CASE 3: RETURN IMAP_BASE64 ($ Text);
        CASE 4: RETURN IMAP_QPRINT ($ text);
        Default: Return $ Text;
      }
    }
  // Multipart.
  if ($ STRUCTURE- & GT; Type == 1) {
    Foreach ($ STRUCTURE- & GT; PARTS AS $ Index = & GT; $ Substruct) {
      $ prefix = "";
      If ($ PartNumber) {
        $ prefix = $ PartNumber. ".";
      }
      $ Data = $ this- & gt; get_part ($ imap, $ uid, $ mimetype, $ substruct,
        $ prefix. ($ index + 1));
      if ($ DATA) {
        Return $ Data;
      }
    }
  }
}
 RETURN FALSE;
}
Function Get_Mime_Type ($ STRUCTURE) {
  $ primarymimeType = Array ("Text", "Multipart", "Message", "Application",
    "Audio", "Image", "Video", "Other");
  if ($ structure- & gt; subtype) {
    return $ primaryMimetype [(int) $ structure- & gt; type]. "/". $ Structure- & gt; subtype;
  }
  return "TEXT / PLAIN";
}

https://stackoverflow.com/questions/25503969/locate -html-and-plain-text-using-imap-fetchstructure

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions