Access Office356 shared mailbox with PHP
Asked Answered
G

2

6

I'm working on a PHP script to read and parse error logs sent to a shared mailbox.

I'm using imap_open() to connect.

If I connect with the following credentials to my own mailbox, it works fine

server: {outlook.office365.com:993/imap/ssl}
username: [email protected]
password: mypas$word

However, if I want to connect to the sared mailbox with the follwowing credentials, I get an error

server: {outlook.office365.com:993/imap/ssl}
username: [email protected]\[email protected]
password: mypas$word

The error is the following: User is authenticated but not connected. (errflg=2)

According to my research that would mean I'm using the wrong password, but since it works when connecting to my own inbox, it can't be wrong

I am a 100% certain my account can access the shared mailbox, I can access it via my account through the Office 365 webinterface

One more detail: my password includes a $ sign, but that shouldn't matter. I use single quotes on the password and escaping the $ sign doesn't help, it results in a failed login even for my personal mailbox.

Golly answered 12/2, 2015 at 15:12 Comment(5)
So what have you done so far?Djambi
This is not really a question of coding, but interpreting Microsoft's error codes.Ditty
Yes, the password WOULD matter, if you're doing imap_open(...., "mypas$word"). that'd be the literal string mypas and whatever (maybe null) value exists in the variable $word.Sadness
When I connect to my own mailbox, I can list all the mails and delete them if I want to. When I want to the shared folder, I don't get further than a failed connection attemptGolly
I'm using single quotes for the password. Escaping the $ doesn't help. The fact that the password works for my own mailbox convinces me that the password is not the problem.Golly
P
6

Try using the account alias

%USERUPN%/%SHAREDALIAS% for example:-

[email protected]/sharedbox where "sharedbox" is the alias of the shared mailbox instead of the full email address

I've seen different reports of some people using forward slash ( / ) and some using a backslash ( \ )


Edit

Are you using the mailbox alias? Do not use [email protected]

Exchange Configuration:

  USER EMAIL: [email protected]
  PASSWORD: password



 SHARED MAILBOX: [email protected]      (could also be @domain.com, of course)
  SHARED MAILBOX ALIAS: shared-mailbox

Note: Please make sure to create the UNIQUE alias always with the mailbox, because it creates the user [alias]@maindomain.com. In this case [email protected]

Settings for IMAP Configuration:

  EMAIL ADDRESS: [email protected] (shared mailbox)
  IMAP SERVER: outlook.office365.com
  SMTP SERVER: smtp.office365.com
  USERNAME: [email protected]\shared-mailbox  (user\shared mailbox alias)
  PASSWORD: password (user's password)
Prognosticate answered 12/2, 2015 at 15:36 Comment(5)
Thanks, but that resulted in a imap_open(): Couldn't open stream {outlook.office365.com:993/imap/ssl}. Changing the \ to / doesn't seem to make a difference.Golly
I don't have access to the configuration so I've contacted the people who do. I'm not sure if there is an alias configured.Golly
the alias is currently the same as the name of the inbox, and that doesn't work. I've requested to change the alias to something differentGolly
We finally got this to work by adding array('DISABLE_AUTHENTICATOR' => 'PLAIN') to the imap_open string. We did also have to create a new shared mailbox, something was wrong with the config of the first one and we couldn't get it to work there.Golly
What was interesting is that this worked for my on my Windows based development system, but didn't work on my Linux production server. Ben Sommerville's solution above DID work on my Linux server, though.Tarantella
W
8

Another solution that worked for us is to add the username data to the mailbox parameter using the appropriate flags.

For example, with the Office 365 configuration of

USER EMAIL: [email protected]
PASSWORD: password
SHARED MAILBOX: [email protected]

then the imap_open call would be

imap_open("{outlook.office365.com:993/imap/ssl/[email protected]/[email protected]}", "[email protected]", "password");

Note the inclusion of the main user email using the /authuser flag and the shared mailbox using the /user flag.

That worked for us when the mailbox alias approach in the previous answer was not successful.

Wiebmer answered 3/6, 2015 at 2:51 Comment(4)
Thanks but that doesn't work for me. I think this only works for certain server configurations. I get: Can't do /authuser with this serverGolly
There is definitely something specific to the server. FYI this was to connect to an Office 365 Business (Enterprise) account. I tried every other approach I could find on the internet or think of and this was the only one that worked for this particular case. There is something about the combination of php imap and exchange/O365 that doesn't play well together :(Wiebmer
Worked for me, but I used the INBOX suffix for the server address {outlook.office365.com:993/imap/ssl/[email protected]/[email protected]}INBOXLadon
Worked for me too, but after also added charset 'US-ASCII', to use imap_search with office365Abstractionist
P
6

Try using the account alias

%USERUPN%/%SHAREDALIAS% for example:-

[email protected]/sharedbox where "sharedbox" is the alias of the shared mailbox instead of the full email address

I've seen different reports of some people using forward slash ( / ) and some using a backslash ( \ )


Edit

Are you using the mailbox alias? Do not use [email protected]

Exchange Configuration:

  USER EMAIL: [email protected]
  PASSWORD: password



 SHARED MAILBOX: [email protected]      (could also be @domain.com, of course)
  SHARED MAILBOX ALIAS: shared-mailbox

Note: Please make sure to create the UNIQUE alias always with the mailbox, because it creates the user [alias]@maindomain.com. In this case [email protected]

Settings for IMAP Configuration:

  EMAIL ADDRESS: [email protected] (shared mailbox)
  IMAP SERVER: outlook.office365.com
  SMTP SERVER: smtp.office365.com
  USERNAME: [email protected]\shared-mailbox  (user\shared mailbox alias)
  PASSWORD: password (user's password)
Prognosticate answered 12/2, 2015 at 15:36 Comment(5)
Thanks, but that resulted in a imap_open(): Couldn't open stream {outlook.office365.com:993/imap/ssl}. Changing the \ to / doesn't seem to make a difference.Golly
I don't have access to the configuration so I've contacted the people who do. I'm not sure if there is an alias configured.Golly
the alias is currently the same as the name of the inbox, and that doesn't work. I've requested to change the alias to something differentGolly
We finally got this to work by adding array('DISABLE_AUTHENTICATOR' => 'PLAIN') to the imap_open string. We did also have to create a new shared mailbox, something was wrong with the config of the first one and we couldn't get it to work there.Golly
What was interesting is that this worked for my on my Windows based development system, but didn't work on my Linux production server. Ben Sommerville's solution above DID work on my Linux server, though.Tarantella

© 2022 - 2024 — McMap. All rights reserved.