I'm using the following code which you may recognise from php.net to delete all the emails in my inbox:
function deleteEmails($emailAddress)
{
// connect to gmail with your credentials
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = $emailAddress; # e.g somebody#gmail.com
$password = '****************';
// try to connect
$inbox = imap_open($hostname,$username,$password) or die('Cannot download information: ' . imap_last_error());
$check = imap_mailboxmsginfo($inbox);
echo "Messages before delete: " . $check->Nmsgs . "<br />\n";
imap_delete($inbox, '1:*');
$check = imap_mailboxmsginfo($inbox);
echo "Messages after delete: " . $check->Nmsgs . "<br />\n";
imap_expunge($inbox);
$check = imap_mailboxmsginfo($inbox);
echo "Messages after expunge: " . $check->Nmsgs . "<br />\n";
imap_close($inbox);
}
which helps deal with a clog in my account that happens on occasion if I let it get out of hand. However, what I really need is for it to delete all my email except the most recent one. I have tried to change imap_delete($inbox, '1:*'); to imap_delete($inbox, '2:*'); but this just caused it to not work at all.
What am I missing?
EDIT
With advise below I tried the following:
imap_delete($inbox, "2:$check->Nmsgs");
But interestingly it deleted all but one of the 'conversations' but in gmail 'conversations' can max out at 61 emails! I'm not sure how to get around this. Also, the deleted ones returned after a few mins...
I know gmail does some interesting stuff with just tagging it differently and putting it in an All Mail folder, since there aren't actual "Folders" like on most IMAP systems. Have you tried just giving it a range, such as
imap_delete($inbox,2:$check->Nmsgs);
If that doesn't work, you might have to just loop through marking them for deletion and then expunge after that.
I found the issue. Gmail by default is set up using 'conversations' meaning any emails coming in with the same subject get grouped together. This screws with everything but it can be turned off in gmail settings. Once done I needed to make sure I was deleting all but the most recent. using imap_delete($inbox,2:$check->Nmsgs); deleted all but the oldest. so the following code did it for me:
$emails = ($check->Nmsgs)-1;
imap_delete($inbox, '1:' . $emails);
so that I was getting the numbers but was deleting all up to the last one to come in (the most recent)
Done
Related
I have been using php-imap-client for some time and has always worked great. The issue is that recently it seems to break with fetching email from outlook/office365 mailboxes now and I can't pinpoint the issue. As far as I know it seems to be breaking with imap_fetchbody() but that is as far as I get.
getMessages() basically does not work and I was wondering if anyone has come across this yet and maybe can shed some light to a solution to fix it.
Things like countMessages and countUnreadMessages work fine but once you try get the email contents that is where it breaks.
$overallMessages = $imap->countMessages();
$unreadMessages = $imap->countUnreadMessages();
Here is all my code
$mailbox = $row['imap_server_address'];
$username = $row['imap_username'];
$password = $row['imap_password'];
$encryption = Imap::ENCRYPT_SSL; // TLS OR NULL accepted
// Open connection
try{
$imap = new Imap($mailbox, $username, $password, $encryption);
// You can also check out example-connect.php for more connection options
}catch (ImapClientException $error){
echo $error->getMessage().PHP_EOL; // You know the rule, no errors in production ...
die(); // Oh no :( we failed
}
// Select the folder INBOX
$imap->selectFolder('INBOX');
// Count the messages in current folder
$overallMessages = $imap->countMessages();
$unreadMessages = $imap->countUnreadMessages();
// Fetch all the messages in the current folder
echo "This echos fine";
$emails = $imap->getMessages();
echo "This does not echo";
var_dump($emails);
I can connect to the server just fine. I can select the INBOX, I can get overall messages and as well as unread. var_dump($email) produces nothing. And there are unread emails in the inbox.
When I echo after $emails = $imap->getMessages(); nothing is echo'd to the screen so something is breaking here.
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
So I'm making a registration/login/you know what I'm talking about system, and I need to verify the user's email addresses in case they forget their password. To do that, I assign them a random PIN which gets sent to them by email. Then, they enter that PIN in the account activation page and yay their account becomes active.
Now, my problem is the e-mail just isn't sending.
I'm using PHP 5.5 on Hostinger, my MX records aren't set up but from the questions that I've read about this, it's not what's causing the problem. Here is the code:
<?php
// connect to database, I'll omit the details because no one cares
$pin=rand(1000,9999);
$email=$_POST['email'];
$rawUser=$_POST['user'];
$user=// hash username but I'm not gonna tell you how because safety reasons
$rawPassA=$_POST['pass1'];
$rawPassB=$_POST['pass2'];
if($rawPassA==$rawPassB){
// hash password, again I'm not gonna tell you how
} else{
echo "<script type='text/javascript'>
window.location.href = 'http://komodokrew.ml/error/login/pass_no_match';
</script>";
};
$first=$_POST['first'];
$checkForUserQuery='SELECT count(*) FROM tableNameThatImNotGonnaTellYouBecauseSqlInjection WHERE user = \'' . $user . '\';';
$checkForUser=mysql_query($checkForUserQuery);
$checkForUserA=mysql_fetch_row($checkForUser);
$checkForUserF=$checkForUserA[0];
if($checkForUserF==0){
$query='INSERT INTO tableNameThatImNotGonnaTellYouBecauseSqlInjection (`user`,`first_name`,`pin`,`password`,`email`,`active`) VALUES (\'' . $user . '\',\'' . $first . '\',' . $pin . ',\'' . $pass . '\',\'' . $email . '\',1);';
mysql_query($query);
$subject='KomoDoKrew - account activation';
$message='You have recently registered an account on KomoDoKrew, under the username ' . $rawUser . '. Please activate your account by visiting komodokrew.ml/activate and entering the following PIN code, your username, and your password. PIN code: ' . $pin . '. Thank you for registering an account with KomoDoKrew! Make sure to join the community at komodokrew.ml/forum.';
$from='admin#komodokrew.ml';
$headers='From:' . $from;
mail($email,$subject,$message,$headers);
echo "<script type='text/javascript'>
window.location.href = 'http://komodokrew.ml/success/login/register';
</script>";
} else{
echo "<script type='text/javascript'>
window.location.href = 'http://komodokrew.ml/error/login/user_exists';
</script>";
};
mysql_close();
?>
Also, yes I KNOW that I'm vulnerable to SQL injections, I'm working on learning prepared statements and PDO, and yes I KNOW that mysql_* is way outdated, I'm working on learning the new PDO stuff, okay? But even if they SQL-injected me, it redirects them after they register anyways, and passwords and usernames are hashed with a varying salt several hundreds of thousands of times, so I'm not too worried at the moment.
This is the typical log that's sent in php_mail.log:
[23-Nov-2016 22:20:28 UTC] mail() on [/home/u964873932/public_html/register/register.php:40]: To: <myEmailAddressThatImNotGonnaTellYou> -- Headers: From:admin#komodokrew.ml
It's not a problem with PHP not being able to get the POST, because the email address of the user gets entered in the database successfully.
Thanks!
I would recommend to do 2 things first:
1. Enable error log:
error_reporting(-1);
ini_set('display_errors', 'On');
set_error_handler("var_dump");
2. And change the line where you are sending email
$status = mail($email,$subject,$message,$headers);
if($status)
{
echo '<p>Your mail has been sent!</p>';
} else {
echo '<p>Something went wrong, Please try again!</p>';
}
Then see if you got any error if no error then Follow this answer: PHP mail form doesn't complete sending e-mail
So I have the code below (and at the top the database connector and session start). However, he doesn't echo the amount of cubes (a valuta on my site).
$cubes = mysql_query("SELECT cubes FROM leden WHERE id='" . $_SESSION['id'] . "'");
echo "You currently have " . $cubes;
What is going wrong here? The database is there, the tables and everything exist.
The mysql_query function returns you a result set. So if you want to get the information you must also do :
$cubesRow = mysql_fetch_object($cubes);
echo "You currently have " . $cubesRow->cubes;
I am currently using TeamSpeak's ServerQuery feature to display all channels and connected users via PHP on my website. Right now it looks like this: (apologies for the crude usernames/channel titles)
It works to display channels and user names. However, I do not want it to do this.
Instead of showing all channels and user names that have connected, I would prefer it just to fetch the amount of users that are currently connected and the maximum amount of users that can connect and display them as seen above. (Along with the server status, i.e online or offline.)
This is the API I am using to connect to the TeamSpeak server via PHP.
Discovered a solution by myself!
Framework
TeamSpeak PHP Framework.
We only really need the libraries folder for this situation, so feel free to delete the docs and images folders.
--
PHP (Thanks to SilentStorm)
<?php
date_default_timezone_set("Europe/London");
require_once("libraries/TeamSpeak3/TeamSpeak3.php");
TeamSpeak3::init();
header('Content-Type: text/html; charset=utf8');
$status = "offline";
$count = 0;
$max = 0;
try {
$ts3 = TeamSpeak3::factory("serverquery://<USER>:<PASSWORD>#<SERVER IP>:<QUERY PORT>/?server_port=<SERVER PORT>&use_offline_as_virtual=1&no_query_clients=1");
$status = $ts3->getProperty("virtualserver_status");
$count = $ts3->getProperty("virtualserver_clientsonline") - $ts3->getProperty("virtualserver_queryclientsonline");
$max = $ts3->getProperty("virtualserver_maxclients");
}
catch (Exception $e) {
echo '<div style="background-color:red; color:white; display:block; font-weight:bold;">QueryError: ' . $e->getCode() . ' ' . $e->getMessage() . '</div>';
}
echo '<span class="ts3status">TS3 Server Status: ' . $status . '</span><br/><span class="ts3_clientcount">Clients online: ' . $count . '/' . $max . '</span>';
?>
Customise
- ServerQuery username (Can be found in TeamSpeak, Tools -> ServerQuery Login
- ServerQuery password (Can be found in TeamSpeak, Tools -> ServerQuery Login
- The server's IP address
- The ServerQuery port (Default - 10011)
- The server's port (Default - 9987)
Save the file appropriately, in the same directory that includes the libraries folder. To display it on a page put the code:
<?php
include('path/to/file/filename.php');
?>
This will then display the TeamSpeak server information on the page! Hope I could help.
I am working on a Cron job script that collects Unseen emails from Google imap server to my database. But sometimes, some emails are not read, so they don't get saved into the database.
Here is the code:
$connection = imap_open ($imapaddressandbox, $imapuser, $imappassword)
or die("Can't connect to '" . $imapaddress .
"' as user '" . $imapuser .
"' with password '" . $imappassword .
"': " . imap_last_error());
$m_search=imap_search ($connection, 'UNSEEN');
if($m_search === false){
email_log("No New Messages ");
}
It seams like for some reason some emails get skipped although they are unread.
Can anyone have an idea why?
Just a note, email is like me#mydomain.com, but using google email.
Thanks
use
imap_open($incoming_server,$username, $password,FT_PEEK);
Try
if (!$m_search) {
// No new mails found
}