qckanemoto/twigged-swiftmessage-builder
最新稳定版本:2.0.0
Composer 安装命令:
composer require qckanemoto/twigged-swiftmessage-builder
包简介
Twig templated Swift_Message builder service.
README 文档
README
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
- PHP 5.3+
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
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
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 setmbstring.languagein php.ini or callmb_language('your_language')on ahead.注意
この機能は内部的に
mb_convert_encoding()に'auto'を渡して実行します。なので、php.ini でmbstring.languageを設定するか、mb_language('Japanese')を事前に実行しておく必要があります。
Embed some image files into message body
You can embed images into message body as below:
{# email.html.twig #} {% block body %} <img src="{{ embed_image(image_path) }}"/> {% 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!
See also functional tests to understand basic usages.
统计信息
- 总下载量: 110
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 1
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2014-11-15