braune-digital / mail-bundle
邮件包
dev-master
2018-08-02 19:23 UTC
Requires
This package is auto-updated.
Last update: 2024-09-10 23:27:13 UTC
README
这个Symfony2-Bundle允许轻松管理带额外翻译的电子邮件模板。电子邮件在SonataAdmin中列出,并可预览。
安装
为了安装此Bundle,您需要以下内容
- Doctrine ORM(必需) -> 实体持久化
- SonataEasyExtends(必需)
- BrauneDigitalTranslationBaseBundle(必需) -> 翻译
- SonataAdmin(可选) -> 后端管理
只需运行以下命令来安装此bundle
composer require braune-digital/mail-bundle
并在您的AppKernel.php中启用Bundle
public function registerBundles() { $bundles = array( ... new BrauneDigital\TranslationBaseBundle\BrauneDigitalTranslationBaseBundle(), new BrauneDigital\MailBundle\BrauneDigitalMailBundle(), ... );
为了使用此bundle,您必须
扩展Bundle
只需运行
php app/console sonata:easy-extends:generate --dest=src BrauneDigitalMailBundle
并在您的AppKernel.php中同样启用扩展的Bundle
public function registerBundles() { $bundles = array( ... new Application\BrauneDigital\MailBundle\BrauneDigitalMailBundle() ... );
您只需要设置user_class
选项
配置
braune_digital_mail: user_class: Application\Sonata\UserBundle\Entity\User # Path to you used User-Entity base_template_path: "emails" #used for template suggestions in SonataAdmin, defaults to "emails", which would resolve to app/Resources/views/emails #base_template_path: ["emails_password_reset", "emails_registration] #Can be an array of paths as well #base_template_path: ~ #Do not use template suggestions (You would have to enter the path manually)
邮件模板
emails/confirm.html.twig:
{% extends 'emails/layout.html.twig' %} {% block body %} {{ object.template.body|raw }} ---USER_NAME{{ object.object.username }}--- ---CONFIRMATION_LINK{% if object.object.confirmationToken is not empty %}{{ url('fos_user_registration_password_confirm', {token: object.object.confirmationToken}) }}{% else %}{{ url('fos_user_registration_password_confirm', {token: 'na'}) }}{% endif %}--- {% endblock %}
其中---USER_NAME{{ object.object.username }}---
将是一个id为USER_NAME的生成占位符。另外,使用txt.twig
文件将内容附加为纯文本
emails/confirm.txt.twig:
{{ object.template.body|raw|striptags }}
---USERNAME{{ object.object.username }}---
---CONFIRMATION_LINK{% if object.object.confirmationToken is not empty %}{{ url('fos_user_registration_password_confirm', {token: object.object.confirmationToken}) }}{% else %}{{ url('fos_user_registration_password_confirm', {token: 'na'}) }}{% endif %}---
```
Placeholders can then be used in the Template-Description (layout path has to be the same):
```
Dear ###USER_NAME###
Thank you for registering. In order to complete your registration, you need to confirm your email address. To do so, click on the following link:
###CONFIRMATION_LINK###
Best regards
```
##Types of Mails
There are currenty two types of Mails:
* Standard Mail
* User Mail (used for mails regarding a single or two users)
## Send Mails
In order to send mails one has to get the template by entering the layout path and creating a new mail:
```php
$layout = 'emails/confirm.html.twig';
$mailService = $this->get('braunedigital.mail.service.mail');
$template = $mailService->getTemplate($layout);
if ($template) {
$mail = new UserMail();
$mail->setTemplate($template);
$mail->setObject($user);
$mail->setObject2(null);
//send the mail directly
$mailService->handle($mail);
//or store it for later sending
$em->persist($mail);
$em->flush();
}
```
The template will now be rendered and the user is available as `object` in the template.
The locale and recipient adress are being loaded from the first user (`object`).
Or for user independent mails:
```php
$layout = 'emails/static_mail.html.twig';
$mailService = $this->get('braunedigital.mail.service.mail');
$template = $mailService->getTemplate($layout);
if ($template) {
$mail = new Mail();
$mail->setTemplate($template);
$mail->setRecipient($email);
$mail->setLocale('en');
//send the mail directly
$mailService->handle($mail);
//or store it for later sending
$em->persist($mail);
$em->flush();
}
```
## SendMailQueueCommand
In order to send mails that have not been handled immediately, the command braunedigital:mails:send has to be executed!