mnavarrocarter / mailman-bundle
此包已被放弃,不再维护。未建议替代包。
SwiftMailer消息的消息管理/存储
1.0.1
2018-04-03 18:40 UTC
Requires
- php: >=7.0
- symfony/framework-bundle: ^4.0
- symfony/swiftmailer-bundle: ^3.2
README
SwiftMailer的邮件消息管理器
安装
composer require mnavarrocarter/mailman-bundle
然后在容器中注册该包
// app/AppKernel.php
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array
// ...
new MNC\MailmanBundle\MNCMailmanBundle(),
);
// ...
}
// ...
}
然后,在composer完成加载之前,将配置包含在您的config.yml中
// app/config/config.yml
//...
mnc_mailman:
from_name: 'Awesome App'
from_email: 'no-reply@awesomeapp.com'
使用方法
此包提供了一个服务,可以存储消息定义,因此您可以从服务本身获取它们。
有两种注册消息的方法。您可以通过获取mnc_mailman.message_storage服务或注入它来完成。然而,这种方法不建议,并且不会给您带来集中的优势。
更好的方法是创建一个名为,例如,AuthMessageRegistrar
的类,使其扩展AbstractRegistrar
,然后使用mailman.registrar
标签将其作为服务注册。
该类必须实现register方法
// src/AppBundle/MessageRegistrar/AuthMessages.php
<?php
namespace Option\MediaBridgeBundle\MessageRegistrar;
use MNC\MailmanBundle\Registrar\AbstractRegistrar;
use MNC\MailmanBundle\Storage\MessageStorageInterface;
/**
* Class AuthMessages
*/
class AuthMessages extends AbstractRegistrar
{
public function register(MessageStorageInterface $storage)
{
// To register a message, simply pass it the twig template and
the subject.
$storage->store('auth.registered', [
'template' => 'AppBundle:mail:auth:registered.html.twig',
'subject' => 'Welcome to our awesome site!',
]);
// You can also pass other stuff
$storage->store('auth.on_password_change', [
'template' => 'AppBundle:mail:auth:password_change.html.twig',
'subject' => 'Your password has been changed',
'data' => ['global' => 'available data'],
'headers' => ['somecustom' => 'Mail header']
]);
// You can also override the default from address and name for a specific
// message
$storage->store('auth.on_account_delete', [
'template' => 'AppBundle:mail:auth:delete_accout.html.twig',
'subject' => 'Your account has been deleted',
'fromName' => 'Your former app',
'formEmail' => 'you-can-reply-now@formerapp.com'
]);
}
}
然后,注册该服务。
// app/config/services.yml
services:
app.auth_registrar:
class: AppBundle\MessageRegistrar\AuthMessages
tags:
- { name: mailman.registrar }
所有电子邮件消息都将以\Swift_Message实例的形式可用。
// src/Controller/SecurityController.php
class SecurityController extends Controller
{
// ...
public function sendRegisteredEmail(User $user)
{
/** @var \Swift_Message $message */
$message = $this->get('mnc_mailman.message_storage')->fetch('auth.registered', [
// An array of data to pass to the twig template
'user' => $user
]);
$message->setTo($user->getEmail(), $user->getName());
$this->mailer->send($message);
}
}