Can't access to Office365 accout with IMAP & PHP - php

I am trying to access to office365 inbox, but I am getting this message
PHP Warning: imap_open(): Couldn't open stream {outlook.office365.com:993/imap/ssl}
Here is my code
$hostname = '{outlook.office365.com:993/imap/ssl/novalidate-cert}';
$username = '*****';
$password = '*****';
$fp = fopen('cox_cash [30 40].txt', 'w+');
if (!$fp) die("Impossible d'ouvrir le fichier");
/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to : ' . imap_last_error());

For me, the issue was not having the imap library for php. Try using function_exists('imap_open') to check if you actually have the library. function_exists() returns true if your php has the function, false otherwise. Simple if statement with echos should do the trick.

Related

Warning: ftp_put(): Starting data transfer [duplicate]

I upload XML file through FTP:
$ftp = "ftp";
$username = "username";
$pwd = "password";
$filename = $_FILES[$xyz][$abc];
$tmp = $_FILES['file']['tmp_name'];
$destination = "/Content/EnquiryXML ";
$connect = ftp_connect($ftp)or die("Unable to connect to host");
ftp_login($connect,$username,$pwd)or die("Authorization Failed");
echo "Connected!<br/>";
if(!$filename)
{
echo"Please select a file";
}
else
{
ftp_put($connect,$destination.'/'.$filename,$tmp,FTP_ASCII)or die("Unable to upload");
echo"File successfully uploaded to FTP";
}
I want to send the XML file created using DOMDocument to a FTP server but I am not able.
The ftp_put returns false.
Most typical cause of problems with ftp_put (or any other transfer command like ftp_get, ftp_nlist, ftp_rawlist, ftp_mlsd) is that PHP defaults to the active mode. And in 99% cases, one has to switch to the passive mode, to make the transfer working. Use the ftp_pasv function.
$connect = ftp_connect($ftp) or die("Unable to connect to host");
ftp_login($connect, $username, $pwd) or die("Authorization failed");
// turn passive mode on
ftp_pasv($connect, true) or die("Unable switch to passive mode");
The ftp_pasv must be called after ftp_login. Calling it before has no effect.
See also:
PHP ftp_put fails with "Warning: ftp_put (): PORT command successful"
my article on the active and passive FTP connection modes.
Further, if your FTP server is reporting an incorrect IP address in the response to the PASV command (what is quite common, if the server is behind firewall/NAT), you might need to workaround it by using:
ftp_set_option($connect, FTP_USEPASVADDRESS, false);
See PHP FTP + Passive FTP Server Behind NAT.
Though the right solution in this case, is to get the server fixed.
This worked:
// connect and login to FTP server
$ftp_server = "host";
$ftp_username = "username";
$ftp_userpass = "password";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
$file ="$abc";
// upload file
if (ftp_put($ftp_conn, "/$abc" , $file, FTP_ASCII)){
echo "Successfully uploaded $file.";
} else {
echo "Error uploading $file";
}
// close connection
ftp_close($ftp_conn);

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

Is any other way to read message from webmail on cpanel using PHP with out using IMAP function

I'm integrating PHP webmail read functionality in php. I already tried using IMAP functions. But it could not connect.So I just ask you is there any possibility to get messages from webamil with out using IMAP function.
Below is my IMAP Code. Can you check the code also give me suggestions for why doesn't work. I'm looking forward for your valuable reply.
$emailAddress = 'info#example.com'; // Full email address
$emailPassword = 'xxxxxxxx'; // Email password
$domainURL = 'example.com'; // Your websites domain
$useHTTPS = false;
/* BEGIN MESSAGE COUNT CODE */
$inbox = imap_open('{'.$domainURL.':143/notls}INBOX',$emailAddress,$emailPassword) or die('Cannot connect to domain:' . imap_last_error());
$oResult = imap_search($inbox, 'UNSEEN');
if(empty($oResult))
$nMsgCount = 0;
else
$nMsgCount = count($oResult);
imap_close($inbox);
echo('<p>You have '.$nMsgCount.' unread messages.</p>');
Thanks in advance

Sudden PHP Error "Warning: imap_open() [function.imap-open]: Couldn't open stream"

Okay, this code 'was' working perfectly and then I started playing around with it in order to let others connect to their e-mails and as you do ran into a few open stream errors along the way due to various typos and such.
Since doing this, all of a sudden I can't connect to my e-mail at all? A short while back I was using the exact same connection code and then browsing my inbox.
I always get the "Warning: imap_open() [function.imap-open]: Couldn't open stream" error.
This is strange as I'm using the exact same code as before, but since bumping into errors I can't connect whatsoever now. It also takes ages to respond.
Here is the code:
$mailbox = imap_open('{mail.artisancodesmith.com:143/notls}INBOX', 'admin#artisancodesmith.com', 'PASSWORD');
if ($mailbox) {
$response = "MAIL MENU:<br>
inbox: View your inbox.<br>
compose: Compose an e-mail.<br>
setup: Set your e-mail account's settings.";
$next = "iorcmail";
}
NOTE: The PHP page is connecting to the e-mail on the same server.
UPDATE:
If I replace "mail.artisancodesmith.com" with "localhost" it works again!
I would preferably like to use my actual IMAP host - I'll see if it works again some time in the future I guess.
Thanks to all who helped. :)
Please use the below code to connect successfully,
$hostname = "{imap.gmail.com:993/imap/ssl/novalidate-cert}";
$mailbox = imap_open($hostname, 'admin#artisancodesmith.com', 'PASSWORD');
if ($mailbox)
{
// do work....
}
I ran into this problem and here is how I solved it;
Login to your gmail account, Under Settings -> Forwarding and POP/IMAP -> Enable Imap.
Go to https://www.google.com/settings/security/lesssecureapps and select "Turn On"
Go to: https://accounts.google.com/b/0/DisplayUnlockCaptcha and enable access.
after this, above code works for me....
I have tried this an works perfectly fine for me
$inbox = imap_open("{imap.gmail.com:993/imap/ssl/novalidate-cert/norsh}Inbox", 'username', 'password')
or die('Cannot connect to Gmail: ' . imap_last_error());
$emails = imap_search($inbox,'All');
if($emails) {
/* begin output var */
$output = '';
/* put the newest emails on top */
rsort($emails);
/* for every email... */
foreach($emails as $email_number) {
/* get information specific to this email */
$overview = imap_fetch_overview($inbox,$email_number,0);
$message = imap_fetchbody($inbox,$email_number,2);
$header = imap_header($inbox, $email_number);
echo "<h1>data</h1>";
echo "<pre>";print_r($message);
echo "<h1>Message</h1>";
echo "<pre>";print_r($message);
echo "<h1>header</h1>";
echo "<pre>";print_r($message);
$overview[0]->seen;
$overview[0]->subject;
$overview[0]->from;
$overview[0]->date;
}
}
/* close the connection */
imap_close($inbox);
maybe are you behind a proxy? if so, I guess you need to auth against it...
resource imap_open(
string $mailbox ,
string $username ,
string $password [,
int $options = 0 [,
int $n_retries = 0 [,
array $params = NULL ]]]
)

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