PHP Classes

CSS files

Recommend this page to a friend!

      MIME E-mail message sending  >  MIME E-mail message sending package blog  >  How Can PHP Send Emai...  >  All threads  >  CSS files  >  (Un) Subscribe thread alerts  
Subject:CSS files
Summary:Problem getting css to html mail
Messages:7
Author:Termos
Date:2009-11-10 07:41:20
Update:2009-11-11 14:34:45
 

  1. CSS files   Reply   Report abuse  
Picture of Termos Termos - 2009-11-10 07:41:20
Hi

Is there a way to include a css-style file to the html mail, I cant seem to get it to work. Only way I can get styles embedded is if I set the style directly at each element as a style attribute. Writing style-tags in head-tag or body-tag doesn't seem to work either.

This is how I include the css file now:
$cssStyleFile = array("FileName"=>"http://www.example.com/css/test.css",
"Content-Type"=>"text/css",
"Disposition"=>"inline"
);

$email_message->CreateFilePart($cssStyleFile, $csspart);

Of course I add the $csspart later in the code with:
$related_parts = array($alternative_part, $csspart);
$email_message->AddRelatedMultipart($related_parts);

In the html-code I have testet with reference to the css file and without. The css-file gets included in the mail as attachment.

Maby Im missing something when I relate to the css file in the html-code? Should it be done like with images, create some sort of 'id' for the css that can be used in the html-code?

I am using a gmail-account as a test receiver, could it be that gmail ignores the css file?

Please advice.

  2. Re: CSS files   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2009-11-10 14:40:51 - In reply to message 1 from Termos
Yes, you need to use the CSS part message-id to reference it in the HTML.

  3. Re: CSS files   Reply   Report abuse  
Picture of Termos Termos - 2009-11-11 07:47:40 - In reply to message 2 from Manuel Lemos
I cant seem to get it right.

Here's part of the code:

...
$cssStyleFile = array("FileName"=>"http://www.test.com/test.css",
"Content-Type"=>"automatic/name",
"Disposition"=>"inline"
);

$email_message->CreateFilePart($cssStyleFile, $csspart);

$cssReference = $email_message->GetPartContentID($csspart);
...

Then I embedd the $cssReference variable in the html code, like so:
<link rel='stylesheet' type='text/css' href='cid:{$cssReference}'>

Then I use the class attribute as usual in the html code. I have checked that $cssReference variable gets a value like "some_hash.css"

The css file gets attached to the mail but classes is ignored by the html code.

What am I missing?

  4. Re: CSS files   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2009-11-11 08:16:14 - In reply to message 3 from Termos
You need to define the HTML as a string like this:

'<link rel="stylesheet" type="text/css" href="cid:'.$cssReference.'">'

  5. Re: CSS files   Reply   Report abuse  
Picture of Termos Termos - 2009-11-11 10:18:22 - In reply to message 4 from Manuel Lemos
Sorry, it still doesn't work. The HTML code that is set to the mailbody looks like this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>css test</title>
<link rel="stylesheet" type="text/css" href="cid:a155de3905deeed57cb3ec5c2747d037.css">
</head>
<body>
<div class="divven">This text should be white with blue background!</div> </body>
</html>

And the css looks like this, I have tried it locally and it works like expected:

body{ background-color: blue; }
.divven{ color: white; }

Part of code to create the mailobject:

$cssStyleFile = array("FileName"=>"http://test.com/test.css",
"Content-Type"=>"automatic/name",
"Disposition"=>"inline"
);

$email_message->CreateFilePart($cssStyleFile, $csspart);
$cssReference = $email_message->GetPartContentID($csspart)

$html_message = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head><title>css test</title></head>
<body>
<link rel="stylesheet" type="text/css" href="cid:'.$cssReference.'">
<div class="divven">This text should be white with blue background!</div>
</body>
</html>';

$email_message->CreateQuotedPrintableHTMLPart($html_message, "", $html_part);

$text_message = "This is an HTML message. Please use an HTML capable mail program to read this message.";
$email_message->CreateQuotedPrintableTextPart($email_message->WrapText($text_message), "", $text_part);

$alternative_parts = array($text_part, $html_part);
$email_message->CreateAlternativeMultipart($alternative_parts, $alternative_part);

$related_parts = array($alternative_part, $csspart);
$email_message->AddRelatedMultipart($related_parts);

$error = $email_message->Send();

Thanks for your assist.

  6. Re: CSS files   Reply   Report abuse  
Picture of Termos Termos - 2009-11-11 10:31:50 - In reply to message 5 from Termos
It might be that my mail client doesn't support neither "style" och "link" tags in html mail.
ref:
campaignmonitor.com/blog/post/2533/ ...


  7. Re: CSS files   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2009-11-11 14:34:45 - In reply to message 5 from Termos
That seems to be working, but keep in mind that some Web mail systems simply discard all tags outside the body tag, so any tags in the head section will be ignored.

The solution is to insert <style> tags in the body section. Take a look at the PHPClasses newsletters to see an example.