PHP Mail is going to Gmail but not Office 365 accounts

I'm sending a HTML email and it is arriving at gmail accounts but not at Office 365 Accounts. My code is below please help me troubleshoot.

if (isset($_POST['submit-forgot'])) {
    $flag = true;
    if (isset($_POST['email-forgot'])) {
        $email = $_POST['email-forgot'];
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $alert = '<div class="alert alert-danger">You must enter a valid email address</div>';
            $flag = false;
        } else {
            $sql = "SELECT * FROM users WHERE email = '$email'";
            $result = $db->query($sql);
            $array = $result->fetch_assoc();
            if ($result->num_rows == 1) {
                $hex = bin2hex(random_bytes(16));
                $sql = "UPDATE users SET hex = '$hex' WHERE email = '$email'";
                $result = $db->query($sql);
                $to = $array['email'];
                $subject = 'Password Reset';
                include 'includes/mail.php';
                $headers  = 'MIME-Version: 1.0' . "\r\n";
                $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
                $headers .= 'From:' . "\r\n";
                $headers .= "To: $to\r\n";
                mail($to, $subject, $message, $headers);
                $alert = '<div class="alert alert-success">If the email address you entered is on file you will receive an email.</div>';


Open in new window

$message = '
    <title>Reset Your Password</title>
<h1>Thanks for being a customer.</h1>
<h2>This link can only be used one time for security purposes.</h2>
<table cellspacing="0" style="border: 2px dashed #FB4314; width: 300px; height: 200px;">
    <tr style="background-color: #e0e0e0;">
        <th>Reset Link:</th><td><a href="' . $hex . '">Reset Password</a></td>

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ares KurkluSoftware EngineerCommented:
I suggest you try to email to a few other domains as well, it may have to do just with office 365, they may be ending up as spam.
This is such a frequent problem that I wrote an article on how to solve it.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dave BaldwinFixer of ProblemsCommented:
Good article but the current site for PHPMailer is .
burnedfacelessAuthor Commented:
Thanks guys I'm going to install PHP Mailer and give that a try.
Tips #5 and #6 are the most critical ones from that article. Make sure you implement BOTH. They both have HUGE impacts in avoiding spam filters.

Thanks for the note about the new url, Dave. The article is about 8 years old so it definitely needs an update!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Office

From novice to tech pro — start learning today.