adding new templates to newsletter module
Hi,
Sorry if this is a simple question. Is there a way of adding more templates to the newsletter module? I only get "product_notification" and "newsletter". I have various subscribers and would like to send out emails using different templates without having to alter the default newsletter template everytime.
Thanks
Christoph
Re: adding new templates to newsletter module
What sort of changes do you mean by "new template" ?
I'm trying to understand the "why" behind your question.
Re: adding new templates to newsletter module
OK, what I am trying to do is send a newsletter out to various groups of subscribers. Those groups should get different types of emails with different layout. The only way I seem to be able to achieve this is by manually altering the (single) newsletter template. What I would like to be able is to have a template for group1, group2 etc and then send them a newsletter based on that template. Does this make it clearer?
Thanks
Christoph
Re: adding new templates to newsletter module
Quote:
Originally Posted by
christoph_lutz
Does this make it clearer?
Not really.
What do you need to change between templates? The only thing in the templates is name/message/copyright/disclaimer ... and the newsletter template also includes an unsubscribe link.
How are you preparing your newsletter content? Would it make sense to put your custom styling etc directly into the HTML of the newsletter?
Or am I missing something about what you're trying to accomplish by using alternate templates?
What specific distinction are you aiming for?
Re: adding new templates to newsletter module
I am preparing the html content outside in a text editor. The problem is that I would like different logos, fundamentally different layout etc. I have experimented with having a "template" that just puts out the html I paste in and nothing else but that means that things like name, copyright, disclaimer etc could not be done easily and if I want those then I'd need to use the template and then I am restricted again to the kind of html framework that comes in the single template...
Christoph
Re: adding new templates to newsletter module
Okay.
How do you currently differentiate between subscriber groups when sending these various newsletters?
Re: adding new templates to newsletter module
i have amended the subscribe to newsletter option where users can tick different categories, so I can select those when sending a newsletter
Christoph
Re: adding new templates to newsletter module
1. edit /includes/functions/functions_email.php
around line 329, find this section. Add the additional code as shown:
Code:
if (!file_exists($template_filename)) {
if (isset($block['EMAIL_TEMPLATE_FILENAME']) && file_exists($template_filename_base . $block['EMAIL_TEMPLATE_FILENAME'] . '.html')) {
$template_filename = $template_filename_base . $block['EMAIL_TEMPLATE_FILENAME'] . '.html';
} elseif (file_exists($template_filename_base . str_replace(array('_extra','_admin'),'',$module) . '.html')) {
2. /admin/includes/modules/newsletter.php
around line 99, find this section. Add the new line, as shown:
Code:
$html_msg['EMAIL_FIRST_NAME'] = $audience->fields['customers_firstname'];
$html_msg['EMAIL_LAST_NAME'] = $audience->fields['customers_lastname'];
$html_msg['EMAIL_MESSAGE_HTML'] = $this->content_html;
$html_msg['EMAIL_TEMPLATE_FILENAME'] = $whatever_filename_you_specify_here;
zen_mail($audience->fields['customers_firstname'] . ' ' . $audience->fields['customers_lastname'], $audience->fields['customers_email_address'], $this->title, $this->content, STORE_NAME, EMAIL_FROM, $html_msg, 'newsletters');
3. Now make whatever additional code edits (to the newsletters.php file) would be required to get the $html_msg['EMAIL_TEMPLATE_FILENAME'] variable to contain the name of the template you want loaded.
The way it's coded above, it'll expect files named like this:
/email/email_template_*******.html
... where ******* is the name you get inserted into the $html_msg['EMAIL_TEMPLATE_FILENAME'] variable.
Since you've made some code edits to allow you to select groups, perhaps you can link a text string with those groups which could be used as the filename piece.
4. repeat #2 & #3 for the product_notifications module file if necessary.
Re: adding new templates to newsletter module
Thanks for your reply and the code and the explanation. I have made the amendmends but don't understand the last step - how do I now compose a message and send it to the right group of people? What do you mean by text string? Would it not be that I should select the template from the drop-down that currently says "newsletter" and "product_notification"?
Thanks
christoph
Re: adding new templates to newsletter module
Quote:
Originally Posted by
christoph_lutz
Thanks for your reply and the code and the explanation. I have made the amendmends but don't understand the last step - how do I now compose a message and send it to the right group of people? What do you mean by text string? Would it not be that I should select the template from the drop-down that currently says "newsletter" and "product_notification"?
That dropdown determines which module to run from the modules/newsletters folder.
If you want to add something to that dropdown and write a new module to go with it, feel free.
I've only given you the steps on how to reference additional templates from within the code.
I have no idea how you've altered things to collect additional newsletter choices, nor do I know how you currently select which audience to send to.
If you use a built-in audience feature from the query_builder tools, then you should be able to match an audience name with a template name so that there are no interface changes required to the newsletter screen. Sorry if that's too much tech talk -- you sounded like you had a handle on programming when you said you altered things to allow additional newsletter choices.
Re: adding new templates to newsletter module
Quote:
Originally Posted by
DrByte
That dropdown determines which module to run from the modules/newsletters folder.
If you want to add something to that dropdown and write a new module to go with it, feel free.
if I just literally copied the newsletter.php file and changed the template name in there, would that work? Then I'd copy that file up to the server and it would appear in the drop-down. What I don't know if other functions would reference back to the newsletter.php file?
Quote:
Originally Posted by
DrByte
Sorry if that's too much tech talk -- you sounded like you had a handle on programming when you said you altered things to allow additional newsletter choices.
It's not too much tech talk - I do have a handle on programming but are relatively new to zencart and have not delved in the email coding at all - mainlly dealt with the template side of things so far.
Re: adding new templates to newsletter module
Quote:
Originally Posted by
christoph_lutz
i have amended the subscribe to newsletter option where users can tick different categories, so I can select those when sending a newsletter
"How" are you selecting those when you send a newsletter?
Re: adding new templates to newsletter module
By selecting the audience from the dropdown. I have added queries to the query builder table that gets me the right group of customers
Re: adding new templates to newsletter module
Okay, then one approach could be to use the query_name value from the query-builder data (ie: the audiences list) to determine which template to use.
1. Rename your special queries like this:
Code:
Subscriber Group: Bleeding Edge Racers
or
Subscriber Group: All Stars ~ Heavy Hitters
2. Create corresponding email template files.
The email templates for the example names above would be:
Code:
/email/email_template_bleedingedgerac.html
/email/email_template_allstars.html
Explanation:
The template names will be built from the query_name (audience names) starting with 'Subscriber Group: ' followed by up to 15 chars for the template name (or until a ~ is found).
Converted to lower-case letters to find the template filename.
If a filename matching the pattern isn't found, the module name will be used instead.
3. Make the following code edits to the admin/includes/newsletters/newsletter.php:
Code:
function send($newsletter_id) {
global $db;
// Look for audience names starting with 'Subscriber Group: ' and accept up to 15 chars for template name (takes name up to ~ or if no ~ then 15 max chars)
$my_template_name = str_replace('Subscriber Group: ', '', $this->query_name);
$my_template_name = preg_replace('/^[0-9a-zA-Z_~]/', '', $my_template_name);
$pos = strpos($my_template_name, '~');
$my_template_name = strtolower(substr($my_template_name, 0, ($pos > 0 && $pos < 15 ? $pos : 15) ));
$audience_select = get_audience_sql_query($this->query_name, 'newsletters');
$audience = $db->Execute($audience_select['query_string']);
$records = $audience->RecordCount();
if ($records==0) return 0;
$i=0;
while (!$audience->EOF) {
$i++;
$html_msg['EMAIL_FIRST_NAME'] = $audience->fields['customers_firstname'];
$html_msg['EMAIL_LAST_NAME'] = $audience->fields['customers_lastname'];
$html_msg['EMAIL_MESSAGE_HTML'] = $this->content_html;
$html_msg['EMAIL_TEMPLATE_FILENAME'] = $my_template_name;
This all requires that you implement the functions_email changes I mentioned earlier.
Re: adding new templates to newsletter module
Hi Dr Byte,
Nice one, this has worked - although your regular expression did not quite work as it cut off some of the name of the group and did not take out the spaces. I have just shortened it to [\s] so it's working fine now.
Just one thing: Although I have now done it your way, I had thought I'd add a column to the table and put the template name in there. Would you think that's a bad idea?
Thanks
Christoph
Re: adding new templates to newsletter module
Feel free to do as you wish with it.
I looked at the new-column-in-table approach and realized there'd need to be coding changes in several places in order to make that work, and I didn't want to risk breaking other things without thorough testing ... so I opted to give you the simplest route.
Sorry about the bad regex ... I meant to go test that but forgot :eek:
Re: adding new templates to newsletter module
Hi,
Fair enough, I just thought the extra column would be relatively simple but have not looked at the wider implications. In any case, I have got it working beautifully now which is great. I have also put in an alert in the result screen to show what template is being used so that other people who may be using it will see what's happening.
Thanks for all you help on this one!
And yes, regular expressions catch me out at times, too :) No worries
Christoph
Re: adding new templates to newsletter module
Dr Byte,
I've done something similar here, where i've duplicated the newsletters module to create an additional one, let's call it "news2".
I was able to use a different email template I created WITHOUT modifying functions_email.
In the duplicated newsletter module... "news2" I changed the following at line 351:
Code:
<?php
zen_set_time_limit(600);
flush();
$i = $module->send($nInfo->newsletters_id);
?>
TO THIS
Code:
<?php
zen_set_time_limit(600);
flush();
$current_page_base = 'news2';
$i = $module->send($nInfo->newsletters_id);
?>
I'm wondering if this is a bad way to accomplish this.
Thanks
DD
Re: adding new templates to newsletter module
I suppose if you only ever intend to send to one group, that's fine.
The workaround to the original poster's question was way more generic than what you are suggesting, and doesn't require changing code everytime one wants to send a different newsletter.