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?
This is such a frequent problem that I wrote an article on how to solve it.
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.
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!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.