Jump to content

Strategies for storing and using email templates


onjegolders
 Share

Recommended Posts

Hey guys,

Was just interested in your different techniques for sending HTML emails through your templates. More specifically, where you store your email codes?

In the past, using smaller, simpler emails I was "OKish" with keeping all the HTML garbled in my form processing at the top of a page but am thinking there must be better ways surely?

Do you use includes? ProcessWire templates? Just interested in seeing your different techniques. 

  • Like 1
Link to comment
Share on other sites

Using pages to build the letter, parent renders all the children's markup.

- parent (page in the page tree, this is the newsletter) this markup is used for the news letter.

    - header (contains markup from header)

    - article (contains markup from image text header link etc.)

    - article etc..

    - image

    - article

    - footer

  • Like 1
Link to comment
Share on other sites

I build an Inputfield that makes connection with mailchimp. On page save a campaign gets created or updated on MailChimp. When you're ready with the newsletter in ProcessWire the newsletter is ready for sending In MailChimp (you only need to go to MailChimp en press send now or set to schedule it).

The real problem here is that MailChimp is quicker then me and the used API is deprecated now :-)

If the people who are clever enough to copy sourcecode and can paste it in a newsletter service that a good route I think. 

Link to comment
Share on other sites

Thanks, that's interesting. 

I was thinking more of a way of mimicking somewhat a Framework approach here. 

// Pseudocode

if (form success)
 
$p = new Page();
// fill in new contact request page details
 
// Now send an email using a template 
// and take with it these variables (name, email etc)
Link to comment
Share on other sites

Thanks Horst, yes it's not quite the same approach as you say. I'm going to have another think.

Am just wondering if there is some way of sending new variables to a render operation?

Something link

$data = [

    'name' => $input->get->name;

    'email' => $input->get->email;

];

$email_message = $pages->get("template=email_template")->render($data);

// send email with message

Link to comment
Share on other sites

@onjegolders, thanks for starting this topic.

I've been working a lot on my email thingy, it has ballooned into a fledgling CRM now, and can do all kinds of neat tricks..

lately i have been implementing HTML templates, and i'm trying to keep it dead-simple; this is still sort of a WIP, but in short:

the template (email-template) has fields:

  • title
  • body, ckeditor - specially configured to accept almost all markup;
  • body_atts (for example [bgcolor=#161616" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0])
  • images
  • custom_css (for example [a:link {color: #61c7dd;}a:hover {color: #59b8cc;}] )
	// If using a template, 
	if($message->template_select) {
		$bodyatts = $message->template_select->body_atts ? ' ' . $message->template_select->body_atts : '';
		$body = "
				<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01//EN'
				'http://www.w3.org/TR/html4/strict.dtd'>
				<html lang='en'>
				<head>
					<title>{$message->title}</title>
					<meta content='text/html; charset=utf-8' http-equiv='Content-Type'>
					<style type='text/css'>{$message->custom_css}</style>
					</head>
				<body$bodyatts>
				";
				
		$body .= $message->body;
				
		$body .= "</body>
					</html>";
					
		$body = str_replace('/site/assets/files/', $base_url . '/site/assets/files/', $body);
		$mail->bodyHTML($body); 
		}

now i'm thinking of making it more flexible and having a field for the head since some of the templates i have tried to use more recently needed different head markup...

how this works is that when you crate a new message, my system clones all of the fields from the template into the new message and then you edit from that clone; so there is a module running on save of the email that checks to see if a template is selected and then replaces the body of the current email you are editing (and the other fields) from the template, but leaves the image references in tact, so that the images used in an html template are still stored with the template, but the body, css, and body_atts can be adjusted if necessary on a per-email level..

  • Like 1
Link to comment
Share on other sites

Am just wondering if there is some way of sending new variables to a render operation?

Something link

$email_message = $pages->get("template=email_template")->render($data);

There is a way, and it works exactly as you described :D Only your syntax is slightly off. You need to pass an array like so (or prepare it beforehand):

$SomePage->render('optional_non-default_template.php', array('recipient'=>'Mr. North'));

Then on that page’s template you can simply call $options['recipient'] and it will === 'Mr. North'.

  • Like 3
Link to comment
Share on other sites

Thanks Macrura, this looks like a really clever approach. Love the name "email thing" by the way ;)

Still not sure if this is being triggered by creating something in the backend, whereas I am more talking about triggering emails via a signup form or that sort of thing.

Then again it's getting late in the afternoon and my brain is a bit fuzzled :D


There is a way, and it works exactly as you described :D Only your syntax is slightly off. You need to pass an array like so (or prepare it beforehand):

$SomePage->render('optional_non-default_template.php', array('recipient'=>'Mr. North'));

Then on that page’s template you can simply call $options['recipient'] and it will === 'Mr. North'.

Wow, did not know that, I need to go back to the docs :D

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...