Recently i have worked on language translation project for that i need to read emails using PHP From the gamil inbox based on the email content i had created projects in the database…fortunately reading email from the gmail was easier than i think thanks to IMAP extension.
Here is the simple example for retrieving emails from the gmail:
NOTE: This will work only if you meet the fallowing requirements
- PHP version should be PHP5 or latest
- PHP IMAP Extension should be enabled in your PHP installation
- IMAP should be enabled in your Gmail settings.
How to enable IMAP in PHP
How to Enable IMAP in XAMPP
IMAP is not enabled by default in Xampp distribution, so to enable it go to the file "\xampp\php\php.ini"
and search for ";extension=php_imap.dll"
and by removing the beginning semicolon at the line ,it’s get enabled ,it should be: extension=php_imap.dll
.
How to enable IMAP in Linux
You can install the PHP5 IMAP module with this command :
apt-get install php5-imap
However it’s not enabled by default so enable it with:
php5enmod imap
To see the changes ,restart Apache
service apache2 restart
Here is the PHP script for retrieve Your Gmail Emails Using IMAP.
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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
set_time_limit(4000); // Connect to gmail $imapPath = '{imap.gmail.com:993/imap/ssl}INBOX'; $password = 'your_gmail_password'; // try to connect $inbox = imap_open($imapPath,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error()); /* ALL - return all messages matching the rest of the criteria ANSWERED - match messages with the \\ANSWERED flag set BCC "string" - match messages with "string" in the Bcc: field BEFORE "date" - match messages with Date: before "date" BODY "string" - match messages with "string" in the body of the message CC "string" - match messages with "string" in the Cc: field DELETED - match deleted messages FLAGGED - match messages with the \\FLAGGED (sometimes referred to as Important or Urgent) flag set FROM "string" - match messages with "string" in the From: field KEYWORD "string" - match messages with "string" as a keyword NEW - match new messages OLD - match old messages ON "date" - match messages with Date: matching "date" RECENT - match messages with the \\RECENT flag set SEEN - match messages that have been read (the \\SEEN flag is set) SINCE "date" - match messages with Date: after "date" SUBJECT "string" - match messages with "string" in the Subject: TEXT "string" - match messages with text "string" TO "string" - match messages with "string" in the To: UNANSWERED - match messages that have not been answered UNDELETED - match messages that are not deleted UNFLAGGED - match messages that are not flagged UNKEYWORD "string" - match messages that do not have the keyword "string" UNSEEN - match messages which have not been read yet*/ // search and get unseen emails, function will return email ids $emails = imap_search($inbox,'UNSEEN'); $output = ''; foreach($emails as $mail) { $headerInfo = imap_headerinfo($inbox,$mail); $output .= $headerInfo->subject.'<br/>'; $output .= $headerInfo->toaddress.'<br/>'; $output .= $headerInfo->date.'<br/>'; $output .= $headerInfo->fromaddress.'<br/>'; $output .= $headerInfo->reply_toaddress.'<br/>'; $emailStructure = imap_fetchstructure($inbox,$mail); if(!isset($emailStructure->parts)) { $output .= imap_body($inbox, $mail, FT_PEEK); } else { // } echo $output; $output = ''; } // colse the connection imap_expunge($inbox); imap_close($inbox); |
I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.
I am Arjun from Hyderabad (India). I have been working as a software engineer from the last 7+ years, and it is my passion to learn new things and implement them as a practice. Aside from work, I like gardening and spending time with pets.
thanks its work for me.. 🙂
I need help pls help me
On server showing
Internal Server Error
The server encountered an internal error or
misconfiguration and was unable to complete
your request.
Please contact the server administrator at
[email protected] to inform them of the time this error occurred,
and the actions you performed just before this error.
More information about this error may be available
in the server error log.
Additionally, a 500 Internal Server Error
error was encountered while trying to use an ErrorDocument to handle the request.
get an error :
Warning: imap_open(): Couldn’t open stream {imap.gmail.com:993/imap/ssl}INBOX in C:xampphtdocskanuemails.php on line 10
Cannot connect to Gmail: Too many login failures
Notice: Unknown: Invalid credentials 36mb47183581iot (errflg=1) in Unknown on line 0
Notice: Unknown: Invalid credentials 36mb47183581iot (errflg=1) in Unknown on line 0
Notice: Unknown: Invalid credentials 36mb47183581iot (errflg=1) in Unknown on line 0
Notice: Unknown: Too many login failures (errflg=2) in Unknown on line 0
not working , could you please help me
Check your user name and password once . If possible share your email.php file
Array ( [0] => Can not authenticate to IMAP server: [ALERT] Please log in via your web browser: https://support.google.com/mail/acco ) Cannot connect to Gmail: 1
??wat to do
Hi arjun, your code works nice. However I want to retrieve entire body text. Currently it just retrieves a small part of the message text. What should I do to get the entire message body of the email?
can you share you code ? because imap_body() function will return entire message body.
Hey Friends
I need help pls anybody help me….
i am suffering last 3 days but not found any solution
I am using Codeigniter i need to store data from email reply.
Patient can question us and it send to doctor email and when doctor answer this question thn its also show our dashboard and store mysql database.
Please help me any body..
please
what is the issue here.
It says:
A PHP Error was encountered
Severity: Warning
Message: imap_open(): Couldn’t open stream {imap.gmail.com:993/imap/ssl}INBOX
Filename: controllers/Mail.php
Line Number: 64
Backtrace:
File: /home/rupinalanepal/email/application/controllers/Mail.php
Line: 64
Function: imap_open
File: /home/rupinalanepal/email/index.php
Line: 292
Function: require_once
Array ( [0] => Can not authenticate to IMAP server: [ALERT] Please log in via your web browser: https://support.google.com/mail/acco ) Cannot connect to Gmail: 1
what might be the solution?
Thank you!!!
Hi Arjun
I Enabled IMAP in my gmail account setting and tried your above code but not getting any output please help me out. I am new in PHP
Hello Arjun,
Thank you for the script.
I can’t sort the mails by order of date desc
Can you me help?
always getting maximum execution time of 30
please help me to fix this , iam using code igniter , and i want to store all emails in gmail to the view that i created ,
but thanks for your kindness to share it all
Hello guys,
I have implemented Gmail Imap thing and working fine but I have 1 question, is there any option to use datetime in imap_search function. I mean currently I am using this like
imap_search($kImap->mailbox,’SINCE 03 August 2017′);
but i want to use this like
imap_search($kImap->mailbox,’SINCE 03 August 2017 10:00′);
So basically i want to import all the email that was there in Gmail since 03 August 2017 10:00
Thanks,
Ajay
I guess you can only pass date, not date time
Hi Arjun.
Thanks for your reply, well is there any other way by which we can fetch email received as per datetime? Actually we are receiving too many emails in our Gmail inbox and i learn that if we fetch email for entire day then sometimes scripts breaks in between. So to fix this we are planing to schedule a cron script which will run in every 15 min and will fetch all the emails received in last 15 Min. Please suggest
Thanks,
Ajay
how do i display imap retrieved mails in descending order based on the received date.ie(recent received mail first followed by the older mail)
How do i echo mail body?
How do i get attached files and mail body with reply messages ?
please help me Arjun Sir i am getting this error again an agin please tell me what i have to do
Warning: imap_open(): Couldn’t open stream {imap.gmail.com:993/imap/ssl}INBOX in C:xampphtdocszoho2imap-mail.php on line 12
Array ( [0] => [ALERT] Please log in via your web browser: https://support.google.com/mail/accounts/answer/78754 (Failure) [1] => Too many login failures ) Cannot connect to Gmail: 1
If you are a gmail user then you have to enable below option in gmail.
https://myaccount.google.com/lesssecureapps?pli=1
please help me Arjun Sir i am getting this error again an agin please tell me what i have to do
Warning: imap_open(): Couldn’t open stream {imap.gmail.com:143/imap/ssl}INBOX in C:xampphtdocszoho2imap-mail.php on line 12
Array ( [0] => [ALERT] Please log in via your web browser: https://support.google.com/mail/accounts/answer/78754 (Failure) [1] => Too many login failures ) Cannot connect to Gmail: 1
how about the view page arjun?
I also get the error : Cannot connect to Gmail: Can not authenticate to IMAP server: [ALERT] Please log in via your web browser: https://support.google.com/mail/acco
please help.
Thanks.
I want to fetch gmail body with image please reply me as well as possible .