Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Read my gmail emails in PHP

Posted on 2009-05-20
7
Medium Priority
?
2,609 Views
Last Modified: 2013-11-29
I am using this litle snippet I found to collect the header of the unread emails in my gmail account but the "From" often shows as just a name and not as the email address it was from...

Anyone know how I could get that info also so it said "From: Name <emailaddress@whateverdomain.com>" ?


$m_username = "uname";
$m_password = "password";
$m_acs = 20;
 
$m_t = 2;
 
$m_mail = imap_open ("{imap.gmail.com:993/imap/ssl}INBOX", $m_username . "@gmail.com", $m_password)
 
or die("ERROR: " . imap_last_error());
 
$m_gunixtp = array(2592000, 1209600, 604800, 259200, 86400, 21600, 3600);
 
$m_gdmy = date('d-M-Y', time() - $m_gunixtp[$m_t]);
 
$m_search=imap_search ($m_mail, 'UNSEEN SINCE ' . $m_gdmy . '');
echo "<hr><center><font color=ffffff>Here are your first $m_acs emails</font></center>";
 
if($m_search < 1){
$m_empty = "No New Messages";}
else {
 
rsort($m_search);
 
if($m_acs > 0){
array_splice($m_search, $m_acs);
}
 
foreach ($m_search as $what_ever ) {
$fromaddress1 = $obj_thang->mailbox . "@" . $object->host;
$obj_thang = imap_headerinfo($m_mail, $what_ever);
 
echo "<div align=center><br /><font face=Arial size=2 color=#ffffff>Message ID# " . $what_ever . "</font>
 
<table bgcolor=#D3D3D3 width=700 border=1 bordercolor=#000000 cellpadding=0 cellspacing=0>
<tr>
<td><table width=100% border=0>
<tr>
<td><table width=100% border=0>
<tr>
<td bgcolor=#F8F8FF><font face=Arial size=2 color=#800000>Date:</font> <font face=Arial size=2 color=#000000>" . date("F j, Y, g:i a", $obj_thang->udate) . "</font></td>
<td bgcolor=#F8F8FF><font face=Arial size=2 color=#800000>From:</font> <font face=Arial size=2 color=#000000>" . $obj_thang->fromaddress . " or  $fromaddress1</font></td>
<td bgcolor=#F8F8FF><font face=Arial size=2 color=#800000>To:</font> <font face=Arial size=2 color=#000000>" . $obj_thang->toaddress . " </font></td>
</tr>
<tr>
</table>
</td>
</tr><tr><td bgcolor=#F8F8FF><font face=Arial size=2 color=#800000>Subject:</font> <font face=Arial size=2 color=#000000>" . $obj_thang->Subject . "</font></td></tr><tr>
</tr>
</table></td>
</tr>
</table>
$header = imap_header($mbox, $message_id);
$from = $header->from;
 
</div></body>";
 
}} echo "<div align=center><font face=Arial size=4 color=#800000><b>" . $m_empty . "</b></font></div>";
//close mailbox bi by bye
imap_close($m_mail);

Open in new window

0
Comment
Question by:Riviera1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 17

Expert Comment

by:Chris Harte
ID: 24430838
The header is an object. To select values you have to loop over them.
$header = imap_header($mbox, $message_id);
 
$from = $header->from;
 
foreach ($from as $id => $object) 
{
    $fromname = $object->personal;
    $fromaddress = $object->mailbox . "@" . $object->host;
}

Open in new window

0
 
LVL 4

Expert Comment

by:manavsi
ID: 24431078
the above is right... Just in case u need for info, chk this link: www.electrictoolbox.com/php-imap-download-email-gmail/

HTH
Manavsi
0
 

Author Comment

by:Riviera1
ID: 24431639
Hi MunterMan

Yes I found that solution thanks elsewhere but where would it fit into the existing script?

Would it also mark the mail as read?

Thx
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Accepted Solution

by:
Chris Harte earned 2000 total points
ID: 24431828
That should do it. Your email reader marks it as read or not, it is not a property of the email.

$m_username = "uname";
$m_password = "password";
$m_acs = 20;
 
$m_t = 2;
 
$m_mail = imap_open ("{imap.gmail.com:993/imap/ssl}INBOX", $m_username . "@gmail.com", $m_password)
 
or die("ERROR: " . imap_last_error());
 
$m_gunixtp = array(2592000, 1209600, 604800, 259200, 86400, 21600, 3600);
 
$m_gdmy = date('d-M-Y', time() - $m_gunixtp[$m_t]);
 
$m_search=imap_search ($m_mail, 'UNSEEN SINCE ' . $m_gdmy . '');
echo "<hr><center><font color=ffffff>Here are your first $m_acs emails</font></center>";
 
