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);
Related
I have a file login.php to try to connect to my database.
I put the file login.ph inside the server folder and started the server.
Then i call the file in the browser and it shows a blank page. It does not respond even if i change the values of the database to an incorrect value.
I don't know if the error is inside the code or if it is another problem.
Thanks.
login.php:
<?php
$username = $_GET['fname'];
$password = $_GET['fpass'];
$con=mysqli_connect("localhost","user","pass","db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$qz = "SELECT contact_id FROM contacts" ;
$qz = str_replace("\'","",$qz);
$result = mysqli_query($con,$qz);
while($row = mysqli_fetch_array($result))
{
echo $row['contact_id'];
}
mysqli_close($con);
?>
You must check $con variable that you have set with the result of the connection:
if (!$con) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
Do you have web server (say apache) installed and running on your server? You have to. Then put your file on the web server folder (say /var/www/html) and test in the browser.
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.
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...
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";
}
}
I've done quite a bit of inbox manipulation with Gmail via IMAP functions in PHP, but one thing I haven't found is a way to create messages. I'm not sure if IMAP or SMTP is required, but I would like to use PHP to create a new message (specifically a draft) that is stored in my inbox with everything ready to hit send at a later date. How do I go about this?
You might want to look at imap_mail_compose()
Edit
This doesn't create the message on the server. You need to use imap_append() also.
Further Edit
This seems to work ok:
<?php
$rootMailBox = "{imap.gmail.com:993/imap/ssl}";
$draftsMailBox = $rootMailBox . '[Google Mail]/Drafts';
$conn = imap_open ($rootMailBox, "sdfsfd#gmail.com", "password") or die("can't connect: " . imap_last_error());
$envelope["to"] = "test#test.com";
$envelope["subject"] = "Test Draft";
$part["type"] = TYPETEXT;
$part["subtype"] = "plain";
$part["description"] = "part description";
$part["contents.data"] = "Testing Content";
$body[1] = $part;
$msg = imap_mail_compose($envelope, $body);
if (imap_append($conn, $draftsMailBox, $msg) === false) {
die( "could not append message: " . imap_last_error() ) ;
}