RFC2822 non compliance, something about Date or From in PHP header?

780 views Asked by At

I'm trying to have a form submit to an email and it's ultimately ending up at a gmail address. However, I'm getting return errors saying:

Our_system_has_detected_that_this_message_is/550-5.7.1_not_RFC_2822_compliant._To_reduce_the_amount_of_spam_sent_to_Gmail,/550-5.7.1_this_message_has_been_blocked._Please_review/550_5.7.1_RFC_2822_specifications_for_more_information.w10si875474obo.147-_gsmtp/

Code below, am I doing something wrong or is there bad code/syntax I'm missing somewhere?:

<?php

    if($_POST) {

        if($_POST["token"]==true){

            $email = $_POST['email'];

            $from_name = 'Whoever';
            $from = '[email protected]';

            $to = '[email protected]';
            $subject = 'Connect Form';
            $semi_rand = md5(time());
            $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
            $headers = "Date: ". date("r") ."\r\n". "From: ".$from .  "\r\nFrom: ".$from_name." <".$from.">\r\n";
            $headers .= "MIME-Version: 1.0\n" .
                "Content-Type: multipart/mixed;\n" .
                " boundary=\"{$mime_boundary}\"";
            $message_top = "This is a multi-part message in MIME format.\n" .
                "--{$mime_boundary}\n" .
                "Content-Type:text/html; charset=\"iso-8859-1\"\n" .
                "Content-Transfer-Encoding: 7bit\n";

                if(sizeof($_POST['contact_via'])>0)
                    $contact_via=implode(", ", $_POST['contact_via']);
                else
                    $contact_via="";

                $email_message = "\n".'<table cellspacing="4">
                  <tr><td align="right">Contact Name : </td><td>'.$_POST['contact_name'].'</td></tr>
                </table>';

            $email_message = stripslashes($email_message);
            $email_message = str_replace("\n","\n",$email_message);
            $email_message = $message_top . $email_message;

            mail($to, $subject, $email_message, $headers);
            echo '<script>window.location = "http://www.website.com/Thank-You.html";</script>';
          exit;

        }else{
            echo '<script>window.location = "http://www.website.com/Thank-You.html";</script>';
            exit;
        }

    }
?>
2

There are 2 answers

2
ceejayoz On

Using mail() directly is a recipe for frustration. There are lots of things to get wrong and you have to hand-code stuff like attachments, HTML e-mail support, etc.

Using a sane wrapper for mailing like SwiftMailer allows you to worry a lot less about getting the esoteric details of mail addressed and focus on more useful things.

In this particular case, it appears you're setting duplicate From: headers (and having a hard time deciding between \r\n and \n).

$headers = "Date: ".date("r")."\r\n" . "From: ".$from . "\nFrom: ".$from_name." <".$from.">\n";

2
Latheesan On

The RFC2822 dictates that Date: and From: are mandatory header fields on the email.

You have this, but incorrectly.

Try this header line:

$headers = "Date: ". date("r") ."\r\n". "From: $from_name <$from>\r\n";