How to serve multi-page / related content in PHP?

Let's say I have application content / xhtml + xml and a gif image. How can I serve (not render) these two in one HTTP request (using PHP)? This should be multi-page / related content. After reading this RFC, I tried something; but it didn’t work. (No, I'm not trying to send emails with attachment)

Thanks in advance.

EDIT . Finally, I succeeded. I think it will be useful if I write how I did it. I added this as an answer. Take a look below.

+6
php mime
source share
3 answers

Here it [worked for me] (No, I did not use PHPMailer, maybe this is useful, but I could not get it to work):

// Two contents : application/xhtml+xml and image/gif // Here we go <?php $boundary = "ghorar#deem"; $im_len = filesize("path/to/abc.gif") header("Content-Type:multipart/related; boundary=\"$boundary\"; type=\"application/xhtml+xml\""); $xml_cnt = <<<EOD <media xmlns="http://www.sth.com/sth.xsd"> <objectURI>cid: 112509abc@syz.com </objectURI> <size>$im_len</size> <type>image/gif</type> <name>abcxyz</name> <description>blah blah</description> </media> EOD; $xml_len = strlen($xml_cnt); $im = imagecreatefromgif("path/to/abc.gif"); $to_send = "This is a multi-part message example in MIME format --$boundary Content-Type: application/xhtml+xml; Content-ID: < e4509xml@asd.com > Content-Length: $xml_len $xml_cnt --$boundary Content-Type: image/gif; Content-ID: < 112509abc@syz.com > Content-Length: $im_Len Content-Transfer-Encoding: binary "; echo $to_send; imagegif($im); echo "\r\n--$boundary--"; imagedestroy($im); ?> 

You can also load xml from a file. But, note that if your xml refers to an image (for example, I am here), you need to reference this with its Content-ID. Also pay attention to the '<' '>' in the Content-ID field of the image (after the border) and the designation 'cid:' in the place where it is indicated. It worked for me. Thanks to Carlos Lima for taking the time with me.

+3
source share

I am afraid that you cannot for the right purpose.

If you want to do this to serve web pages, as far as I can tell, browsers will not work with such MIME answers to render pages.

If you need an example of how these messages work, send an email with an attachment and on your mail client (and not in web mail) go to the option "View source" in the body of the message.

You will see your message, attachment, and possibly other parts in the same message using the MIME Multipart encoding.

OTOH, if you want it to send email, there are libraries such as PHPMailer that will do the whole encoding for you.
If you need it, check this example on your website.

Edit:

You can use PHPMailer to create a message, then you just use the result instead of actually sending the email.

Try something like this:

** This is unverified code, only for the starting point **

 <?php require_once('../class.phpmailer.php'); $mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch try { $mail->MsgHTML(file_get_contents('contents.html')); $mail->AddAttachment('images/phpmailer.gif'); // attachment $mail->AddAttachment('images/phpmailer_mini.gif'); // attachment $mime_message = $mail->CreateBody(); echo $mime_message; } catch (phpmailerException $e) { echo $e->errorMessage(); //Pretty error messages from PHPMailer } catch (Exception $e) { echo $e->getMessage(); //Boring error messages from anything else! } ?> 
+5
source share

I tried to achieve the same.

It seems that MSIE is the only browser that currently supports multiple / sibling initially (Opera may have some support, but I did not play with it), however when downloading such files it seems to completely ignore the HTTP headers regarding the mime type and other things (such as caching information, location, etc.). Indeed, MSIE will open the file only if the URL has the file extension .mht (that is, it violates most HTTP rules).

In addition, MSIE will happily download everything that contains the embedded multipart / related file (provided that it is encoded as plain text) without providing the data contained in the application. The functionality for processing these files seems to be a quick hack that allows you to view files

If you reconfigure your web server to parse .mht urls using php (or use mod_rewrite to reassign the url), you will probably find that it can work (although I would not hope that it works with added query string). But expect problems if you create dynamic content (it will not expire from the cache / update when you expect).

Shorten a long story short - it won’t work.

0
source share

All Articles