PHP multi-part mail with embedded css

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?
Calvin ClosePresidentAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.

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;
Calvin ClosePresidentAuthor Commented:
Actually, I fixed this by putting the <style type="text/css"> section in the <BODY> section of the HTML, and then everything worked fine.
PAQed with points refunded (500)

Community Support Moderator

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.