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?

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:

  • string generation by means of PHP;
  • usage of simple %placeholder% replacement mechanisms (str_replace, strtr, preg_replace, etc.)
  • simple PHP templating and conditional logic (short open tags in PHP and alternative syntax for control structures)

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.

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật