I've been struggling with this for days and can't find a clear solution. Hope someone can help.
I need to be able to style a SimpleNews newsletter differently than system emails. In particular I want to change the body background colour for the newsletter (along with other things but the body styling has been the core problem). The approaches I've tried are:

1. Add a class name to the $classes array in html.tpl.php

Using template.php I added a class name to be applied to the body tag and started testing. The problem is that Emogrifier ignores the class styling on the body tag and just applies the tag styling. Didn't proceed further with this option after discovering this.

2. Add stylesheet via template_preprocess_simplenews_newsletter_body in template.php

I added the line:

drupal_add_css(drupal_get_path('theme', 'newsletter') . '/css/newsletter.css', array('group' => CSS_THEME));

but it seems template_preprocess_simplenews_newsletter_body doesn't preprocess css. Not sure why. I thought in Drupal 7 the whole idea was to preprocess all rendered elements and then create the output at the end of the process.
This worked fine in both template_preprocess_html and template_preprocess_page but not in template_preprocess_node (although this did work for the browser page when I switched themes to test further - just not the emailed page) and template_preprocess_htmlmail (this worked once and then didn't work again after that).

3. Add stylesheet via template_preprocess_page

So I can get a result using the page preprocess function and I found that the page node type for the newsletters was 'simplenews' when testing pages in the browser. I assumed all I needed to do was:

function newsletter_preprocess_page(&$variables) {
  if ($variables['node']->type == 'simplenews') {
    drupal_add_css(drupal_get_path('theme', 'newsletter') . '/css/newsletter.css', array('group' => CSS_THEME));
  }
}

but this doesn't work once it is sent to the mail server. It works in the browser though.

As an aside I'm having a really difficult time debugging. On browser pages I use Drupal for FireBug all the time but this doesn't show up when sending mail and it seems that there are different variables between the browser pages and the mail pages - for example, what happened to my node variable once it got sent? Any clues on this aspect would also be very much appreciated.

So I'm at a real loss now on the best approach to take to personalise the CSS for my newsletter.

Hope someone can help.

Comments

pillarsdotnet’s picture

Did you look at http://drupal.org/node/1260178 by any chance?

Das123’s picture

Thanks for the reply. I did look through it earlier but perhaps I didn't really understand what was going on. Dissecting it using the process listed - would it basically not work for the following reasons?:

node, simplenews and htmlmail templates are all loaded and processed as part of the preparation procedures (steps 8, 10, 11 and 15). At this point these are no longer part of the Drupal node structure but are just a type of render array - so any preprocessing will be lost from these procedures due to the sendmail processes.

Echo then wraps the render array triggering both the html and page preprocesses which is why style-sheeting can be applied at this point.

The problem being that at this point the render array is no longer part of the standard Drupal structure so structures such as node->type no longer apply.

So the problems still persist unfortunately:

1. How can template_preprocess_html or template_preprocess_page be used to flag if the rendered message is SimpleNews or a generic HtmlMail type? I need to load a different CSS file depending on the email being a system email or a newsletter.

2. What is the best way to debug or look inside the render array used for Echo in step 17? I'm flying blind once the message hits the sendmail process and would really appreciate some clues about this please.

Das123’s picture

I'm really at a loss here, sorry. What I have so far as a method of trying to spot potential variables is in template.php:

function newsletter_preprocess_page(&$variables) {
  $variables['test'] = $variables;
}

and in page.tpl.php

  print_r($test);

I see the standard variables that are usually sent to the page.tpl.php file but most of the values are now missing. Eg. $variables['node'] is now empty. There is no reference to simplenews at all any more.

I don't have a clue how to add a variable within this process. Is what I'm trying to do not possible? I'd be surprised if I'm the only person ever trying to do this.

pillarsdotnet’s picture

I see your problem in #1 and don't have any easy answers right now.

In #2 my only comment is that echo doesn't deal with a render array -- it only passes raw HTML to the Drupal page generation process.

So yeah, this whole system probably doesn't do what you want it to, absent some kind of extension.

And realistically, I'm not going to have time to dig into it this week; sorry.

Das123’s picture

Thanks again for your reply. At least I can stop trying to change the body tag now. :) I'll instead wrap the newsletter format in a div and style it locally there.

salvis’s picture

Status: Active » Closed (outdated)