PHPackages                             thecodingmachine/swift-twig-mail-template - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Templating &amp; Views](/categories/templating)
4. /
5. thecodingmachine/swift-twig-mail-template

ActiveLibrary[Templating &amp; Views](/categories/templating)

thecodingmachine/swift-twig-mail-template
=========================================

This package contains a implementation of the SwiftMailTemplate interface which use twig to render the mail message.

v5.1.0(7y ago)331.6k10[1 issues](https://github.com/thecodingmachine/swift-twig-mail-template/issues)1MITPHPPHP &gt;=7.0.0

Since Sep 13Pushed 7y ago7 watchersCompare

[ Source](https://github.com/thecodingmachine/swift-twig-mail-template)[ Packagist](https://packagist.org/packages/thecodingmachine/swift-twig-mail-template)[ Docs](https://github.com/thecodingmachine/swift-twig-mail-template)[ RSS](/packages/thecodingmachine-swift-twig-mail-template/feed)WikiDiscussions 5.0 Synced 1mo ago

READMEChangelog (5)Dependencies (3)Versions (6)Used By (1)

[![Build Status](https://camo.githubusercontent.com/5b9bce87eb62e122a660ff1e1a093f9a0ed805a0973474e44562f1ca4dd1b847/68747470733a2f2f7472617669732d63692e6f72672f746865636f64696e676d616368696e652f73776966742d747769672d6d61696c2d74656d706c6174652e7376673f6272616e63683d352e30)](https://travis-ci.org/thecodingmachine/swift-twig-mail-template)[![Coverage Status](https://camo.githubusercontent.com/0c36c25d03e87b8644a30e545f0312b5c6cbccad8da49d34937ac6abda957d80/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f746865636f64696e676d616368696e652f73776966742d747769672d6d61696c2d74656d706c6174652f62616467652e7376673f6272616e63683d352e3026736572766963653d676974687562)](https://coveralls.io/github/thecodingmachine/swift-twig-mail-template?branch=5.0)[![Scrutinizer Quality Score](https://camo.githubusercontent.com/90407567af0520ad97851a255b10c577f0bb990c15f35b8e5140c831434bef69/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f746865636f64696e676d616368696e652f73776966742d747769672d6d61696c2d74656d706c6174652f6261646765732f7175616c6974792d73636f72652e706e673f623d352e30)](https://scrutinizer-ci.com/g/thecodingmachine/swift-twig-mail-template?b=5.0)[![Code Coverage](https://camo.githubusercontent.com/2a27733b9f1fdecda1e2f64d5a2ca7fe660e9ef4491b9ce1e821c884c0b1b4de/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f746865636f64696e676d616368696e652f73776966742d747769672d6d61696c2d74656d706c6174652f6261646765732f636f7665726167652e706e673f6272616e63683d352e30)](https://scrutinizer-ci.com/g/thecodingmachine/swift-twig-mail-template?b=5.0)[![Open Source Love](https://camo.githubusercontent.com/f51de32d275906916ae11f73fb488c3d6c2710e9c36da34ecef00358129bce52/68747470733a2f2f6261646765732e66726170736f66742e636f6d2f6f732f76312f6f70656e2d736f757263652e7376673f763d313032)](https://github.com/ellerbrock/open-source-badge/)[![Open Source Love](https://camo.githubusercontent.com/2d4eac62d5f5830a5309100b88e0ecccb171aee1fbcd794149f4a580b0010c56/68747470733a2f2f6261646765732e66726170736f66742e636f6d2f6f732f6d69742f6d69742e7376673f763d313032)](https://github.com/ellerbrock/open-source-badge/)

Swift Twig Mail Generator
=========================

[](#swift-twig-mail-generator)

This package takes a Twig template and generates a Swift mail from this template.

Installation
------------

[](#installation)

```
composer require thecodingmachine/swift-twig-mail-template

```

Once installed, you can start creating an instance of the `SwiftTwigMailTemplate` class.

The `SwiftTwigMailTemplate` represents a mail template that can generate Swift mails.

Example
-------

[](#example)

Because we consider that an example is better than everything else...

Start by creating your mail template. Your template should have two blocks:

```
{% block subject %}
    Your suject
{% endblock %}

{% block body_html %}
    Body with HTML.
{% endblock %}
```

If you want you can add another block containing your text body. This block is optional since we can get your the text body directly from the html one.

```
{% block body_text %}
    Body without HTML.
{% endblock %}
```

Also, you can create a mail with a text body only (and no HTML):

```
{% block subject %}
    Your suject
{% endblock %}

{% block body_text %}
    Body with text only.
{% endblock %}
```

Now, let's create a `SwiftTwigMailTemplate` instance. This object will generate a `SwiftMail` from the twig template.

```
// We assume that $twigEnvironment is a valid TwigEnvironment instance
$twigSwiftMailTemplate =  new SwiftTwigMailTemplate($twigEnvironment, 'path/to/template.twig');

// The renderMail method generates a Swift mail object.
$swiftMail = $twigSwiftMailTemplate->renderMail(['paramKey' => paramValue]);

// We fill the swift mail with additional information
$swiftMail->setFrom('sender@example.com');
$swiftMail->setTo('recipient@example.com');

// We assume that $mailer is a valid Swift_Mailer instance
$mailer->send($swiftMail);
```

You can also embed images directly into the email by using the embedImage function. By embedding an image into the email, most mail clients will display the image directly without printing the traditional warning.

Note: embedImage expects one parameter: the full path to the image file.

```
{% block subject %}
    Welcome !
{% endblock %}

{% block body_html %}

    Hello World!
{% endblock %}
```

### Going further

[](#going-further)

The `SwiftTwigMailTemplate` class has been designed with dependency injection in mind. Instances can be easily put in your container for easy reuse. Furthermore, the `SwiftTwigMailTemplate` class shares a lot with the `Swift_Mail` class. You can:

- setting the from address
- setting the from name
- setting the to address
- setting the to name
- setting the Bcc address
- setting the Bcc name
- setting the Cc address
- setting the Cc name
- setting the ReplyTo address
- setting the ReplyTo name
- setting the max line size
- setting the priority
- setting the read receip to
- setting the return path

###  Health Score

35

—

LowBetter than 80% of packages

Maintenance13

Infrequent updates — may be unmaintained

Popularity32

Limited adoption so far

Community23

Small or concentrated contributor base

Maturity62

Established project with proven stability

 Bus Factor1

Top contributor holds 50% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~151 days

Recently: every ~98 days

Total

6

Last Release

2767d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/1104771?v=4)[mouf](/maintainers/mouf)[@Mouf](https://github.com/Mouf)

![](https://avatars.githubusercontent.com/u/1847918?v=4)[TheCodingMachine](/maintainers/thecodingmachine)[@thecodingmachine](https://github.com/thecodingmachine)

![](https://www.gravatar.com/avatar/988fb049e7b031fc14b8ce38b8f5bc3e40dbd6bfb3b5bc7f7061b68c41ba39f1?d=identicon)[xhuberty](/maintainers/xhuberty)

---

Top Contributors

[![moufmouf](https://avatars.githubusercontent.com/u/1290952?v=4)](https://github.com/moufmouf "moufmouf (11 commits)")[![xhuberty](https://avatars.githubusercontent.com/u/8350192?v=4)](https://github.com/xhuberty "xhuberty (6 commits)")[![daetcm](https://avatars.githubusercontent.com/u/39995314?v=4)](https://github.com/daetcm "daetcm (2 commits)")[![wael1802](https://avatars.githubusercontent.com/u/18535079?v=4)](https://github.com/wael1802 "wael1802 (2 commits)")[![alexisprevot](https://avatars.githubusercontent.com/u/11993949?v=4)](https://github.com/alexisprevot "alexisprevot (1 commits)")

---

Tags

twigmailswift

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/thecodingmachine-swift-twig-mail-template/health.svg)

```
[![Health](https://phpackages.com/badges/thecodingmachine-swift-twig-mail-template/health.svg)](https://phpackages.com/packages/thecodingmachine-swift-twig-mail-template)
```

###  Alternatives

[twig/extra-bundle

A Symfony bundle for extra Twig extensions

91292.0M314](/packages/twig-extra-bundle)[twig/intl-extra

A Twig extension for Intl

36663.2M220](/packages/twig-intl-extra)[rcrowe/twigbridge

Adds the power of Twig to Laravel

9105.9M50](/packages/rcrowe-twigbridge)[twig/string-extra

A Twig extension for Symfony String

21946.0M132](/packages/twig-string-extra)[twig/cssinliner-extra

A Twig extension to allow inlining CSS

23018.5M53](/packages/twig-cssinliner-extra)[symfony/ux-twig-component

Twig components for Symfony

21814.8M161](/packages/symfony-ux-twig-component)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
