1

I'm working on a webshop (used to be for just one product, expanding it to multiple products and multiple shops hooked up to one backoffice, can't use PrestaShop because of compatibility).
We send 3 kinds of emails, all filled with certain data.

First we send an order notification when someone orders and pays (that's when we see it in the list of orders to be processed), this contains the order details including full list of products and prices.

Then we send another email once we mail the order, this includes pretty much the same as the order notification (except some details it doesn't contain and it has some extras).

We also receive an email when people use the contact form that uses the same styling.

Right now what happens is that the system grabs all the items in the order and creates a string containing the table with the items. This works fine, but it's sloppy. I also have the complete email script (using PHPMailer) copied and pasted to 7 different spots where it is possible that an email will be sent.

I need a better way to do this. I was thinking about writing a wrapper for PHPMailer automatically doing most things, and maybe include a few helper functions to do stuff like the item table, but I'm really not sure how to best go about this. Smarty and other templating engines look too bulky for this.

Is there one 'perfect' way to do this?

4

There's no single explicit approach here for making "perfect" mail templates, as everything depends on the complexity of your templates and scalability you want to achieve.

Through my experience, I've been working with different approaches, and all of them served well for the specific project needs. It's matter of personal taste and skill, one have to find the wise balance between the complexity of the templates themselves and the ease of implementation, in order not to overdo the thing.

Criteria, when choosing approach/tools:

  • The complexity of template:
    • can it be solved by simple placeholders,
    • or it will contain iterators, additional conditional logic;
  • Frequency of modification:
    • dynamic (fetched from DB, generated on the flight, frequently changed through backend, etc.)
    • static
    • themes support (may be counted as sub-type of dynamic)
  • Computational resources:
    • amount emails sent per period of time: mass-emails, sole emails;
    • memory/CPU consumption by your script;
    • caching mechanisms and other optimizations;
  • Tools/libraries already involved to the project:
    • utilize built-in templating tools from existing email library;
    • framework tools;
    • template engine used for the UI;
  • Further maintenance/support/scalability of the solution in long-time perspective.

Two solutions:

1. The simple way:

2. The advanced way:

  • PHP Template engines: TWIG template engine (excellent choice for Symfony, Laravel, OctoberCMS, etc.), SMARTY template engine (or any other template engines: dwoo, Mustache, Volt, etc.)
  • built-in tools to PHP Framework or CMS;
  • specialized email libraries (PHPMailer, SwiftMailer, Pear, etc.);
  • external APIs and external services(MailChimp, ActiveCampaign, SendGrid, etc)

The everything above said is my experience and personal opinion.

-2

That's exactly what I've done with similar projects, created a wrapper function around phpmailer.

I then have an email template file, eg:

<html>
<body>
<h1>{SUBJECT}</h1>
{BODY}
</body>
</html>

I then have a function which I call with the relevant data which loads the templates and does text replacements.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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