Email sent from the PHP mail() function hosted on a Cpanel account frequently ends up in SPAM (all the time for @outlook.com, some of the time for @gmail.com)
I've determined it is NOT due to the content of the message but due to something in the headers. My best guess is that it's either because there's a header field indicating the email was sent by a PHP script, or it's the fact that no matter if you set the From: Reply-To: and Return-Path: to the same email address, the SMTP server is always showing the email was sent from {cpanel account username}@servername.serverdomain.com and this does not match the From/Reply-To address and it appears it overrides any Return-Path address I use to the {cpanel account username}@servername.serverdomain.com
As you know Web apps can send email on behalf of a user where the user doesn't have an email address hosted on the same domain that the application is running from. So the server may be serverdomain.com and the user's email might be something@someotherdomain.com
Examining the headers of the test script or the emails sent from, the SMTP server is always showing the email was sent from cpanel-account-username@server1.serverdomain.com Part of the header after coming through to Gmail shows this:
Return-Path: <cpanel-account-username@server1.serverdomain.com>
Received: from server1.serverdomain.com (server1.serverdomain.com. [XXX.XXX.XXX.XXX])
by mx.google.com with ESMTPS id w7si15164878pgi.491.2020.09.22.16.51.14
for <testto@gmail.com>
(version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128);
Tue, 22 Sep 2020 16:51:14 -0700 (PDT)
Received-SPF: pass (google.com: domain of cpanel-account-username@server1.serverdomain.com designates XXX.XXX.XXX.XXX as permitted sender) client-ip=XXX.XXX.XXX.XXX;
Authentication-Results: mx.google.com;
spf=pass (google.com: domain of cpanel-account-username@server1.serverdomain.com designates XXX.XXX.XXX.XXX as permitted sender) smtp.mailfrom=cpanel-account-username@server1.serverdomain.com
Received: from cpanel-account-username by server1.serverdomain.com with local (Exim 4.93) (envelope-from <cpanel-account-username@server1.serverdomain.com>) id 1kKs41-00014X-4u for test@gmail.com; Tue, 22 Sep 2020 16:51:13 -0700
To: testto@gmail.com
Subject: Something
X-PHP-Script: suitecpanelhostdomain.com/cms/index.php for XXX.XXX.XXX.XXX
X-PHP-Originating-Script: 1006:test_script.php
From: From Name <something@someotherdomain.com>
Reply-To: something@someotherdomain.com
MIME-Version: 1.0
Content-Type: text/html; charset=ISO-8859-1
X-Priority: 3
X-Mailer: PHP7.3.22
If I was able to change the header so it thinks the email was sent from something@someotherdomain.com instead of cpanel-account-username@server1.serverdomain.com I don't think that would work as the server1.serverdomain.com's IP address would not match the MX or SPF records for someotherdomain.com since the cpanel account's domain does not handle email for that application user's email address's domain.
Some coders out there running PHP on CPanel (or using PHP mail()) in general must have run into this before and wondering if anyone found any solutions? Should I switch to PhpMailer for better header control and if so what should I change?