Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

accessing my gmail inbox via php code

how i can access my gmail account through my php code? I need to get the subject and the from address to from my gmail account.And then i need to mark the accessed as read on gmail Should i use gmail pop3 clint?is that any framework that i can use for accessing gmail pop3 server.

like image 241
user156073 Avatar asked Sep 06 '09 14:09

user156073


People also ask

How can I retrieve mail from Gmail using PHP?

php /* connect to gmail */ $hostname = '{imap.gmail.com:993/imap/ssl}INBOX'; $username = 'my gmail id'; $password = 'my gmail password'; /* try to connect */ $inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' .

How can I get into my Gmail inbox?

On your computer, go to Gmail. Enter your Google Account email or phone number and password. If information is already filled in and you have to sign in to a different account, click Use another account. If you get a page that describes Gmail instead of the sign-in page, at the top right of the page, click Sign in.

What is PHP IMAP?

IMAP stands for Internet Mail Access Protocol, PHP-IMAP functions helps you to access an email account and fetch emails from them. Using these functions you can also work with NNTP, POP3 protocols and local mailbox access methods. With the help of this library you can create web applications that handle your emails.


2 Answers

I would just use the PHP imap functions and do something like this:

<?php
    $mailbox = imap_open("{imap.googlemail.com:993/ssl}INBOX", "[email protected]", "PASSWORD");
    $mail = imap_search($mailbox, "ALL");
    $mail_headers = imap_headerinfo($mailbox, $mail[0]);
    $subject = $mail_headers->subject;
    $from = $mail_headers->fromaddress;
    imap_setflag_full($mailbox, $mail[0], "\\Seen \\Flagged");
    imap_close($mailbox);
?>

This connects to imap.googlemail.com (googlemail's imap server), sets $subject to the subject of the first message and $from to the from address of the first message. Then, it marks this message as read. (It's untested, but it should work :S)

like image 138
Jerry Avatar answered Sep 18 '22 15:09

Jerry


This works for me.

<?php

$yourEmail = "[email protected]";
$yourEmailPassword = "your password";

$mailbox = imap_open("{imap.gmail.com:993/ssl}INBOX", $yourEmail, $yourEmailPassword);
$mail = imap_search($mailbox, "ALL");
$mail_headers = imap_headerinfo($mailbox, $mail[0]);
$subject = $mail_headers->subject;
$from = $mail_headers->fromaddress;
imap_setflag_full($mailbox, $mail[0], "\\Seen \\Flagged");
imap_close($mailbox);
?>
like image 38
Floyd Avatar answered Sep 19 '22 15:09

Floyd