0

I am trying to get past an attachment and get the text in the body of a gmail.

I have tried:

$message = imap_qprint(imap_fetchbody($inbox,$email_number,""));

And have attempted using several different part #s, but if there is an attachment, even if there is a message in the body, all I get is text that looks like this:

gu1OyQR8H/cpWM3Vnmv2R9O+ymSvBRIImhqaK7EqPaH8GUtxwyfxHhc6JjiM6nmEyMzIJ/jcameckzqQL

I have tried to get all the part #s of the gmail, but I am not sure what the code is for that.

If I knew what the part # is for the body of the email I might be able to retrieve the text of the body


EDIT

Here is the code to access the gmail email:

<?php
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'user';
$password = 'pass';
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());

$emails = imap_search($inbox,"UNSEEN");

if($emails) {
$output = '';

rsort($emails);

foreach($emails as $email_number) {

$headerInfo = imap_headerinfo($inbox,$email_number);
$structure = imap_fetchstructure($inbox, $email_number);

$overview = imap_fetch_overview($inbox,$email_number,0);

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

$output .= 'Subject: '.$overview[0]->subject.'<br />';
$output .= 'Body: '.$message.'<br />';
$output .= 'From: '.$overview[0]->from.'<br />';
$output .= 'Date: '.$overview[0]->date.'<br />';
$output .= 'CC: '.$headerInfo->ccaddress.'<br />';

$status = imap_setflag_full($inbox, $overview[0]->msgno, "\\Seen \\Flagged");
}

echo $output;
}

imap_close($inbox);
?>

And here is the output:

Subject: Test
Body: 
From: Test Acct
Date: Thu, 30 May 2019 11:59:22 -0400
CC:

Notice that the Body above is empty, but in the email there is displayed text:

Dear Valued Partner,

Loan No.23322 has been successfully locked with a Final Price of -.821%. Please see the attached Lock Confirmation for details. You may also pick the document up on EASE. ...

However, in the source I see this instead:

RGVhciBWYWx1ZWQgUGFydG5lciwgPGJyLz4NCg0KPGJyLz4NCkxvYW4gTm8uMTIxOTE4NjIyMSBo
YXMgYmVlbiBzdWNjZXNzZnVsbHkgbG9ja2VkIHdpdGggYSBGaW5hbCBQcmljZSBvZiAtLjgyMSUu
IFBsZWFzZSBzZWUgdGhlIGF0dGFjaGVkIExvY2sgQ29uZmlybWF0aW9uIGZvciBkZXRhaWxzLiBZ
b3UgbW

So it appears as if the text in the body of the email is encrypted and has nothing to do with the IMAP code.

If I forward the email to the same email address, when I check that message, this line of code works:

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

because in the source of the forwarded email the text in the body of the email displays normally, without the original encryption

EDIT 2


Adding this line of code did the trick:

 $message1 = imap_base64($message);
user3053462
  • 39
  • 1
  • 4
  • Checkout https://stackoverflow.com/questions/32655874/cannot-get-the-body-of-email-with-gmail-php-api/32669447#32669447 – Ramesh May 30 '19 at 08:41
  • 1
    By "gmail", do you mean "e-mail"? More importantly, can you show us any of the message ([edit] it into the question, don't try to add it in a comment)? Without at least some representative extract of the content, all we could do is guess at what the problem is. – IMSoP May 30 '19 at 09:10

1 Answers1

0

For anybody who has the same issue, here is the code that works:

<?php
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'user';
$password = 'pass';
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());

$emails = imap_search($inbox,"UNSEEN");

if($emails) {
$output = '';

rsort($emails);

foreach($emails as $email_number) {

$headerInfo = imap_headerinfo($inbox,$email_number);
$structure = imap_fetchstructure($inbox, $email_number);

    $overview = imap_fetch_overview($inbox,$email_number,0);

    $message = imap_qprint(imap_fetchbody($inbox,$email_number,"1"));

//since the incoming email was encrypted with base64, I insert this line below to decode it, and the above part number, "1", will work. This was the key
$message1 = imap_base64($message);

$output .= 'Subject: '.$overview[0]->subject.'<br />';
$output .= 'Body: '.$message1.'<br />';
$output .= 'From: '.$overview[0]->from.'<br />';
$output .= 'Date: '.$overview[0]->date.'<br />';
$output .= 'CC: '.$headerInfo->ccaddress.'<br />';

$status = imap_setflag_full($inbox, $overview[0]->msgno, "\\Seen \\Flagged");
}

echo $output;
}

/* close the connection */
imap_close($inbox);
?>
user3053462
  • 39
  • 1
  • 4