This can be duplicated post but please read before you flag it.
I using one mail function what I made and work a while until I not start receiving error messages from Yahoo.com and SkyNet.be.
Now I know that this error is realated to Message-ID or Date header inside email but my fealing is that Message-ID is main problem and need to solve that part.
Here is my code how I generate Message-ID:
// Generate secure unique message ID
$msgID = sprintf(
"<%s.%s@%s>",
base_convert(microtime(), 10, 36),
base_convert(bin2hex(
function_exists('random_bytes') ? random_bytes(8) : (
function_exists('openssl_random_pseudo_bytes') ? openssl_random_pseudo_bytes(8) : decbin(rand(100,819))
)
), 16, 36),
$_SERVER['SERVER_NAME']
);
Like you see above, I first checking is there PHP7.x solution for generating random bytes using random_bytes() function. If is version PHP5.x i use standard openssl_random_pseudo_bytes() function and if that function is not enabled or PHP version is 4.x or lower I use simple decbin() with rand().
Here is also my header setup:
// Preferences for Subject field
if(function_exists('iconv_mime_encode')){
$subject_preferences = array(
"input-charset" => 'utf-8',
"output-charset" => 'utf-8',
"line-length" => 76,
"line-break-chars" => PHP_EOL
);
$mime_subject = iconv_mime_encode("Subject", $subject, $subject_preferences);
}
else
$mime_subject = mb_encode_mimeheader("Subject: {$subject}", strtoupper('utf-8'), 'B', "\r\n", strlen('Subject: '));
// Headers
$headers = array(
"Content-type: text/html; charset=utf-8",
$mime_subject,
"From: {$from_name} <{$from_email}>",
"Reply-To: {$from_email}",
"X-Mailer: PHP/" . phpversion(),
"X-Sender: {$from_name} <{$from_email}>",
"X-Priority: 1 (Higuest)",
"X-MSMail-Priority: High",
"Importance: High",
"Content-Transfer-Encoding: 8bit ",
"Date: " . date("r (T)") . " ",
"Message-ID: {$msgID}",
"In-Reply-To: {$msgID}",
"References: {$msgID}",
"Return-Path: {$from_email}",
"MIME-Version: 1.0 "
);
$header = join(PHP_EOL, $headers);
Now main question is: -Why I getting error:
SMTP error from remote mail server after end of data: 554 Message not allowed - Headers are not RFC compliant[291]
???
To solve the problem you have to remove the "Subject" in the headers array.
Php mail function has a parameter that should be used to pass the "Subject"
function mail ($to, $subject, $message, $additional_headers = null, $additional_parameters = null)
So if you add the the Subject in additional headers, the subject of the message will be sent 2 times and this is the problem.
for more details take a look here : https://www.yetanotherblog.com/2016/02/29/554-message-not-allowed-headers-are-not-rfc-compliant291/
You need add probably to message header
Date: 2011-11-11
From: <email>
To: <email>
Subject: Subject here
Some server checking if header containt this strings :)
Here is RFC(newest) https://www.rfc-editor.org/rfc/rfc5322
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With