I am connection to a mailbox and getting header information for each message like this:
$mailbox = new PhpImapMailbox(
env('MAIL_IMAP_PATH') . env('MAIL_FOLDER'), // IMAP server and mailbox folder
env('MAIL_LOGIN'), // Username for the before configured mailbox
env('MAIL_PASSWORD') // Password for the before configured username
);
$mailsIds = $mailbox->searchMailbox('ALL');
foreach($mailsIds as $mail_elem) {
$mail = $mailbox->getMail($mail_elem);
}
getMail
gives me all the header infos without the body. I have checked now every single method which exists on $mailbox->
and there is no way to get the body. What am I doing wrong here?
Second approach is to use the stream from imap_open()
and imap_fetchbody()
. This feels more like a workarround because I connect a second time to the mailbox, but is also does not work:
foreach($mailsIds as $mail_elem) {
$imap_stream = imap_open(env('MAIL_IMAP_PATH') . env('MAIL_FOLDER'),
env('MAIL_LOGIN'), env('MAIL_PASSWORD'));
$message = imap_fetchbody($imap_stream, $mail_elem, 1.1);
}
I am getting an error:
imap_fetchbody(): Bad message number
Someone has an idea what is going on?
2
Answers
You have to get the body with the
$mail
object not the$mailbox
object.This isn’t the cause but the third parameter of fetchbody is a string, not a float, so write it like:
Your issue is probably about $mail_elem that’s probably doesn’t match with the index of the imap. In case they are UID, you need to add the flag FT_UID in the 4th paramates of imap_fetchbody.
Anyaway if you are planning to use imap_* library, I advise you to use this wrapper: php-taniguchi (it’s quite documented and it has an example file).
The read() method is quite simple:
read(int $from, int $number, bool $details = false, bool $seen = false, bool $attachments = false)
Set all to true and you will get everything you want, it’s quite easy: