PHPackages                             ttskch/twigged-swiftmessage-builder - 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. ttskch/twigged-swiftmessage-builder

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

ttskch/twigged-swiftmessage-builder
===================================

Twig templated Swift\_Message builder service.

2.0.0(9y ago)21162MITPHP &gt;=5.3.3

Since Jun 4Compare

[ Source](https://github.com/ttskch/TwiggedSwiftMessageBuilder)[ Packagist](https://packagist.org/packages/ttskch/twigged-swiftmessage-builder)[ Docs](https://github.com/ttskch/TwiggedSwiftMessageBuilder)[ RSS](/packages/ttskch-twigged-swiftmessage-builder/feed)WikiDiscussions Synced today

READMEChangelogDependencies (5)Versions (3)Used By (2)

TwiggedSwiftMessageBuilder
==========================

[](#twiggedswiftmessagebuilder)

[![Build Status](https://camo.githubusercontent.com/fd867d11db5cc2d50cdde90e376ce6ceff247aa095e1882fb607e069ff76512e/68747470733a2f2f7472617669732d63692e6f72672f7474736b63682f5477696767656453776966744d6573736167654275696c6465722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/ttskch/TwiggedSwiftMessageBuilder)[![Latest Stable Version](https://camo.githubusercontent.com/50d45fa1e97eeaa3c1b37cd3212a7eecf469643f1b14f5d91e097cff826f5e5f/68747470733a2f2f706f7365722e707567782e6f72672f7474736b63682f747769676765642d73776966746d6573736167652d6275696c6465722f762f737461626c652e737667)](https://packagist.org/packages/ttskch/twigged-swiftmessage-builder)[![Total Downloads](https://camo.githubusercontent.com/a5c2a815ebdd94142e99a6f50f56ade9383a528e15ab7f1f4b6cc15cb7574d58/68747470733a2f2f706f7365722e707567782e6f72672f7474736b63682f747769676765642d73776966746d6573736167652d6275696c6465722f646f776e6c6f6164732e737667)](https://packagist.org/packages/ttskch/twigged-swiftmessage-builder)

`TwiggedSwiftMessageBuilder` class allows you following things:

- to create Twig templated Swift\_Message
- to create inline styled html email from unstyled html and css strings
- to embed some image files into message body

Requirements
------------

[](#requirements)

- PHP 5.3+

Getting started
---------------

[](#getting-started)

First add this dependency into your `composer.json`:

```
{
    "require": {
        "ttskch/twigged-swiftmessage-builder": "~2.0"
    }
}
```

Then you can send Twig templated email as below:

```
{# email.txt.twig #}

{% block from %}no-reply@example.com{% endblock %}
{% block from_name %}[Example]{% endblock %}
{% block subject %}Welcome to [Example]!{% endblock %}

{% block body %}
Hello [Example] World!
{% endblock %}
```

```
// in your application.

$builder = new \Ttskch\TwiggedSwiftMessageBuilder\TwiggedSwiftMessageBuilder($tiwg);  // $twig is an instance of \Twig_Environment class.

$message = $builder->buildMessage('email.txt.twig');
$message->setTo('hoge@example.com');

$mailer->send($message);    // $mailer is an instance of \Swift_Mailer class.
```

In Twig template you can define many things by using `{% block [field-name] %}{% endblock %}`. These fields can be defined.

- from
- from\_name
- to
- cc
- bcc
- reply\_to
- subject
- body

Use variables in Twig template
------------------------------

[](#use-variables-in-twig-template)

Offcourse you can pass variables and use them in Twig template as below:

```
{# email.txt.twig #}

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

```
// in your application.

$builder = new \Ttskch\TwiggedSwiftMessageBuilder\TwiggedSwiftMessageBuilder($tiwg);

$message = $builder->buildMessage('email.txt.twig', array(
    'site_title' => 'FooBar Service',
));
$message->setTo('hoge@example.com');

$mailer->send($message);
```

Use inline-styled html email
----------------------------

[](#use-inline-styled-html-email)

You can make inline-styled html from unstyled html and css strings. To allow recipients of your html email to receive it with Gmail, you will have to make inline-styled html body.

```
// in your application.

$builder = new \Ttskch\TwiggedSwiftMessageBuilder\TwiggedSwiftMessageBuilder($tiwg);

$message = $builder->buildMessage('email.html.twig');

$style = file_get_contents('/path/to/style.css');

$message = $builder->setInlineStyle($message, $style);

$mailer->send($message);
```

> **Note**
>
> This functionality is using `mb_convert_encoding()` with `'auto'` internally. So if you use this you **must** set `mbstring.language` in php.ini or call `mb_language('your_language')` on ahead.
>
> **注意**
>
> この機能は内部的に `mb_convert_encoding()` に `'auto'` を渡して実行します。なので、php.ini で `mbstring.language` を設定するか、`mb_language('Japanese')` を事前に実行しておく必要があります。

Embed some image files into message body
----------------------------------------

[](#embed-some-image-files-into-message-body)

You can embed images into message body as below:

```
{# email.html.twig #}

{% block body %}

{% endblock %}
```

```
// in your application.

$builder = new \Ttskch\TwiggedSwiftMessageBuilder\TwiggedSwiftMessageBuilder($tiwg);

$message = $builder->buildMessage('email.html.twig', array(
    'image_path' => '/path/to/image/file',
));

// you can get renderable html with base64 encoded images. (In case you want to print preview.)
$renderableHtml = $builder->renderBody($message);

// you must finalize embedding before send message.
$message = $builder->finalizeEmbedding($message);

$mailer->send($message);
```

Enjoy!
------

[](#enjoy)

See also [functional tests](tests/FunctionalTest.php) to understand basic usages.

###  Health Score

28

—

LowBetter than 52% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity13

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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 ~110 days

Total

2

Last Release

3567d ago

Major Versions

1.0.0 → 2.0.02016-09-23

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/4360663?v=4)[Takashi Kanemoto](/maintainers/ttskch)[@ttskch](https://github.com/ttskch)

---

Top Contributors

[![ttskch](https://avatars.githubusercontent.com/u/4360663?v=4)](https://github.com/ttskch "ttskch (1 commits)")

---

Tags

twigSwift\_MessageSwift\_Mailer

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/ttskch-twigged-swiftmessage-builder/health.svg)

```
[![Health](https://phpackages.com/badges/ttskch-twigged-swiftmessage-builder/health.svg)](https://phpackages.com/packages/ttskch-twigged-swiftmessage-builder)
```

###  Alternatives

[twig/cssinliner-extra

A Twig extension to allow inlining CSS

22919.7M81](/packages/twig-cssinliner-extra)[timber/timber

Create WordPress themes with beautiful OOP code and the Twig Template Engine

5.7k3.6M127](/packages/timber-timber)[twig/intl-extra

A Twig extension for Intl

36567.2M320](/packages/twig-intl-extra)[symfony/ux-twig-component

Twig components for Symfony

22017.2M313](/packages/symfony-ux-twig-component)[symfony/ux-live-component

Live components for Symfony

1636.5M115](/packages/symfony-ux-live-component)[twig/inky-extra

A Twig extension for the inky email templating engine

16613.2M70](/packages/twig-inky-extra)

PHPackages © 2026

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