Strange Characters at bottom of PHP mail() HTML Email

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rwheindl
Forum Newbie
Posts: 1
Joined: Wed Jan 28, 2009 11:00 pm

Strange Characters at bottom of PHP mail() HTML Email

Post by rwheindl »

I have a php contact form which I just built. It is successfully sending emails, but it adds extra characters to the HTML portion of the email message. I've included a screenshot and the PHP code that generates the email message. I've scoured the internet for several hours now and I'm completely stuck. I've tried changing the character encoding to UTF-8 too and same thing happens. Can someone who has expertise with php mail() function and email headers provide some insight into where the garbage at the bottom is coming from? The plain text version displays fine. Several email readers show this same behavior with the HTML message so I'm inclined to think there's a problem with my code. Thanks.

Image

Here's the code.

Code: Select all

// Prep and send the email.
        
        // Message body assembly.
        $body = "Nana,\n\n";
        $body .= "<p>An email has been sent from ".$form['title']." ".$form['firstname']." ".$form['lastname'].". Here is the message:</p>\n";
        $body .= "<pre>".$form['comment']."</pre>\n\n";
        $body .= "<p>This additional information was provided:<br />\n\n";
        $body .= $form['title']." ".$form['firstname']." ".$form['lastname']."<br />\n";
        $body .= $form['addr1']."<br />\n";
        if(isset($form['addr2']) && ($form['addr2'] != ''))
            $body .= $form['addr2']."<br />\n";
        $body .= $form['city'].", ".$form['state']." ".$form['zip']."<br /><br />\n\nPhone: ".$form['phone']."<br />\n";
        if(isset($form['fax']) && ($form['fax'] != ''))
            $body .= "Fax: ".$form['fax']."<br /><br />\n\n";
        $body .= 'Email: <a href="mailto:'.$form['email'].'">'.$form['email']."</a></p>\n\n";
        $body .= '<p>Submitting IP: <a href="http://ws.arin.net/whois/?queryinput='.$form['ip'].'" target="_blank">'.$form['ip']."</a></p>\n";
        
        // Make sure we have a subject which shows the Date/Time submitted.
        $timestamp = date("M. jS, Y @ g:i a");
        $subject = "$site contact form submitted on $timestamp";
        
        ### Generate message headers.
        $mail_header =  "From: ".$form['firstname']." ".$form['lastname']." <".$form['email'].">\r\n".
                        "Reply-To: ".$form['firstname']." ".$form['lastname']." <".$form['email'].">\r\n".
                        "MIME-Version: 1.0\r\n";
        $boundary = uniqid("HTMLEMAIL");
        // Attach Plain Text Version
        $mail_header .= "Content-type: multipart/alternative; boundary=$boundary\r\n\r\n".
                        "This is a multi-part message in MIME format.\r\n\r\n".
                        "--$boundary\r\n".
                        "Content-type: text/plain; charset=iso-8859-1\r\n".
                        "Content-transfer-encoding: base64\r\n\r\n";
        $mail_header .= chunk_split(base64_encode(strip_tags($body)));
 
        // Attach HTML version
        $mail_header .= "--$boundary\r\n";
        $mail_header .= "Content-type: text/html; charset=iso-8859-1\r\n".
                        "Content-transfer-encoding: base64\r\n\r\n";
        $mail_header .= chunk_split(base64_encode($body));
        
        // Sendmail -f option
        $param = "-f ".$form['email'];
        
        // AWAY WE GO!
        if(!mail($to,$subject,$body,$mail_header,$param)){
            echo "<h1>Error sending message.</h1>";
            exit;
        }
Post Reply