Office365 smtp rejected

As a reference for those who are experiencing this issue with Office365 I’ll share the solution what worked for me.
Background: I found this issue occurs when you are hosting your CRM o domain different to the one you have setup on your Office365. (That’s my case at least).

To solve it you either add your CRM domain to your Office365.
If for some reason you are not allow to do it, the workaround is to modify the sugar library that creates the emails on SuiteCRM (Thanks to @R3xMan). To do so you need to modify file \include\SugarPHPmailer.php from


...
       $this->isHTML(false);  // default to plain-text email
        
	$this->Hostname = $sugar_config['host_name'];
        $this->WordWrap = 996;
        // cn: gmail fix
        $this->protocol = ($this->oe->mail_smtpssl == 1) ? 'ssl://' : $this->protocol;
        $this->SMTPAutoTLS = false;
...

To:


...
       $this->isHTML(false);  // default to plain-text email
        
	$this->Hostname = $sugar_config; // TAKE A LOOK INTO THIS LINE
        $this->WordWrap = 996;
        // cn: gmail fix
        $this->protocol = ($this->oe->mail_smtpssl == 1) ? 'ssl://' : $this->protocol;
        $this->SMTPAutoTLS = false;
...

You can check the original post here:
https://suitecrm.com/suitecrm/forum/suitecrm-7-0-discussion/21720-smtp-error-501-5-5-4-invalid-domain-name-7-11

Thanks,
AlxGr