March 29, 2012
PHP script - retrieve email from your Gmail account
This script can be used to retrieve recent emails from your Gmail account. It was developed in hurry and is meant to provide a sort of conceptual base for more advanced scripts/programs.Usage:
Just declare some variables in the top section of the script and run it. You can also extend it to do advance stuffs like read and automatically delete spam mails, create auto responders etc.
Requirement:
Your server must have imap extension.
- <?php
- session_start() ;
- //error_reporting(-1);
- error_reporting( 0 ) ;
- // DEFINE VARIABLES.
- $pass = "ZZZZZ" ; // password to access this page.
- $gmail_username = "myusername@gmail.com" ; // your google account username.
- $gmail_password = "mypassword" ; // your google account password.
- $perpage = 20 ; // number of mails to be display per page.
- if ( isset( $_POST['pass'] ) )
- {
- if ( $pass == $_POST['pass'] )
- {
- $_SESSION['logged'] = 1 ;
- }
- else
- {
- echo "Invalid Login Details !" ;
- }
- }
- ?>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title>FETCH MAIL</title>
- <style type="text/css">
- body{font:12px sans-serif;background:rgb(210,210,250);}
- .topbar{background:rgb(150,0,250);font:15px cursive;font-weight:bold;color:white;border:1px solid silver;padding:5px;}
- .message{background:rgb(255,255,255);border:1px solid rgb(0,0,155);padding:5px;}
- .mailbox{border:1px dashed silver;padding:5px;}
- input,submit,select{padding:3px;font:12px cursive;font-weight:bold;background:transparent;border:1px solid rgb(50,50,50);}
- .header{font:40px 'script mt bold';font-weight:bold;color:purple;}
- </style>
- </head>
- <body>
- <?php
- if ( ! isset( $_SESSION['logged'] ) && ! isset( $_POST['pass'] ) )
- {
- exit( "Please Log In<form method='post'><input type='password' name='pass'><input type='submit' value='Go'></form>" ) ;
- }
- $link = imap_open( "{imap.gmail.com:993/imap/ssl}INBOX", $gmail_username, $gmail_password ) or exit( "Connection aborted: " . imap_last_error() ) ;
- $mails = imap_search( $link, 'ALL' ) ;
- rsort( $mails ) ;
- $total = count( $mails ) ;
- if ( isset( $_GET['page'] ) )
- {
- if ( is_numeric( $_GET['page'] ) )
- {
- $begin = round( $_GET['page'], 0 ) * $perpage ;
- }
- else
- {
- $begin = $total ;
- }
- }
- else
- {
- $begin = $total ;
- }
- echo "<table class='main'><tr><td><h2 class='header'>FETCH EMAIL</h2></td></tr><tr><td>" ;
- for ( $i = $begin; $i >= $begin - $perpage; $i-- )
- {
- if ( $i <= $total )
- {
- $overview = imap_fetch_overview( $link, $i, 0 ) ;
- $message = imap_fetchbody( $link, $i, 2 ) ;
- $message = preg_replace( "@<style.+?</style>@is", "", $message ) ;
- $message = strip_tags( $message ) ;
- $read = ( $overview[0]->seen == 0 ) ? 'NOT READ YET' : 'ALREADY READ' ;
- echo "
- <div class='mailbox'>
- <br/>
- <div class='topbar'>
- [ $i ]. SUBJECT : {$overview[0]->subject} || SENT FROM: {$overview[0]->from} || SENT AT: {$overview[0]->date} || $read
- </div>
- <br/>
- <div class='message'>
- <pre>{$message}</pre>
- </div>
- </div>
- " ;
- }
- }
- echo "</td></tr></table>" ;
- // pagination
- echo "<table class='nav'><tr><td>" ;
- if ( $begin > 0 )
- {
- $pre = $begin - 1 ;
- echo "<form method='get' action=''>
- <input type='hidden' name='page' value='{$pre}' />
- <input type='submit' value='Previous' />
- </form>" ;
- }
- echo "</td><td>" ;
- if ( $begin < $total )
- {
- $nxt = $begin + 1 ;
- echo "<form method='get' action=''>
- <input type='hidden' name='page' value='{$nxt}' />
- <input type='submit' value='Previous' />
- </form>" ;
- }
- echo "</td><td>" ;
- $factor = round( $total / $perpage, 0 ) ;
- echo "<form method='get' action=''>
- <select name='page'>
- " ;
- while ( $factor >= 0 )
- {
- echo "<option value='{$factor}'>{$factor}</option>" ;
- $factor-- ;
- }
- echo "</select> <input type='submit' value='Goto Page'></form>" ;
- echo "</td></tr></table>" ;
- ?>
- </body>
- </html>
DOWNLOAD
Do inform me if you use it or released an extended version of this script. :)
Related Posts
Subscribe to:
Post Comments (Atom)
Hi, i thіnk that і saw you visitеd my ѕіte sο i came to “return the favοг”.
ReplyDeleteI am attеmpting to find things to enhanсе my
ωеbsitе!I suppose its ok
to use a few of your іdeаs!!
my wеbsitе payday loans
Also see my web site > payday loans