# getContent() isn't working as expected?

**URL:** <https://discourse.laminas.dev/t/getcontent-isnt-working-as-expected/2889>\
**Category:** Components & MVC\
**Tags:** laminas-mail\
**Created:** [May 21, 2022, 2:27pm UTC](https://discourse.laminas.dev/t/getcontent-isnt-working-as-expected/2889 "2022-05-21T14:27:34Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![michaelnoby](https://avatars.discourse-cdn.com/v4/letter/m/73ab20/32.png) [@michaelnoby](https://discourse.laminas.dev/u/michaelnoby)\
**Post date:** [May 21, 2022, 2:27pm UTC](https://discourse.laminas.dev/t/getcontent-isnt-working-as-expected/2889/1 "2022-05-21T14:27:35Z")

</div>

Hi, this is my working code:

```auto
$mail = new Imap([
    'host' => 'mail.provider.com',
    'port' => 993,
    'user' => 'user@domain.com',
    'password' => '123',
    'ssl' => 'SSL'
]);

echo $mail->countMessages() . " messages found\n";

foreach ($mail as $message) {
echo $message->from.'</br>';
echo $message->subject.'</br>';
}

```

Im later using this loop to store the data on a DB.  
But if i try to access the body or full source of the email, can’t find a way to do it.

I’ve tried:  
$message-\>getContent()  
$message-\>getBody()  
$message-\>getBodyText()

Also tried doing a loop with $message-\>getParts() and then $part-\>getRawContent().

Particularly with $message-\>getContent() i get `Undefined array key 0`.

**My intention is to at least save full email source (the whole plain source) in a DB (or in a .txt file on my server).**

On a second intention, would be to just store the whole `text/html` part on my DB.

Any pointers on how to do it?

Thanks!

---

<div class="post-metadata">

**Author:** ![ALTAMASH80](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/altamash80/32/895_2.png) [@ALTAMASH80](https://discourse.laminas.dev/u/ALTAMASH80)\
**Post date:** [May 29, 2022, 12:51pm UTC](https://discourse.laminas.dev/t/getcontent-isnt-working-as-expected/2889/2 "2022-05-29T12:51:12Z")

</div>

The document [here](https://docs.laminas.dev/laminas-mail/read/) suggests using “foreach” statement like below:

```auto
foreach ($mail as $messageNum => $message) {
    // do stuff ...
}

```

Or the other way would be like the below:

```auto
$totalMessages = $mail->countMessages();

for(int $index = 0; $index < $totalMessages; $index++)
{
  $message = $mail->getMessage($index);
  echo $message->getSubject() . '<br />';
  echo $message->getContent() . '<br />';
}

```

I hope it helps.
