How to use IMAP in PHP to fetch mail body content?
Asked Answered
B

2

8

I can't fetch email body content.

This is my code

<?php
/* connect to server */
$hostname = '{myserver/pop3/novalidate-cert}INBOX';
$username = 'username';
$password = 'password';

/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Tiriyo: ' . imap_last_error());
//echo $inbox;
/* grab emails */
$emails = imap_search($inbox,'ALL');


/* if emails are returned, cycle through each... */
if($emails) {

  /* begin output var */
  $output = '';

  /* put the newest emails on top */
  rsort($emails);

  /* for every email... */
  foreach($emails as $email_number) {
    //$email_number=$emails[0];
//print_r($emails);
    /* get information specific to this email */
    $overview = imap_fetch_overview($inbox,$email_number,0);
    $message = imap_fetchbody($inbox,$email_number,2);

    /* output the email header information */
    $output.= '<div class="toggler '.($overview[0]->seen ? 'read' : 'unread').'">';
    $output.= '<span class="subject">'.$overview[0]->subject.'</span> ';
    $output.= '<span class="from">'.$overview[0]->from.'</span>';
    $output.= '<span class="date">on '.$overview[0]->date.'</span>';
    $output.= '</div>';

    /* output the email body */
    $output.= '<div class="body">'.$message.'</div>';
  }

  echo $output;
}

/* close the connection */
imap_close($inbox);
?>

When I use to fetch email from Gmail, the email body content is displayed, but once I use my mail server, I can't fetch the body content of email.

Can you help me fix this problem?

Bloodfin answered 3/3, 2011 at 7:18 Comment(4)
What mail server are you using?Ruelu
Tested it; your script is working fine with Gmail as you said. The errors are probably coming from your mail server. Are there any other details you can provide?Ruelu
Probably a duplicate of #5170989Ruination
Try github.com/MonstaApps/PHP-IMAP-Fetcher. Pipe or fetch emails, log to MySQL, and save attachments.Pugilist
B
46

I had found solution, Error is with this line

$message = imap_fetchbody($inbox,$email_number,2);

Now, I am using

$message = imap_fetchbody($inbox,$email_number, 1.2);

To receive body content in text/html format

Below i had given datails of available options. This might some one

()Root Message Part (multipart/related)
(1) The text parts of the message (multipart/alternative)
(1.1) Plain text version (text/plain)
(1.2) HTML version (text/html)
(2) The background stationary (image/gif)
Bloodfin answered 4/3, 2011 at 10:7 Comment(2)
Thanks, I've been searching the exact same thing... That's why is also good to check the docs jaja... I just copy/paste an example from Internet and expected to work...Mandrake
look up fetch_structure, u need to decode the message if it is encoded.Presa
S
0

The Zeta Mail component allows much more convenient fetching of mails from IMAP and POP and can parses the incoming emails into a nice and clean object structure so you can handle it easily.

Stallard answered 3/3, 2011 at 7:38 Comment(3)
He's using POP, not IMAP. Check the first parameter to the imap_open call.Ruination
Sorry, got that wrong. However, works with POP too: incubator.apache.org/zetacomponents/documentation/trunk/Mail/…Stallard
I updated my answer to reflect that IMAP and POP is both possible.Stallard

© 2022 - 2024 — McMap. All rights reserved.