3

I am writing a simple php script for reading emails.

When I extract mail body with:

imap_body($mbox, $i);

I have returned something like this:

--0016e6db2b334d4d7904a883f4ec 
Content-Type: text/plain; charset=ISO-8859-1 

<The message arrived> 

--0016e6db2b334d4d7904a883f4ec 
Content-Type: text/html; charset=ISO-8859-1 

<The message arrived> 

--0016e6db2b334d4d7904a883f4ec-- 

How can I extract only: ? I am using php_imap.

Thanks.

michele
  • 26,348
  • 30
  • 111
  • 168

2 Answers2

3

If you are using IMAP, you can use the imap_body() function to read the body. PHP has ton of functions that will help you with parsing an email.

http://www.php.net/manual/en/ref.imap.php

Check out the fetch_body() function too. It will allow you to get just part of the body.

http://www.php.net/manual/en/function.imap-fetchbody.php

For example:

$message = imap_fetchbody($inbox,$email_number, 1.2);
serialworm
  • 761
  • 3
  • 13
  • the problem is that the entire string is printed by imap_body($mbox, $i); – michele Jul 20 '11 at 18:25
  • Have you tried fetch_body()? `imap_fetchbody ( resource $imap_stream , int $msg_number , string $section [, int $options = 0 ] )` The section is the argument that you can send to get the main body. – serialworm Jul 20 '11 at 18:29
  • This may help. http://stackoverflow.com/questions/5177772/how-to-use-imap-in-php-to-fetch-mail-body-content – serialworm Jul 20 '11 at 18:34
  • I've edited my original answer with an example of how to the the html text from an email using fetch_body() – serialworm Jul 20 '11 at 18:38
0

If anyone in future asking same question, this code worked for me:

imap_fetchbody($this->connection, $sequence, 1);