I’m looking for a way to download the whole raw email data (including attachment), similar to what you get by clicking “Show Original” in Gmail.
Currently, I can get the raw header and some parts of the mail body by this code:
$this->MailBox = imap_open($mailServer, $userName, $password, OP_SILENT);
...
$email->RawEmail = imap_fetchbody($this->MailBox, $msgNo, "0");
$email->RawEmail .= "\n".imap_fetchbody($this->MailBox, $msgNo, "1");
Also I know that changing the 3rd parameter of imap_fetchbody might return the encoded attachment. Guess I need a loop here to get the raw email part by part, but what’s the condition to stop the loop?
Is there an easy way to get the whole email at once?
Any help would be appreciated.
If you want to fetch all, you need to fetch
imap_fetchheader.imap_fetchstructure.imap_fetchmime.imap_fetchbody.As mime-message can have multiple bodies, you need to process each part after the other. To reduce load to the server use the
FT_PREFETCHTEXToption forimap_fetchheader. Some example code aboutimap_fetchstructurein another answer that shows handling the imap connection and iterating over parts of a message already through encapsulation.