Emails sent via PHPMailer library end up in spam folder for Gmail and Hotmail users

I’m working with a PHP mail library to send emails that include file attachments. The problem I’m facing is that when I send emails to Gmail or Hotmail addresses, they always end up in the spam or junk folder instead of the inbox.

I’ve been trying to figure out what’s causing this issue. Is there something wrong with my email configuration? Are there specific settings I need to adjust to improve deliverability? I’m also wondering if using a different sending domain would help with this problem.

Here’s the code I’m currently using:

<?php

ini_set('display_errors', 1);
error_reporting(E_ALL);

require 'vendor/autoload.php';

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

$mailer = new PHPMailer();
$mailer->From = '[email protected]';
$mailer->FromName = 'John Doe';
$mailer->Subject = 'Document Attachment';
$mailer->Body = 'Hello there';
$mailer->AddAddress('[email protected]');
$mailer->AddAttachment('./document.pdf', 'document.pdf', 'base64', 'application/pdf');
$mailer->Send();

?>

What steps should I take to ensure my emails reach the inbox instead of spam?

hey, just adding my 2 cents - u should check ur server’s IP for blacklisting, it’s often the cause. also, ur code doesn’t include SMTP auth, which is a big red flag for mail providers. try using a real email service with proper SMTP settings.

Sending emails directly from your server without proper authentication can lead to deliverability issues. Providers like Gmail and Hotmail have tight restrictions. I faced a similar situation with my client communications last year. To resolve it, I transitioned to using SMTP via services like SendGrid or Amazon SES, both of which carry better reputations. Additionally, ensure you have SPF and DKIM records set up in your DNS settings; this process is quick and greatly improves your chances. Furthermore, reconsider your email content as it might be triggering spam filters; avoid generic phrases in subject lines and strive for a more personal tone. Instead of direct attachments, consider sharing files via cloud storage links.