It could be email, you might be on to something and it is probably worth checking out. During a low activity time, you might want to enable email debugging and perform an action that generates an email. See if there is anything interesting in the SMTP debug output.
I've had experience with folks using office365 in the past, but am not aware of any inherent performance issues with Moodle using it to send email.
If the problem is in fact delay on the SMTP connection to Office 365 mail servers, I wondered if maybe this mail service rate limits connections, or if some specific setting change might be needed on the 365 account. So I did some web searching:
- https://social.technet.microsoft.com/Forums/lync/en-US/74410bfb-c2d5-460a-9bb7-2da6e1aa97a0/imap-smtp-slow-performance-on-office-365-10sec-idle-time-before-receivingsending-data?forum=officeitpro
- https://www.google.com/search?q=office+365+smtp+slow&oq=office+365+smtp+slow
- it seems you aren't the only one
Interesting concept... maybe your Moodle server isn't able to support TLS 1.1, see link:
- https://community.spiceworks.com/topic/1052142-delay-issue-receiving-emails-from-exchange-online-365-to-exchange-2003
- The idea on that link is that if your Moodle server can make TLS 1.1 smtp connections, that email sends more quickly. If your Moodle system cannot negotiate TLS 1.1, then there is a bunch more "negotiation" that slows it down.
- I don't know if this is a worthwhile rabbit hole or not, but knowing the OpenSSL version your PHP is built against on your Moodle server might be telling as to whether or not this theory might be true
Regards,
Jamie