简体   繁体   中英

PHP Multi-Part Text/HTML showing blank

hoping I can get some help because this is driving me nuts.

I have tried various scripts to do this but the one I landed on was this. What is wrong with this picture? I get an email but its completely blank. However the source of the email has content.

EMAIL CODE

//define receiver and subject

    $to = 'email@gmail.com';
    $subject = 'Test email'; 

//create boundary

    $random_hash = md5(date('r', time())); 

//define the headers

    $headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com";
    $headers .= "\r\nContent-Type: multipart/alternative; boundary=\"$random_hash\""; 

//define the body of the message.

    $message = "--$random_hash\n" .  
    "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .  
    "Content-Transfer-Encoding: 7bit\n\r" .  

    "Hello\n\rWorld!!!\n\r" .  

    "--$random_hash\n" .  
    "Content-Type: text/html; charset=\"iso-8859-1\"\n" .  
    "Content-Transfer-Encoding: 7bit\n\r" .  

    "<h2>Hello World!</h2>\n\r" .  

    "--$random_hash--";

//send the email

    $mail_sent = mail( $to, $subject, $message, $headers );

EMAIL SOURCE (RECEIVED)

    Received: (qmail 26359 invoked by uid 48); 21 Mar 2013 10:09:01 -0700
Date: 21 Mar 2013 10:09:01 -0700
Message-ID: <20130321170901.26356.qmail@mydomain.com>
To: me@mydomain.com
Subject: Test email
X-PHP-Originating-Script: 10001:plug-send-direct.php
From: webmaster@example.com
Reply-To: webmaster@example.com
Content-Type: multipart/alternative; boundary="f41fbfe6fda45eb7f5a2724ba135d292"

--f41fbfe6fda45eb7f5a2724ba135d292
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit


Hello

World!!!

--f41fbfe6fda45eb7f5a2724ba135d292
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

<h2>Hello World!</h2>

--f41fbfe6fda45eb7f5a2724ba135d292--

What gives?

Try making sure your HTML is properly formed with "html", "head" and "body" tags. Also, make sure they are closed properly.

<html>
<head>
</head>
  <body>
  Stuff
  </body>
</html>

It would be recomended to look into using a framework like PEAR::Mail_Mime to handle complex emails. Utilizing Mail_mimePart::addsubpart() it should be easier and more readable to get this done correctly. Here's a basic example for reference once it's installed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM