PHP multi-part mail with embedded css

Posted on 2006-04-13
Last Modified: 2008-03-06
I am trying to get a multi-part MIME email working.  I created a string $body with embedded css (is using <style>) at the start of the $body variable.  When I send the email as HTML only (not multi-part), all the styles work,  I have converted it to multi-part and now the styles do not work.

My $body:

<style type="text/css">
body {
      background-color: #ffffff;
td {
      font-family: Verdana, Arial, Helvetica, sans-serif;
      font-size: 12px;
.td_section {
   color: rgb(255,255,255);
   background-color: rgb(61,79,143);
.rowDark {
   font-size: 11px;
   color: rgb(0,0,0);
   background-color: rgb(232,232,232);
   padding: 1px 4px;

Dear Sir,
html stuff

I have also created another string $body_text to put my plain text into.

My php code:

      if (strtoupper(substr(PHP_OS,0,3)=='WIN')): $eol="\r\n"; elseif (strtoupper(substr(PHP_OS,0,3)=='MAC')): $eol="\r"; else: $eol="\n"; endif;
      $header      =      "From: My Name <" . $from . ">". $eol;
        $header      .=      "Reply-To: My Name <". $from . ">".$eol ;
        $header      .=      "Return-Path: My Name <". $from . ">".$eol ;

      $header .= "Message-ID: <".$now." TheSystem@".$_SERVER['SERVER_NAME'].">".$eol;
      $header .= "X-Mailer: PHP v".phpversion().$eol;          // These two to help avoid spam-filters

      # Boundry for marking the split & Multitype Headers
      $header .= 'MIME-Version: 1.0'.$eol;
      $header .= "Content-Type: multipart/related; boundary=\"".$mime_boundary."\"".$eol;
      $msg = "";
      # HTML Version
      $msg .= "--".$mime_boundary.$eol;
      $msg .= "Content-Type: text/html; charset=iso-8859-1".$eol;
      $msg .= "Content-Transfer-Encoding: 8bit".$eol;
      $msg .= $body.$eol.$eol;
      # Text Version
      $msg .= "--".$mime_boundary.$eol;
      $msg .= "Content-Type: text/plain; charset=iso-8859-1".$eol;
      $msg .= "Content-Transfer-Encoding: 8bit".$eol;
      $msg .= $body_text.$eol.$eol;
      # Finished
      $msg .= "--".$mime_boundary."--".$eol.$eol;  // finish with two eol's for better security. see Injection.


Why does it not work?
Question by:calvinclose
    LVL 49

    Expert Comment

    Although not a direct answer to your problem: When it comes to using css in html email, it is best to use inline styles only, ie: style="". This method has proven itself to be working where <style></style> tags failed.

    LVL 11

    Expert Comment

    You are missing the multipart/alternative in your message

    $msg.="Content-Type: multipart/alternative;\n";

    Add the above two lines

    In your code the following
    $msg = "";

    #HTML Version
    $msg .= "--".$mime_boundary.$eol;

    would be

    $msg = "";

    $msg.="Content-Type: multipart/alternative;\n";

    #HTML Version
    $msg .= "--".$mime_boundary.$eol;

    Author Comment

    Actually, I fixed this by putting the <style type="text/css"> section in the <BODY> section of the HTML, and then everything worked fine.

    Accepted Solution

    PAQed with points refunded (500)

    Community Support Moderator

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    A colleague recently asked me about how to give his client a small part of the web site that could be completely under the client's control.  Since I have done this sort of thing before to add emergency banners to a web site, I decided I would creat…
    Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn how to count occurrences of each item in an array.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now