I'm configuring an instance of osTicket helpdesk software, which is open source and written in PHP. My server is running Apache2.4 on Ubuntu 16.04, with PHP7.1.
Now, I'm trying to connect osTicket with Office 365, for fetching incoming emails to helpdesk, using POP+SSL to connect with outlook.office365.com on port 995 (as specified here https://support.office.com/en-us/article/office-365-urls-and-ip-address-ranges-8548a211-3fe7-47cb-abb1-355ea5aa88a2?omkt=en-US&ui=en-US&rs=en-US&ad=US).
My problem is that connections take really long time, up to several minutes. But eventually, they succeed (at least some).
I've debugged this with telnet outlook.office365.com 995, finding that connections are attempted with IPv6 addresses. When one attempt times out, another IP is automatically received for the next attempt - usually another IPv6 address.
However, when telnet (at unpredictable intervals) receives an IPv4 address for the connection, I can immediately proceed with HELO and POP3 commands.
It therefore looks like IPv6 is the reason for slow connections inside osTicket, but I am uncertain about where the trouble can be resolved.
Can IPv4 be set as preferred via PHP or curl (if this is used?), or Apache, or could some OS level command fix the issue? I do not want to disable IPv6 for the entire server, however.