if($m_search < 1)
{
    $m_empty = "No New Messages";
}
else
{
 
    rsort($m_search);
 
    if($m_acs > 0)
    {
        array_splice($m_search, $m_acs);
    }
 
    foreach ($m_search as $what_ever )
    {
        $fromaddress1 = $obj_thang->mailbox . "@" . $object->host;
        $obj_thang = imap_headerinfo($m_mail, $what_ever);
 
         
        $from = $header->from;
 
        foreach ($from as $id => $object)
        {
            $fromname = $object->personal;
            $fromaddress = $object->mailbox . "@" . $object->host;
        }
 
 
 
        echo "<div align=center><br /><font face=Arial size=2 color=#ffffff>Message ID# " . $what_ever . "</font>
 
<table bgcolor=#D3D3D3 width=700 border=1 bordercolor=#000000 cellpadding=0 cellspacing=0>
<tr>
<td><table width=100% border=0>
<tr>
<td><table width=100% border=0>
<tr>
<td bgcolor=#F8F8FF><font face=Arial size=2 color=#800000>Date:</font> <font face=Arial size=2 color=#000000>" . date("F j, Y, g:i a", $obj_thang->udate) . "</font></td>
<td bgcolor=#F8F8FF><font face=Arial size=2 color=#800000>From:</font> <font face=Arial size=2 color=#000000>" . $obj_thang->fromaddress . " or  $fromaddress1</font></td>
<td bgcolor=#F8F8FF><font face=Arial size=2 color=#800000>To:</font> <font face=Arial size=2 color=#000000>" . $obj_thang->toaddress . " </font></td>
</tr>
<tr>
</table>
</td>
</tr><tr><td bgcolor=#F8F8FF><font face=Arial size=2 color=#800000>Subject:</font> <font face=Arial size=2 color=#000000>" . $obj_thang->Subject . "</font></td></tr><tr>
</tr>
</table></td>
</tr>
</table>               
 
        $fromname $fromaddress        
 
        </div></body>";
 
    }
}
echo "<div align=center><font face=Arial size=4 color=#800000><b>" . $m_empty . "</b></font></div>";
//close mailbox bi by bye
imap_close($m_mail);

Open in new window

0
 

Author Comment

by:Riviera1
ID: 24431967
Oops ;)

Warning: Invalid argument supplied for foreach()

At the line:

foreach ($from as $id => $object)


$m_acs = 20;
 
$m_t = 2;
 
$m_mail = imap_open ("{imap.gmail.com:993/imap/ssl}INBOX", $m_username . "@gmail.com", $m_password)
 
or die("ERROR: " . imap_last_error());
 
$m_gunixtp = array(2592000, 1209600, 604800, 259200, 86400, 21600, 3600);
 
$m_gdmy = date('d-M-Y', time() - $m_gunixtp[$m_t]);
 
$m_search=imap_search ($m_mail, 'UNSEEN SINCE ' . $m_gdmy . '');
echo "<hr><center><font color=ffffff>Here are your first $m_acs emails</font></center>";
 
if($m_search < 1)
{
    $m_empty = "No New Messages";
}
else
{
 
    rsort($m_search);
 
    if($m_acs > 0)
    {
        array_splice($m_search, $m_acs);
    }
 
    foreach ($m_search as $what_ever )
    {
        $obj_thang = imap_headerinfo($m_mail, $what_ever);
 
 
        $from = $header->from;
 
        foreach ($from as $id => $object)
        {
            $fromname = $object->personal;
            $fromaddress = $object->mailbox . "@" . $object->host;
        }
 
 
 
        echo "<div align=center><br /><font face=Arial size=2 color=#ffffff>Message ID# " . $what_ever . "</font>
 
<table bgcolor=#D3D3D3 width=700 border=1 bordercolor=#000000 cellpadding=0 cellspacing=0>
<tr>
<td><table width=100% border=0>
<tr>
<td><table width=100% border=0>
<tr>
<td bgcolor=#F8F8FF><font face=Arial size=2 color=#800000>Date:</font> <font face=Arial size=2 color=#000000>" . date("F j, Y, g:i a", $obj_thang->udate) . "</font></td>
<td bgcolor=#F8F8FF><font face=Arial size=2 color=#800000>From:</font> <font face=Arial size=2 color=#000000>" . $obj_thang->fromaddress . " or  $fromaddress1</font></td>
<td bgcolor=#F8F8FF><font face=Arial size=2 color=#800000>To:</font> <font face=Arial size=2 color=#000000>" . $obj_thang->toaddress . " </font></td>
</tr>
<tr>
</table>
</td>
</tr><tr><td bgcolor=#F8F8FF><font face=Arial size=2 color=#800000>Subject:</font> <font face=Arial size=2 color=#000000>" . $obj_thang->Subject . "</font></td></tr><tr>
</tr>
</table></td>
</tr>
</table>
 
</div></body>";
 
    }
}
echo "<div align=center><font face=Arial size=4 color=#800000><b>" . $m_empty . "</b></font></div>";
//close mailbox bi by bye
imap_close($m_mail);

Open in new window

0
 
LVL 17

Expert Comment

by:Chris Harte
ID: 24433055
That will teach me to cut and paste
Replace
$from = $header->from;

with
$from = $obj_thang->from;
0
 

Author Closing Comment

by:Riviera1
ID: 31583451
Great help thanks :)
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question