Showing posts with label google. Show all posts
Showing posts with label google. Show all posts

Reading Gmail With PHP IMAP

Posted by Unknown On Tuesday, 8 January 2013 42 comments


Hello dear friends .Our new php snippet is simple code for loading gmail messages using IMAP. Here using php imap extension to use inbox. Before you proceding this project make sure about your imap setting in Gmail. 






To start with we should have the following minimum requirements
  • PHP5
  • IMAP enabled in your Gmail settings.
  • PHP IMAP Extension is enabled
 
Configuration with mail.


               /* connect to gmail with your credentials */
                $hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
                $username = 'YOUR_GMAIL_USERNAME'; # e.g somebody@gmail.com
                $password = 'YOUR_GMAIL_PASSWORD';
 


Connection using Gmail’s IMAP.


           /* try to connect */
               $inbox = imap_open($hostname,$username,$password) or 
               die('Cannot connect to Gmail: ' . imap_last_error());
 
 
 

Source code.


    
<?php
/**
 *	Uses PHP IMAP extension, so make sure it is enabled in your php.ini,
 *	extension=php_imap.dll
  */
 set_time_limit(3000); 
 /* connect to gmail with your credentials */
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'YOUR_GMAIL_USERNAME'; # e.g somebody@gmail.com
$password = 'YOUR_GMAIL_PASSWORD';
/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
$emails = imap_search($inbox,'ALL');
/* useful only if the above search is set to 'ALL' */
$max_emails = 16;
/* if any emails found, iterate through each email */
if($emails) {
    $count = 1;
        /* 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);
        /* get mail message */
        $message = imap_fetchbody($inbox,$email_number,2);
       echo $message; 
                
        if($count++ >= $max_emails) break;
    }
  } 
/* close the connection */
imap_close($inbox);
echo "Done";

?>
 
 

Download Source file

 
 
 
 
 


READ MORE

how send email using gmail’s smtp server with PHP

Posted by Unknown On Friday, 28 December 2012 5 comments

         This post explains you how to “Sending Mail using SMTP and PHP” using SMTP credentials. In this post will show you how to send email using Gmail’s SMTP server.



You need to install two pear packages, Mail and SMTP. Code is quite self-explanatory.

example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?php
require_once "Mail.php";

$from    = "mail1@gmail.com";
$to      = "mail2@gmail.com";
$subject = "Test Message!";
$body    = "Its working!";

$config=array(
    'host'      => 'ssl://smtp.googlemail.com',
    'port'      => 465,
    'auth'      => true,
    'username'  => 'gmail user name',
    'password'  => 'gmail password'
);

$headers=array(
    'From'      => $from,
    'To'        => $to,
    'Subject'   => $subject
);

$smtp = Mail::factory('smtp',$config);

$mail = $smtp->send($to, $headers, $body);

if(!(PEAR::isError($mail)))
{
    echo "Mail sent successfully!";
}

else
{
    echo $mail->getMessage();
}
?>
READ MORE

Fashion