php IMAP connect to hotmail - php

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...

Related

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 block after html not executing

I have two blocks of code in the body section of an html ( .php) document. The code block before my heading executes, but the block after the heading does not. I've tried phpinfo(); in the first block and it runs fine. Try it in the second block and it doesn't. I have no idea why this could be. If anyone has suggestions I'm all ears.
I'm currently running xampp on a windows machine.
<body>
<?php
if (isset($_SESSION['ProfileID'])){
echo "<div style='text-align: right'>"
. "<a href='CZSN_Login.php'>Log "
. "Out</a></div>\n";
}
//here phpinfo() executes
?>
<h1>Chinese Zodiac Social Network</h1>
<?php
require_once("Includes/inc_ChineseZodiacDB.php");
//here phpinfo() will not execute
if (isset($_SESSION['ProfileID'])){
echo "<h2>Member Pages</h2>\n";
echo"<p>Below is a list of the members of the Chinese Zodiac Social"
. "Network. Click on a member's name to view that member's detailed information"
. "You may also choose to <a href='CZSN_MyProfile.php'>update your profile</a>.</p>\n";
$SQLQuery="select first_name, last_name, user_name "
. "from zodiac_profiles order by "
. "last_name, first name, user_name;";
$result=$DBConnect->query($SQLQuery);
if ($result===false){
echo $ErrorMsgs[];
}
else{
//This should never happen, but we can check anyway.
if ($result->num_rows==0){
echo "<p>There are no members to show.</p>\n";
}
Here is the code in my inc_ChineseZodiacDB.php file:
<?php
$ErrorMsgs = array();
$DBConnect = #new msqli("localhost", "root", "password", "chinese_zodiac");
if ($DBConnect->connect_error)
$ErrorMsgs[] = "The database server is not available."
. "Connect Error is "
. $mysqli->connect_errno
. " " . $mysqli->connect_error . ".";
?>
Try searching in require_once ("Includes / inc_ChineseZodiacDB.php"); there can be triggered die() or exit();
I guess you get "Error headers already sent", because of the title <h1>Chinese Zodiac Social Network</h1>. For phpinfo() to work there shouldn't be any output yet.
You are closing the php tag in the inc_ChineseZodiacDB file (i.e. you have ?> at the end of that file).
When your first file reads in the inc_ChineseZodiacDB file and sees the ?>, it interprets that as the end of your php section of code, and all the other code after require_once("Includes/inc_ChineseZodiacDB.php"); is ignored and read as regular html.
Remove that line, and your problem should be fixed:
<?php
$ErrorMsgs = array();
$DBConnect = #new msqli("localhost", "root", "password", "chinese_zodiac");
if ($DBConnect->connect_error)
$ErrorMsgs[] = "The database server is not available."
. "Connect Error is "
. $mysqli->connect_errno
. " " . $mysqli->connect_error . ".";
I figured out the ultimate problem. in the include file it reads:
<?php
$ErrorMsgs = array();
$DBConnect = #new msqli("localhost", "root", "password", "chinese_zodiac");
if ($DBConnect->connect_error)
$ErrorMsgs[] = "The database server is not available."
. "Connect Error is "
. $mysqli->connect_errno
. " " . $mysqli->connect_error . ".";
?>
where it should read:
<?php
$ErrorMsgs = array();
$DBConnect = #new mysqli("localhost", "root", "password", "chinese_zodiac");
if ($DBConnect->connect_error)
$ErrorMsgs[] = "The database server is not available."
. "Connect Error is "
. $mysqli->connect_errno
. " " . $mysqli->connect_error . ".";
?>
Note, I was calling msqli rather than mysqli. One simple typo...I'm still not understanding why the error messages were not being thrown for calling an undefined function. I added error_reporting(E_ALL); and ini_set('display_errors', 1); to the top of the file as well, and still no errors thrown. Either way, it works now. Thank you to everyone for the assistance.

Wordpress PHP Server Script to send email

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.

Connecting to ftp server

I am trying to connect to multilple ftp servers using ftp_connect and ftp_login. My code is very simple as given
foreach ($ftp_accounts as $ftp_account) {
$connect_id = $ftp_account['server'];
$user = $ftp_account['user'];
$pass = $ftp_account['password'];
echo 'Ftp Server: '.$connect_id.'<br> Username: '.$user. '<br> Password: '.$pass.'<br>';
if (ftp_login($connect_id, $user, $pass)){
echo "| Connected to server " . $connect_id . " as " . $user . '<br>';
} else {
echo "Couldn't connect to $connect_id as $user \n";
}
}
I have a table in my database with multiple servers and their credentials. but the code i wrote give me the following error (warning) message
ftp_login() expects parameter 1 to be resource, string given
and it goes to the else statement for every server.
Any idea what I am doing wrong ?
Thanks in advance
You need to use ftp_connect() to connect to the server before trying to login. Additionally, you need to check the result of ftp_connect() to ensure it was able to connect before you try to login. If it wasn't then it returns FALSE and you should not proceed to try and login.
foreach ($ftp_accounts as $ftp_account) {
$connect_id = ftp_connect($ftp_account['server']);
if($connect_id !== false)
{
$user = $ftp_account['user'];
$pass = $ftp_account['password'];
echo 'Ftp Server: '.$connect_id.'<br> Username: '.$user. '<br> Password: '.$pass.'<br>';
if (ftp_login($connect_id, $user, $pass)){
echo "| Connected to server " . $connect_id . " as " . $user . '<br>';
} else {
echo "Couldn't connect to $connect_id as $user \n";
}
} else {
echo 'failed to connect to server';
}
}
Replace
<?php
$connect_id = $ftp_account['server'];
?>
with
<?php
$connect_id = ftp_connect($ftp_account['server']);
?>
Hope this helps.
Let me answer my question. First of all the replies I get was all corect options to be checked for such problems I highly appriciate the replies but one thing you also need to check is that wether your server firelwall allows to connect to any outgoing ftp server or not ?
If it does not allow you to connect then even with everything written correct in your code you will not be able to connect to the server. So before testing your code check your server firewall settings.
It worked for me when i allowed all outgoing connections.
Once again Thanks for the replies
You forgot to make connection to server using ftp_connect function, assuming that in variable $ftp_account['server'] you have valid FTP server host address.
foreach ($ftp_accounts as $ftp_account) {
$connect_id = ftp_connect($ftp_account['server']);
if ($connect_id === false) {
echo "Failed to connect to FTP server";
continue;
}
$user = $ftp_account['user'];
$pass = $ftp_account['password'];
echo 'Ftp Server: ' . $ftp_account['server'] . '<br> Username: ' . $user . '<br> Password: ' . $pass . '<br>';
if (ftp_login($connect_id, $user, $pass)){
echo "| Connected to server " . $ftp_account['server'] . " as " . $user . '<br>';
} else {
echo "Couldn't connect to " . $ftp_account['server'] . " as " . $user . "\n";
}
}

Getting the name of an Exchange mailbox to connect to it programmatically

I am writing a PHP script to connect to an Exchange server and read messages from a mailbox. I have it connecting to my inbox just fine. What I'm trying to do now is get PHP to connect to a different mailbox that I have access to (Let's call it "Test Mailbox").
I tried this code:
imap_open( '{mail.domain.com:143}Test Mailbox', 'myusername', 'mypassword' );
But it said the mailbox doesn't exist. How can I get a list of mailboxes or get the path to the mailbox?
You should really think about doing this with Exchange Web Services (EWS). This will get you the data you want via SOAP versus IMAP which isn't going to be able to produce alot of things.
You can get a list of mailboxes with imap_getmailboxes(). As for selecting a mailbox, try without the leading /.
From php.net:
$mbox = imap_open("{mail.domain.com:143}", "username", "password", OP_HALFOPEN)
or die("can't connect: " . imap_last_error());
$list = imap_getmailboxes($mbox, "{mail.domain.com:143}", "*");
if (is_array($list)) {
foreach ($list as $key => $val) {
echo "($key) ";
echo imap_utf7_decode($val->name) . ",";
echo "'" . $val->delimiter . "',";
echo $val->attributes . "<br />\n";
}
} else {
echo "imap_getmailboxes failed: " . imap_last_error() . "\n";
}
imap_close($mbox);

Categories