georgringer/templatedmail

Better HTML email templates

Installs: 142

Dependents: 0

Suggesters: 0

Security: 0

Stars: 12

Watchers: 3

Forks: 2

Open Issues: 2

Type:typo3-cms-extension

dev-tests 2019-10-27 23:29 UTC

This package is auto-updated.

Last update: 2024-11-28 11:09:06 UTC


README

This extension is a proof of concept how to improve the templating of mails. The plans are to ship this code with TYPO3 10 and provide the extension for 9x.

Benefits

  • All mails share the same layout which makes it easier to style mails
  • It is faster to create nice mails

Requirements

  • TYPO3 10
  • PHP 7.2

Usage

$templatedEmail = GeneralUtility::makeInstance(TemplatedEmail::class);
$templatedEmail
    ->to('dummy@example.org')
    ->from(new NamedAddress('noreply@example.org', 'TYPO3'))
    ->subject('A mail')
    ->htmlContent('<h1>Hello</h1> an example')
    ->textContent('Hello' . LF . 'an example')
    ->send();

This example will send one mail with the following parts:

Further examples

The examples can also be called by CLI with ./web/bin/typo3 mail:template.

Using a single template file

$templatedEmail = GeneralUtility::makeInstance(TemplatedEmail::class);
$templatedEmail
    ->to('dummy@example.org')
    ->from(new NamedAddress('noreply@example.org', 'TYPO3'))
    ->subject('A mail')
    ->context(['title' => 'My title'])
    ->htmlTemplateFile('EXT:templatedmail/Resources/Private/Templates/Examples/Example.html')
    ->send();

Using custom template paths

$templatedEmail = GeneralUtility::makeInstance(TemplatedEmail::class);
$templatedEmail
    ->to('dummy@example.org')
    ->from(new NamedAddress('noreply@example.org', 'TYPO3'))
    ->subject('A mail')
    ->setTemplateRootPaths(['EXT:dummy/Resources/Private/Templates/'])
    ->setLayoutRootPaths(['EXT:dummy/Resources/Private/Layouts/'])
    ->context(['title' => 'My title'])
    ->htmlByTemplate('Examples/Simple')
    ->textByTemplate('Examples/Simple')
    ->send();

Providing custom translations

$templatedEmail = GeneralUtility::makeInstance(TemplatedEmail::class);
$templatedEmail
    ->to('dummy@example.org')
    ->from(new NamedAddress('noreply@example.org', 'TYPO3'))
    ->subject('A mail')
    ->setLanguage('de')
    ->context(['title' => 'My title'])
    ->htmlTemplateFile('EXT:templatedmail/Resources/Private/Templates/Examples/Example.html')
    ->send();
<f:section name="content">
	<h1>{f:translate(languageKey:defaults.language,key:'LLL:EXT:templatedmail/Resources/Private/Language/dummy.xml:good_morning')}, {title}</h1>
</f:section>

Adding inline images

Use the provided ViewHelper to attach images properly.

<html xmlns:f="http://typo3.org/ns/TYPO3/CMS/Fluid/ViewHelpers"
      xmlns:email="http://typo3.org/ns/GeorgRinger/Templatedmail/ViewHelpers"
      data-namespace-typo3-fluid="true">

<f:layout name="Simple"/>

<f:section name="content">
    <email:mailImage mail="{mailMessage}" src="fileadmin/my-files/CA191.JPG" width="100" />
</f:section>
</html>

Configuration

The paths are configured in the site configuration

templatedMail:
  templateRootPath: EXT:templatedmail/Resources/Private/Templates/
  partialRootPath: EXT:templatedmail/Resources/Private/Partials/
  layoutRootPath: EXT:templatedmail/Resources/Private/Layouts/

If a mail is sent via CLI, the used site can be set with $templatedEmail->setSite($site);