0

I am trying to create a simplenews newsletter, also using Simplenews Scheduler, to weekly send out a newsletter with the content defined by views.

I have a content type set up, and the views defined. I have the views inserted into the content type using Viewfield. This all works fine and the email sends with the views content. However, the styling is not correct.

My views return lists of full nodes which I have set the View Mode of as "Email: HTML". Within the templates in my theme I have added the mark-up I want for the node with that View Mode. When viewing the newsletter on the site (as a node) it all looks correct. However, when I send the newsletter, the email I receive has the view in it's raw format (like that which is displayed in the view preview) and has none of the mark-up I've specified.

How can I get the correct mark-up showing? I don't know if I'm just missing a setting somewhere or something.

Many thanks.


My newsletter template (simplenews-newsletter-body--13--email-html.tpl.php) is as follows:

<?php /*This appears in the email*/ ?>
TEST NEWSLETTER TEMPLATE
<div>
    <?php /*The following should print the full newsletter node including the views*/ ?>
    <?php print render($build); ?>
</div>
<div>
    <?php /*The following should print a single view*/ ?>
    <?php print views_embed_view('news_upcoming_events','block_1'); ?>
</div>

The event node template (node--event.tpl.php) which is returned in the view used in the newsletter template:

<?php if($view_mode == 'email_html'){ ?>
    TEST EMAIL TEMPLATE
    <div>
        <h3><a href="<?php print $node_url; ?>"><?php print $title; ?></a></h3>
        <div><?php print render($body); ?></div>
    </div>
<?php }else{ ?>
    NORMAL VIEW
<?php } ?>

The newsletter template all appears fine in the email. I see the line TEST NEWSLETTER TEMPLATE, but the event node templates are not used in the views output. It is instead some generic drupal/views output.


This is the way the view is setup.

View configuration

And this is the preview displayed with in the view setup. This is also what is appearing in the email when the newsletter is sent, instead of the mark-up declared in my template file. (It is correct when viewing the node on the website though)

Preview of the view

0

4 Answers 4

1

This sounds very similar to what I did a while ago. I documented my setup on drupal.org. This seems to work for your already. I think your MailSystem is the source of error here. Make sure the MailSystem that is used by Simplenews is using the required style sheets and of course the custom templates from your web theme. Also note, that not all styles that are possible on a website an be used in an email, in fact, only a pretty small subset of styles will work at all.

Take a look at the Emogrifier module, it converts your stylesheets to inline styles for emails. Also, for images and other URLs, the Pathologic module may be of help as it converts relative URLs to absolute ones.

6
  • Thanks for your response. Yes, same setup as you've documented. May have been your documentation I read originally, it was a while ago I started this project. It's not to do with stylesheets, as I'm going to use inline styling once I have got it working. At the moment I'm just trying to get it to output the correct markup. Checking my Mail System settings the "Theme to render the emails" is set correctly. And the node template for the email is correct, its just the templates for the nodes in the views which are not loading. Any ideas? Thanks. Nov 3, 2014 at 16:50
  • Well, that's weird, but I have an idea what might happen under specific circumstances. Can you actually add some (unconditioned) DEBUG message in your template which would then show up in your newsletter? That is, do you know if your template file is used at all? If that works, it might be that the simplenews node display type collides with the views display type. But it's hard to test for me though.
    – Paul
    Nov 3, 2014 at 18:30
  • My template for the newsletter is definitely used. I've already added my own "THIS IS HTML EMAIL" text to it to check this. I've done the same for the templates of the nodes that show for the view, and these display correctly when viewing the newsletter node on the site, but not when sending the test newsletter. The view mode I'm using all round is "Email: HTML". Difficult one to really share anything more useful like code etc. Might try some other ways of inserting views to see if that works. Nov 3, 2014 at 19:38
  • My question was, if the text "THIS IS HTML EMAIL" also shows up in the email. Maybe you could share your template file pastebin or similar?
    – Paul
    Nov 3, 2014 at 19:48
  • Yes it does show for the newsletter template, but not for the nodes that are returned from the views. So I send the newsletter, the email I receive has "THIS IS HTML EMAIL" at the beginning as it is in the newsletter template. But it does not show this message for any of the nodes the views return. Just tried views_embed_view() straight in the template file for the newsletter which has the same issue. I will see about posting the template file. Nov 3, 2014 at 19:53
0
+50

