So I’ve finally got around to officially publishing my first WordPress plugin: Mail Integration for Office 365 by Cross Connected – WordPress plugin | WordPress.org

It’s by no means the first plugin I’ve ever written, but to date my exploits have solely focused on developing plugins that meet my own requirements; filling niche gaps that are hardly mass market. However, I think I’ve finally spotted a gap that could have some legs – the ability to connect WordPress to Office 365 to send emails from an Exchange/365 account!

Up until recently it has been possible to connect to Office 365/Exchange server via SMTP. However, Microsoft have now disabled basic authentication over SMTP by default, requiring more modern authentication methods.

You can re-enable basic authentication, but if you use two factor authentication (which you should by the way!), this can cause issues. It is also far less secure than OAuth2 (the authentication protocol this plugin uses), and Microsoft are right to disable it in an age of GPU password cracking and brute force attacks!

Now don’t get me wrong, there are plugins out there that will connect you to Office 365/Exchange. However, the ones I’ve found only provide this through a paid for feature/addon. Given that Office 365 is itself a paid for service, to my mind that is a little annoying.

For this reason I’ve made this free plugin for all to use and hopefully benefit from. It’s a little convoluted to set up, but that’s the nature of OAuth2 I’m afraid. However, if you’re patient and follow the guide found here diligently, you should be able to get it up and running pretty quickly.


29 Comments

Tim Turner · 25th January 2021 at 7:19 pm

Hi Edward,
Not sure where else to try and reach out for support but I’ve followed the guide and have everything setup but when I got to submit my form on my site, I get a “REST API is not yet supported for this mailbox.” message at the top of the page. I know you mentioned about M$ not enabling the graph API to work with free M$ account but I have paid office 365 business account, not even sure this would be related to that or not.

    Edward · 25th January 2021 at 11:49 pm

    Hi Tim, so couple of things come to mind.

    Firstly, are you sure you’re registering the right account with the plugin? Make sure your browser hasn’t saved your personal hotmail/live details and isn’t logging you in with those rather than the account you wish to authorise the plugin with. The easiest way to check this is to open the WordPress admin via an in private/incognito browser window that will force you to enter your login details for the account you wish to authorise after clicking the authorise button.

    Second thing to check if that doesn’t resolve things, is to see if you have your Office 365/Exchange licence assigned to the account you’re attempting to register within the admin portal of 365 (may seem obvious, but you can create a user within 365 with no licences). If you’ve set up a user such as [email protected], or [email protected], these would also need a separate 365 mail licence assigned, unless you set these up as shared mailboxes.

    Third thing to check (which may not be relevant in your case, but still worth checking), is that your account has been migrated over to 365 as explained in this guide: https://docs.microsoft.com/en-us/exchange/troubleshoot/user-and-shared-mailboxes/rest-api-is-not-yet-supported-for-this-mailbox-error

David Sandil · 29th January 2021 at 2:20 pm

Wonderful plugin. I have configured the plugin as required. Thanks to your detailed steps. It worked for a day may be and then it stopped sending emails. When I go to the plugin page and authorise it again, it get authorised successfully but finally when I press save button, it shows error – ‘Invalid OAuth State’ at the top of the plugin settings page. Any idea?

    Edward · 30th January 2021 at 10:19 am

    Hi David, so if you’re not using version 1.3.0 then that might explain things. A few people were having this issue with version 1.2 but it was resolved in version 1.3.0 (according to the same users). If you are using version 1.3.0, then I’d try removing the plugin completely (making sure you haven’t chosen to remember the credentials in the plugin settings page). Deactivate and then delete the plugin, redownloading it again. It’s unlikely, but I guess possible, that there could be an issue with the transition between 1.2 and 1.3, causing the invalid state error as I changed the way I was recording the state variable in temporary storage between the two versions.

Tyler Reed · 4th February 2021 at 4:23 am

When I click save settings I get this:

Error: Options page mail-integration-365-settings_deactivation_options not found in the allowed options list.

What do I need to fix?

    Tyler Reed · 4th February 2021 at 4:29 am

    Fixed it, followed the instructions on the post above.

