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);
?>
Related
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?
I´m working on a script in PHP that reads all mails in INBOX, detects special mails via Header-Info and moves processed mails into a folder. I want to exclude this folder from further reading, at the moment my script also finds mails that are already in the processed folder. I´m using the imap_open function like this:
$host = "{" . HOST . ":" . PORT . "/imap/ssl/novalidate-cert}INBOX";
$email = EMAIL;
$password = PASSWORD;
$imap = imap_open($host, $email, $password) or die("Cannot Connect " . imap_last_error());
$emailIds = imap_search($imap, 'FROM ' . SCAN_EMAIL_FOR_OFFERS, SE_UID);
if (!empty($emailIds)) {
foreach ($emailIds as $emailId) {
$email = new readMail();
$header = $email->get_headerinfo($imap, $emailId);
if (strpos($header->subject, "Angebot Nr.") != false) {
...Doing stuff...
$email->move_mail($imap, $emailId, MOVE_PROCESSED_TO_FOLDER);
}
}
} else {
echo "No new offers.";
}
imap_close($imap);
Regards
Edit: This is the move-func of readMail-Class...
function move_mail($imap, $uid, $inboxFolder) {
$msgNo = imap_msgno($imap, $uid);
$movingMail = imap_mail_move($imap, $msgNo, 'INBOX/'. $inboxFolder);
if($movingMail == false) {die(imap_last_error());}
}
I tried to open my mail server with imap_open(). I always get this error:
Warning: imap_open(): Couldn't open stream {sslin.df.eu:993/novalidate-cert}
I tried this code:
{sslin.df.eu:993/novalidate-cert}INBOX
{sslin.df.eu:993}INBOX
{sslin.df.eu:993}
{sslin.df.eu:993/imap/ssl/novalidate-cert}
{sslin.df.eu:993/imap/ssl/novalidate-cert}INBOX
{sslin.df.eu:993/imap/novalidate-cert}INBOX
{sslin.df.eu:993/novalidate-cert}INBOX
my current code:
//The location of the mailbox.
$mailbox = '{sslin.df.eu:993/novalidate-cert}INBOX.';
//The username / email address that we want to login to.
$username = 'example#example.com';
//The password for this email address.
$password = 'xxx';
//Attempt to connect using the imap_open function.
$imapResource = imap_open($mailbox, $username, $password);
//If the imap_open function returns a boolean FALSE value,
//then we failed to connect.
if($imapResource === false){
//If it failed, throw an exception that contains
//the last imap error.
throw new Exception(imap_last_error());
}
//If we get to this point, it means that we have successfully
//connected to our mailbox via IMAP.
//Lets get all emails that were received since a given date.
$search = 'SINCE "' . date("j F Y", strtotime("-7 days")) . '"';
$emails = imap_search($imapResource, $search);
//If the $emails variable is not a boolean FALSE value or
//an empty array.
if(!empty($emails)){
//Loop through the emails.
foreach($emails as $email){
//Fetch an overview of the email.
$overview = imap_fetch_overview($imapResource, $email);
$overview = $overview[0];
//Print out the subject of the email.
echo '<b>' . htmlentities($overview->subject) . '</b><br>';
//Print out the sender's email address / from email address.
echo 'From: ' . $overview->from . '<br><br>';
//Get the body of the email.
$message = imap_fetchbody($imapResource, $email, 1, FT_PEEK);
}
}
I am trying to get a server script to use wp_mail() and send an email to a user. I am doing this as part of a password reset routine, and calling the script with Ajax from the user's side.
I am getting the following error from my server script:
Fatal error: Call to undefined function wp_mail() in /var/www/abc-php/pwd.php on line 57
I cannot seem to find an answer to my question, so maybe I am asking incorrectly. Is what I am doing possible?
My server script (pwd.php) contains:
<?php
$setFromId = "info#abc.com.au";
$setFromName = "Info # abc";
$replyToId = "info#abc.com.au";
$replyToName = "Info # abc";
$sendToEmail = base64_decode($_GET["ste"]); //the URL in AJAX call contains base64 encoded data
$resetPwd = base64_decode($_GET["rp"]);
$resetSalt = base64_decode($_GET["rs"]);
$param = $_GET["var"];
$username = "xxxxxxxxx";
$password = "xxxxxxxx";
$host = "localhost";
$database = "xxxxxxxxx";
$con = mysqli_connect($host, $username, $password, $database);
// Check connection
if (mysqli_connect_errno($con)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
} else {
switch ($param) {
case "PWDRES": // Query 1: Current Logged In Partner Details
$mysqli = new mysqli($host, $username, $password, $database);
if (mysqli_connect_errno()) {
echo '<p>Unable to connect to the server at this time to update and send password. Please contact SMA for help or try again.</p>';
} else {
$myquery = "UPDATE abc_users
SET `password` = SHA2('" . $resetPwd . $resetSalt . "', 512),
`salt_value` = SHA2('" . $resetSalt . "', 512)
WHERE email_user_id = '" . $sendToEmail . "'";
mysqli_query($mysqli, $myquery);
if($mysqli->affected_rows >= 1) {
$headers = 'Reply-To: <' . $replyToName . '> ' . $replyToId;
$subject = "Password Reset - Confidential";
$body = "Subject: " . $subject . "<br/><br/>Your new password is: <br/><strong>" . $resetPwd . "</strong><br/><br/>Please ensure you reset this password next time you log in.";
$mail = wp_mail($sendToEmail, $subject, $body, $headers);
echo "Password was successfully reset and sent to " . $sendToEmail;
} else if($mysqli->affected_rows == 0) {
echo "No user id row was updated in the process of resetting password. Please try again.";
} else if($mysqli->affected_rows < 0) {
echo "There was an SQL error updating the user id password table. Please contact SMA for help.";
};
};
break;
case "OTHER1"
etc, etc, etc...
default;
};
};
unset($con);
unset($myquery);
unset($mysqli);
?>
Any help is appreciated.
You need to include wp-load.php in your file....
Just put this in top of your file.
require_once( dirname(__FILE__) . '/wp-load.php' );
The problem is you are not including Wordpress, so you are not using it at all (and it isn't including its libraries).
Plus, this is unrelated to the question, but you have a weakness in your code :
Any users can define the recipient of the email.
I am using imap_open to connect to my hotmail account. Now I want to check list of all folders like inbox, junk, sent etc. using imap_list() like this.
<?php
$mbox = imap_open("{pop3.live.com:995/pop3/ssl}", "username", "password")
or die("can't connect: " . imap_last_error());
$boxes = imap_list($mbox, '{pop3.live.com:995/pop3/ssl}', '*');
print_r($boxes);
imap_close($mbox);
?>
but it shows only Inbox. Actually I want to check mails in the junk folder.
Try
$username = 'username';
$password = 'password';
$server = '{imap-mail.outlook.com:993/ssl}';
$connection = imap_open($server, $username, $password);
$mailboxes = imap_list($connection, $server,'*');
print_r(imap_errors());
print_r($mailboxes);
imap_close($connection);
It's worked for me and hope it'll help someone :)
This Code Is Not My Own As I Pulled It From php.net. But I Can Say It Will Work.
<?php
//check for new messages
$mailbox = imap_open("{localhost/pop3:110}INBOX",
"#username#","#password#");
// Check messages
$check = imap_check($mailbox);
print("<PRE>");
print("Date most recent message : " . $check->Date);
print("<BR>");
print("Connection type : " . $check->Driver);
print("<BR>");
print("Name of the mailbox : " . $check->Mailbox);
print("<BR>");
print("Number of messages : " . $check->Nmsgs);
print("<BR>");
print("Number of recent messages : " . $check->Recent);
print("<BR>");
print("</PRE>");
// show headers for messages
$index=1;
$header = imap_header($mailbox, $index);
print("<PRE>");
print("Header Date : " . $header->Date . "<BR>");
print("Header To : " . $header->to) . "<BR>";
print("Header From : " . $header->From . "<BR>");
print("Header cc : " . $header->cc . "<BR>");
print("Header ReplyTo : " . $header->ReplyTo . "<BR>");
print("Header Subject : " . $header->Subject . "<BR></PRE>");
print("<PRE>");
print(imap_body($mailbox,$index));
print("</PRE><HR>");
imap_close($mailbox);
?>
Hope That Helps A Bit.
change imap_list($mbox, '{pop3.live.com:995/pop3/ssl}', '*'); to imap_list($mbox, '{pop3.live.com}', '*');
no need for port or connection protocol...