Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,704 members, 7,809,681 topics. Date: Friday, 26 April 2024 at 12:59 PM

Connecting To Your Gmail Account From Your Php Application. - Webmasters - Nairaland

Nairaland Forum / Science/Technology / Webmasters / Connecting To Your Gmail Account From Your Php Application. (1970 Views)

List Of Don'ts In A PHP Application / Post your PHP Problems Here / Sms Sending Php Application (2) (3) (4)

(1) (Reply) (Go Down)

Connecting To Your Gmail Account From Your Php Application. by cyberomin(m): 11:32am On Apr 16, 2009
Hi friends for quite sometimes i have been really busy working on stuffs,
well am glad am back, a good number of your will be wandering if this is actually achievable since google is python based, yes it's possible. Connecting to gmail is not rocket science believe me it's as simple as ABC, this is done through the IMAP* protocol*. Now let the lesson begin, the code will come first then explanation later.


<?php
$dns = "{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX";
$email = "my_email@gmail.com";
$password = "your_password";

$open = imap_open($dns,$email,$password ) or die("Cannot open stream ".imap_last_error());
if ($open)
{

    echo "You have ".imap_num_msg($open). " messages in your inbox\n\r";

        for($i=1; $i <= 5; $i++)
        {

            $header = imap_header($open,$i);
            echo $header->Subject." (".$header->Date."wink";


        }


    echo "\n\r";
    $msg = imap_fetchbody($open,1,"","FT_PEEK"wink;
    echo $msg;
    imap_close($open);


}
else
{


    echo "False";


}

?>


first we create our variables;
$dns = holds the location to the gmail inbox root, Gmail requires that the IMAP connection be made on port 993 and it must be an SSL* connection
$email is your gmail account id,
$password is your gmail account password,

after setting our variable, we establish a connection, using the imap function imap_open function, first the function accepts our $dns variable, followed by our email id and then password imap_open($dns,$email,$password ), we check to see if the connection was successful, if
it it was not we termainate it and print out a message followed by the last error that imap encountered, that way we know the cause of our problem we hold our connection in a variable called $open 'cos we will use it in multiple places.

If our open was successfull, we can proceed by getting the total no of messages in our inbox with the function imap_num_msg, this function accepts the connection string as it's parameter.

We then loop through to get the first five message in our inbox, using the for loop(control structure), imap_header helps us get the headers of our message like the sender name, subject and the date, it accepts our connection string and total no of message that we want to view as it's parameter, we them access our message subject and date the message was sent by using the Subject and the Date functions(method) respectively.

For us to read our first message we use the imap_fetchbody method this method accepts our connection handler and the message number, an optional parameter and a last parameter FT_PEEK, this parameter means don't mark this message as read, if it has never been read, that way if you had 5 inbox, and after reading the mail, it would still set the message flag as unread, we the print out our message and close our imap connection, this way we reduce server load.

IMAP - Internet Message Access Protocol
Protocol - This is a way in which computer identify themselves in a network
SSL - Secured socket layer.
method and function means the same thing.

Note: you will need to enable IMAP function in your PHP, so go to your PHP config file, php.ini and uncomment the imap function by removing the semi-colon before it ";".


I hope you all enjoy this brief lesson, next we will talk about how to delete a message from our gmail account, then subsequently how to compose and send a mail.

Drop all your comment and questions here.

Thank you all for your time.

I remain Cyberomin.
Re: Connecting To Your Gmail Account From Your Php Application. by segsalerty(m): 11:43am On Apr 16, 2009
THis is extraordinary ,
did you thought about this yourself ? this code syntax built by you ?
But , Thanks so much
Re: Connecting To Your Gmail Account From Your Php Application. by cyberomin(m): 12:05pm On Apr 16, 2009
@segsalerty , meaning what, if you are a programmer and you sit down to study, you can pretty much pull things through, any way thanks for dropping by.
Re: Connecting To Your Gmail Account From Your Php Application. by segsalerty(m): 1:11pm On Apr 16, 2009
cheesy Good

(1) (Reply)

Lagos January Web Programming Training At Give Away Price / Oracle 10g Study Guide, Question & Answers And Interactive Video Software / Acquisition Of Webhosting Company

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 15
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.