Fred · 5th February 2021 at 7:46 am

Greetings! First, thanks for this plugin, it is what I need. BUT I followed the very good instructions, and everything looks good. Until i save the settings, then I get ‘Invalid OAuth State´And in the Office 365 email account trying to send from my WordPress I get delivery failure 550 5.7.708 Service unavailable. Access denied, traffic not accepted from this IP. I suspect I need to do something in Office 365? Im a admin.

    Fred · 5th February 2021 at 7:49 am

    Forgot to mention, Im using latest version, tried deactivate, delete, reinstall, same issue.

        Fred · 6th February 2021 at 7:06 pm

        I already have tried that solution, no luck.

          Senne · 27th February 2021 at 10:55 am

          Hi Edward and Fred,
          I have no issues in seeting it up but I do get the “550 5.7.708 Service unavailable. Access denied, traffic not accepted from this IP” NDR… It’s a rather new O365 tenant but I have no idea what to try next… Fred, were you able to get yours working? Did you contact Microsoft directly?

          What IP are they not trusting? The one where the WordPress website is hosted and that is calling the Graph API?

          Thanks for pointing me in the right direction, your plugin is absolutely what I was looking for, almost there 🙂

          Edward · 28th February 2021 at 6:48 pm

          Hi, so it must be the IP of the WordPress website. If you are using a shared server which hosts multiple websites under the same IP, it’s possible that other websites used for spam or nefarious purposes have caused the IP address to be flagged by Microsoft. Many cheaper/free web hosting services are shared services. This may not apply in your case, but it seems to be a potential reason for the issue you’re facing, without knowing the details of your web hosting.

Lennart · 28th February 2021 at 12:34 pm

Hi Edward,

first of all, thank you so much for this wonderful plugin! It’s amazingly simple and lightweight. Great work! 👌

I do have one issue though: I use Contact Form 7 and after I submit a form (send a message), the loading animation keeps going on. There is no response (i.e. “successfully sent”) for the user who sends a message. This only happens when activating your plugin; the standard email sending function works. Are you aware of that or is it something with my individual configuration?

I’m very much looking forward to hearing back from you! 🙂

Lennart Wörmer · 28th February 2021 at 12:39 pm

Hi Edward, I have just discovered that the problem is caused by the Mail (2) function of Contact Form 7. If you deactivate it, it works perfectly. Having it activated causes the issue. The second email won’t be sent either.

    Edward · 28th February 2021 at 6:49 pm

    Hi Lennart, I did have issues getting Contact Form 7 to register that an email had been sent, but glad you’ve found a work around.

      Edward · 13th April 2021 at 5:22 pm

      Just to say I think the Contact form 7 issue has been resolved.

        Aaron · 11th May 2021 at 2:48 am

        I don’t think the issue is quite fixed.
        The plugin works for a single mail, but Mail (2) will still not send.
        This plugin does otherwise look great, so hoping it is not too hard to fix

          Aaron · 11th May 2021 at 2:51 am

          Nevermind, issue was from a separate plugin, nothing wrong with this one!

Andrew Read · 2nd March 2021 at 6:06 pm

Hello,

I am not using Mail (2) in the Contact Form 7 plugin, but my website won’t send emails through the contact form. I just get a constantly spinning dial beside the send button. Is there any way to get some support on this?

A “send test email” section in your plugin would be a great tool to let us discover if there is an issue with your plugin sending emails or if it is indeed an issue with Contact Form 7.

