email loss while using php imap - php

$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = '******#gmail.com';
$password = '******';
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
$emails = imap_search($inbox,'SUBJECT "string"');
$mail_ids = array();
if($emails) {
echo count($emails);
}
This code gives output of 309. But, when I search in gmail, using same keyword, I get 344 results.
Any idea where data is lost or where I am wrong?

$emails = imap_search($inbox,'BODY "string" SUBJECT "string"');
found more results as per #arnt's comment. Thanks :)

Related

how can i check how much i have downloaded from gmail using imap

I'm new to using imap with php to get message from gmail.
In gmail docs they said
Important: To avoid temporarily locking yourself out of your account, make sure you don't exceed 2500 MB per day for IMAP downloads and 500 MB per day for IMAP uploads. If you're setting up a single IMAP account on multiple computers, try taking a break between each setup.
Here's the link
I'm using this code to get mail from INBOX folder
// Connect to gmail
$imapPath = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'exampleusername';
$password = 'examplepassword';
// try to connect
$inbox = imap_open($imapPath, $username, $password) or die('Cannot connect to Gmail: ' . imap_last_error());
// 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 {
//
}
print_r($output);
$output = [];
}
// colse the connection
imap_expunge($inbox);
imap_close($inbox);
My question is how i can check for how much memory i had using for IMAP dowloads and for IMAP uploads?

PHP IMAP not fetching records from cpanel roundcube inbox

Screenshot
Connection timed out when tried to fetch emails from the cpanel roundcube.
All credentials are perfect and hostname and server also working fine.
$hostname = '{mail.server.com:993/novalidate-cert/imap/ssl}';
$username = 'username';
$password = 'password';
$stream = imap_open($hostname, $username, $password) or die('Cannot connect: ' . imap_last_error());
print_r(imap_errors());
Can anyone help please?
Thank you

Imap count all mails of email account

I'm trying to count all emails from and email but my script only count mails from inbox,
anyone know how to count all emails from the mail account including sent,spam,deleted, etc
$mailcnf = "mail.office365.com:993/imap/ssl/novalidate-cert";
$conn_str = "{".$mailcnf."}INBOX";
$username = 'test3#sjnewman.co.uk';
$password = 'Woju6532';
$imap = imap_open($conn_str,$username,$password) or die('Cannot connect to Server: ' . imap_last_error());
echo $message_count = imap_num_msg($imap);
first use imap_list to list all available folders.
then $conn_str = "{".$mailcnf."}$mailbox"instead of mailbox
imap_num_msg should return the number of emails in the current mailbox
You can loop through each folder and use imap_status() to count the number of emails in each folder. Here's an example:
<?php
$username = 'mail#example.com';
$password = 'password123';
// Define the connection string:
$server = '{server.example.net:993/ssl}';
// Connect to the server:
$connection = imap_open($server, $username, $password);
// List the mailboxes:
$mailboxes = imap_list($connection, $server, '*');
// Loop through the mailboxes:
foreach($mailboxes as $mailbox) {
$status = imap_status($connection, "$mailbox", SA_ALL);
if ($status) {
echo "Mailbox: $mailbox\t\tMessages: " . $status->messages . "\n";
} else {
echo "imap_status failed: " . imap_last_error() . "\n";
}
}
// Close the connection:
imap_close($connection);
?>

php imap function find bounce backs

So I have the basic problem of a mailing list and I am trying to find all the undeliverable emails and turn them off. I am using php imap function and I have been going through all the fetch and structure functions and although they do give you the information is there anything that concisely will say this is a bad email based on the error code. Currently my best option is using imap_fetchbody with option 2 and doing a find for 550 or 5.4.4 or 5.1.1 etc and then grabbing the email. I wanted to know if there was a function that tells you bad good or at least just has the above codes 550, 5.4.4 etc in a concise array format.
set_time_limit(0);
$hostname = "{localhost:110/pop3/novalidate-cert}INBOX";
$username = 'xxxx';
$password = "xxxx";
$inbox = imap_open($hostname, $username, $password) or die("Cannot connect to pop: " . imap_last_error());
$emails = imap_search($inbox, 'ALL');
if($emails){
$output = '';
rsort($emails);
foreach($emails as $key1 => $email_number){
if($key1 < 10){
$imap_fetchstructure = imap_fetchstructure($inbox, $email_number);
$imap_fetchbody = imap_fetchbody($inbox, $email_number, 1);
$imap_fetch_overview = imap_fetch_overview($inbox, $email_number);
$imap_bodystruct = imap_bodystruct($inbox, $email_number, 1);`
} else{
break;
}
}
}
imap_close($inbox);

Getting Error like imap_open(): Couldn't open stream in server

To fetch gmail mails, i am using below code. it is working fine in local, but i am getting error in server like :
Warning: imap_open(): Couldn't open stream {imap.googlemail.com:993/imap/ssl/novalidate-cert}INBOX in C:\xampp\htdocs\criticaloglive\email_real.php on line 10
Cannot connect to Gmail: Too many login failures
Here's my code :
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$server = '{imap.googlemail.com:993/imap/ssl/novalidate-cert}INBOX';
$username = 'mymail#gmail.com';
$password = 'mypassword';
// try to connect
$inbox = imap_open($server,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
Use the $hostname instead of $server. also try with not secure imap with port 143
$inbox = imap_open($hostname,$username,$password) ;
this code works for me.

Categories