I just gave it a try and can confirm that the newsletter with the embeded view is ignoring the templates that work fine when you view them in the browser. (I don't know why, so maybe raise an help request on their Drupal issue page?)

I tried a view with Full Content nodes and edited the node.tpl.php (like you are trying) and using a Fields view and editing the views-view-fields.tpl.php and neither worked in the sent email (but both worked in the bowser).

However - fields set in the view in the normal way were respected and sent in the email so, a solution is to change your embeded view to use Fields instead of Full Content and create fields for all the things you want. eg Title, body etc etc.

Overriding the field using Rewrite Results works fine also.

5
  • Thanks for your answer and confirming that my issue does exist by default. I've looked at using fields, but in order to do the inline styling I want, I would need to use the view templates which also seem to be ignored in the email! Rewrite results will remove any inline styling. Nov 7, 2014 at 10:45
  • a potential workaround for that - drupal.org/node/853880 Nov 8, 2014 at 21:51
  • actually, I don't think that helps in the end ! Nov 8, 2014 at 22:00
  • Thanks for your help. The use of fields in the view did lead me to the eventual solution which I have posted. Nov 13, 2014 at 11:25
  • well done - looks good, I was going to suggest Views PHP but its sometimes badly received advice. Nov 13, 2014 at 11:32
0

I assume you use Simplenews 7.x-1.x version.

Try to override your $node->content using hook_node_view($node, $view_mode) instead of template.

Also have a look at simplenews_build_newsletter_mail(). And SimplenewsSourceNode::buildBody(). If simplenews really send email using these functions, than you can override simplenews_newsletter_body theme with template_preprocess_simplenews_newsletter_body().

0

So it turns out even looping through the view within the template and loading the nodes individually using the nid doesn't work either. Therefore, I needed to find a way to get the view to output with the exact mark-up I require.

The solution I found was using Views PHP. Although thus has performance issues, it is only needed to produce the newsletter which is sent, and so is never experienced by the end user.

I added all the fields I needed in the view and then used a "Global: PHP" field to put it all together with the inline styling I required. This all went within the "Output code" section of the field.

Unfortunately, the available variables don't all work in the Views PHP module and so it is not straight forward to put together, but is possible. An example of my final solution looks something like this:

<?php
    //Get the url for the node
    $options = array('absolute' => TRUE);
    $url = url('node/' . $data->nid, $options);
?>
<table style="margin:0; padding: 0; border: 0; outline: 0;border-collapse: collapse;">
    <tbody style="margin:0; padding: 0; border: 0; outline: 0;">
    <?php
        //load the images from the view
        foreach($data->field_field_event_image as $loadedImage){
            $file = file_load($loadedImage['rendered']['#item']['fid']);
            $image = image_load($file->uri);
            $content = array(
                'file' => array(
                '#theme' => 'image_style',
                '#style_name' => $loadedImage['rendered']['#image_style'],
                '#path' => $image->source,
                '#width' => $image->info['width'],
                '#height' => $image->info['height'],
            ),
        );
        print '<tr style="margin:0; padding: 0; border: 0; outline: 0;"><td style="padding:0; margin:0; border: 0; outline: 0;">';
        print drupal_render($content);
        print '</td></tr>';
    }
    ?><tr style="margin:0; padding: 0; border: 0; outline: 0;">
        <td style="padding:10px; border: 0; outline: 0; margin:0;">
            <h3 style="margin: 0;padding:0;font-weight: normal;">
                <a href="<?php print $url; ?>" style="color:#666666;font-size: 1.3em;font-family: Verdana,Tahoma,sans-serif;">
                    <?php print $row->title; ?>
                </a>
            </h3>
            <div style="font-family: Verdana,Tahoma,sans-serif;color:#999999;font-size: 0.9em;"><?php
                foreach($data->field_field_start_date as $date){
                    print $date['rendered']['#markup'];
                }
            ?></div>
            <div style="color: #999999;margin: 5px 0 10px;font-size: 1em;font-family: Verdana,Tahoma,sans-serif;">
                <?php print $data->field_body[0]['rendered']['#markup']; ?>
            </div>
            <div style="text-align: right;">
                <a href="<?php print $url; ?>" style="color:#999999;font-family: Verdana,Tahoma,sans-serif;font-size: 1em;">Read more</a>
            </div>
        </td>
    </tr></tbody>
</table>

Newsletter all looks good now and I have already started to send it out. Thanks everyone for their thoughts on the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.