Thanks for your effort put into this plugin though!

    Edward · 5th March 2021 at 10:20 am

    Hi Andrew, I know I looked at this issue ages ago and couldn’t figure out a resolution. It seems, because I intercept the PHPMailer() event through using WordPress’ wp_mail() pluggable function, I have to stop the PHPMailer event to stop WordPress trying to connect over STMP, instead capturing the mail attributes and sending them via Microsoft Graph’s REST API. Whilst the wp_mail() event then returns true, it seems PHPMailer doesn’t, and because Contact Form 7 is waiting for PHPMailer to return true, it keeps the spinning animation, rather than showing a “mail sent successfully” message. I ran out of ideas/trial and error approaches to getting around this limitation, but I’m open to any ideas from others more knowledgeable than me! I think I may have to provide an alternative to use OAuth over SMTP, rather than the REST API (which I believe is what other paid for plugins use), as this will mean I don’t have to intercept and stop the PHPMailer events in order to redirect the mail request over a REST API.

      Christian Callsen · 10th March 2021 at 3:57 pm

      Hi Edward! I’m seeing the exact same problem. Is there a workaround until you can fix the plugin, please (please)? Otherwise, the plugin prevents the contact form from working.
      Cheers!
      Christian

        Edward · 13th April 2021 at 5:21 pm

        Hi both, I believe the issue with the contact forms has now been resolved.

Adrian · 11th March 2021 at 7:27 am

Hey Edward.
Thanks for the perfect plugin. It does exactly what I was looking for. The setup instructions worked like a charm.
However, I have a problem sending HTML emails. They do not arrive properly formatted to the recipient. It seems that they are sent as “text” although the contact form plugin (Form Maker v.1.13.53) is set to use HTML. If I deactivate your plugin, it works. Do you have any idea what the problem could be?
Thanks and greetings from Switzerland.
Adrian

Edward · 12th March 2021 at 7:48 pm

Hi Adrian, so I think I’ve figured out what’s going on here. I hadn’t realsied there are two ways of setting the content type of an email with wp_mail(), and one doesn’t require a content-type header. I think the Form Maker is changing the content type by a filter which doesn’t result in a content-type header being sent (at least at the function level). Given my plugin was looking for the content-type header, it wasn’t recognising that the content type of the email had changed from default text type of wp_mail(). I’ve adjusted the code to listen for the content type filter event, as well as looking at the headers (giving precedent to the header value if both exist). I’ve tested with the Form Maker plugin and this seems to have resolved the issue.

As I’ve only just released a new version, I’m probably going to leave it for a few week before pushing the new one (there may be a few more things that need updating). However, you can install the beta version from the following link (indeed it would be good to get some feedback on whether you have any issues with it anyway): https://drive.google.com/file/d/1zdfDsY3CbtTFbEO668vCs70j72y3m84C/view?usp=sharing

Here’s a guide for how to install a plugin manually (note only do so from sources you trust…): https://www.hostinger.com/tutorials/wordpress/how-to-install-wordpress-plugins

    Adrian · 13th March 2021 at 3:50 pm

    Hi Edward, thank you for the quick reply. I have installed and tested the updated version. The problem with the HTML email has been solved.
    Now I have another question: In Form Maker, a reply-to address can be defined for the email to the administrator. However, the email does not contain this information. Apparently it gets lost. If your plugin is deactivated, it works fine. Do you maybe also have a solution for this?
    Thank you and best regards, Adrian

      Edward · 11th April 2021 at 9:57 am

      Hi Adrian, the reply-to issue is something I’m aware of. If you’re technically minded, the issue is that when I’m overriding the wp_mail($to, $subject, $message, $headers, $attachments) event, the $headers variable doesn’t seem to be transferred. Hence it appears there is no reply-to header, even when one is set. It might be something obvious I’m missing, I just haven’t had the time to investigate as yet.

Edward · 13th April 2021 at 5:20 pm

Hi Adrian, I have now updated the plugin to version 1.6.0 which seems to be setting the reply-to address correctly. I think this update should have also resolved the issue with Contact Form 7 and other similar plugins.

Jose Fernando · 19th April 2021 at 5:34 pm

Hi Edward:
I have problems with the plug-in and contact form 7. The plug-in works if there is only one recipient in the “To” field of the “mail” tab of contact form 7. If there are more recipients, in DevTools I get this error: {code: “invalid_json”, message: “The response is not a valid JSON response.”}. And the mail is not sent.

    Edward · 26th April 2021 at 8:25 am

    Hi Jose, sorry you’re having issues. I’ll look into this. I have an idea of what’s going on but will need to investigate to check.

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *