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! } ?>
Carlos Lima
source share