The following code works to retrieve emails from my account (for privacy reasons I have blocked out the account I was actually using with xxxxxxxxxx but rest assured, it works).
The problem I'm facing here is that when I try to compare the value of one variable ($sender) to a string value (Facebook), the values never equate. Even though when I echo $sender, it echoes Facebook, but for some reason the code is not giving me the results that I intended when $sender == Facebook.
I've checked to make sure there are no unwanted spaces or anything. I suspect that I must do something to this $sender variable before I can compare it to a string, maybe it is not a string right now? I don't know what type of variable this is. Please Help!!!! Thanks!
<?php
/* connect to gmail */
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'xxxxxxxxxxxxxxxxxx@xxxxxxxxx';
$password = 'xxxxxxxxxx';
/* try to connect */
$inbox = imap_open($hostname, $username, $password) or die('Cannot connect to Gmail: ' . imap_last_error());
/* 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) {
/* 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 */
$sender = $overview[0]->from;
$subject = $overview[0]->subject;
?>
<div class="socUpdate">
[color=#FF0000]<?php
if($sender == "Facebook"){
echo $subject;
}else{
echo $sender . " has sent you an email.";
};
?>[/color]
</div>
<?php
}
}
/* close the connection */
imap_close($inbox);
?>
I have also tried using the trim function around the sender variable, but still no luck. Any ideas?