title: PHP: 554 Message not allowed - Headers are not RFC compliant[291]
body: <p>This can be duplicated post but please read before you flag it.</p>
  
  <p>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.</p>
  
  <p>Now I know that this error is realated to <code>Message-ID</code> or <code>Date</code> header inside email but my fealing is that <code>Message-ID</code> is main problem and need to solve that part.</p>
  
  <p>Here is my code how I generate <code>Message-ID</code>:</p>
  
  <pre><code>// Generate secure unique message ID
  $msgID = sprintf(
      "&lt;%s.%s@%s&gt;",
      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']
  );
  </code></pre>
  
  <p>Like you see above, I first checking is there PHP7.x solution for generating random bytes using <code>random_bytes()</code> function. If is version PHP5.x i use standard <code>openssl_random_pseudo_bytes()</code> function and if that function is not enabled or PHP version is 4.x or lower I use simple <code>decbin()</code> with <code>rand()</code>. </p>
  
  <p>Here is also my header setup:</p>
  
  <pre><code>// Preferences for Subject field
  if(function_exists('iconv_mime_encode')){
      $subject_preferences = array(
          "input-charset"     =&gt; 'utf-8',
          "output-charset"    =&gt; 'utf-8',
          "line-length"       =&gt; 76,
          "line-break-chars"  =&gt; 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} &lt;{$from_email}&gt;",
      "Reply-To: {$from_email}",
      "X-Mailer: PHP/" . phpversion(),
      "X-Sender: {$from_name} &lt;{$from_email}&gt;",
      "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);
  </code></pre>
  
  <p>Now main question is: -Why I getting error:</p>
  
  <p><code>SMTP error from remote mail server after end of data: 554 Message not allowed - Headers are not RFC compliant[291]</code></p>
  
  <p>???</p>