Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 340
  • Last Modified:

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 Close
Calvin Close
1 Solution
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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now