gos/mailer-bundle

此包已被废弃且不再维护。没有建议的替代包。

Mailer Bundle,简单的电子邮件模板及可重用

dev-master / 1.0.x-dev 2014-10-01 15:40 UTC

This package is auto-updated.

Last update: 2020-08-17 22:12:32 UTC


README

#Gos Mailer Bundle#

Build Status Scrutinizer Code Quality Code Coverage

此项目目前处于开发中,请小心使用。

提供了一种简单的方法在大型项目中构建邮件系统。易于维护,具有专用类,构建邮件就像构建表单一样。如果你使用Symfony/Form,那么对于你来说非常简单,MailBuilder具有相同的API :)

安装

你需要composer来安装依赖。

{
    "require": {
        "gos/mailer-bundle": "{last stable version}"
    }
}

然后在项目的根目录下运行命令composer update

AppKernel.php中添加此行

$bundles = array(

	//Other bundles
    new Gos\Bundle\MailerBundle\GosMailerBundle(),
);

使用

第一步,我们将创建一个电子邮件,每个电子邮件都有自己的类,如下所示

<?php
//Acme/DemoBundle/Mail/RegistrationMail.php

use Gos\Bundle\MailerBundle\Manager\Transport\Mail;
use Gos\Bundle\MailerBundle\Manager\Transport\Builder\MailBuilderInterface;
use Acme\DemoBundle\Entity\User;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class RegistrationMail extends Mail
{
    /**
     * @var User
     */
    protected $user;

    /**
     * @var array
     */
    protected $applicationInfo;

    /**
     * Dependency related to our email.
     * @param $applicationInfo
     */
    public function __construct($applicationInfo)
    {
        $this->applicationInfo = $applicationInfo;
    }

    /**
     * Dependency related to our email.
     * @param User $user
     */
    public function setUser(User $user)
    {
        $this->user = $user;
    }

    /**
     * @param MailBuilderInterface $builder
     * @param array                $options
     */
    public function buildMail(MailBuilderInterface $builder, Array $options)
    {
    	//Build you header fields
        //$builder->($headerType, $value, Array $options)
        $builder->add('to', $this->user->getEmail());

        $builder->add('subject', 'mail.subject', array(
            'parameters' => array('%username%' => $this->user->getUsername(),
            '%app_name%' => $this->applicationInfo['name']),
            'translation_domain' => 'registration'
        ));

        $builder->add('from', 'no-reply@gos.fr');

        /**
         Using the bundle configuration you can make shortcut to centralized emails
         $builder->add('from', 'no_reply');
        */
    }

    /**
     * Send some parameters to the view
     * @param  array $options
     * @return array
     */
    public function finishView(Array $options)
    {
        return array(
            'user' => $this->user
        );
    }

    /**
     * Define options of you
     * @param OptionsResolverInterface $resolver
     */
    protected function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'template' => 'AcmeDemoBundle:Mail:registration.html.twig'
            //'content_type' => 'text/html' (by default)
        ));
    }

    /**
     * Name of you mail (must be unique)
     * @return string
     */
    public function getName()
    {
        return 'acme_demo_mail_registration';
    }
}

从这里,RegistrationMail 是你电子邮件的抽象表示。现在将其注册为服务。

services:
    acme.demo_bundle.mail.registration:
        class: Acme\DemoBundle\Mail\RegistrationMail
        public: false
        arguments:
            - %project%
        tags:
            - { name: mail }

发送电子邮件

<?php
//Acme/DemoBundle/EventListener/UserSubscriber.php

use Gos\Bundle\MailerBundle\Manager\Transport\MailFactory;
use Gos\Bundle\MailerBundle\Manager\Transport\MailTransportManager;
use Gos\Bundle\ResourceBundle\ClassInterface\ActiveStateInterface;
use Gos\Bundle\UserBundle\Events\UserLifeCycleEvent;
use Gos\Bundle\UserBundle\Events\UserLifeCycleEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;


class UserSubscriber implements EventSubscriberInterface
{
    /**
     * @var \Gos\Bundle\MailerBundle\Manager\Transport\MailTransportManager
     */
    protected $mailTransportManager;

     /**
     * @var \Gos\Bundle\MailerBundle\Manager\Transport\MailFactory
     */
    protected $mailFactory;


    /**
     * Inject our dependencies
     * @param MailTransportManager $mailTransportManager
     * @param MailFactory          $mailFactory
     */
    public function __construct(MailTransportManager $mailTransportManager, MailFactory $mailFactory)
    {
        $this->mailTransportManager = $mailTransportManager;
        $this->mailFactory = $mailFactory;
    }

    /**
     * @return array
     */
    public static function getSubscribedEvents()
    {
        return [
            UserLifeCycleEvents::NEW_USER => array('onNewUser', 0),
        ];
    }

    /**
     * @param UserLifeCycleEvent $event
     */
    public function onNewUser(UserLifeCycleEvent $event)
    {
		$user = $event->getUser();

        //Create our email
        $registrationMail = $this->mailFactory->createNamed('acme_demo_mail_registration');
        $registrationMail->setUser($user);

        /**
        * The factory is not needed in our case because we send only one mail. Factory avoid
        * to inject many dependencies, because you can directly inject acme.demo_bundle.mail.registration
        *
        * public function __construct(RegistrationMail $registrationMail, MailTransportManager $manager){
        *   $registrationMail->setUser($user);
        *  	$manager->send($registrationMail);
        * }
        *
        **/

        //Send it
        $this->mailTransportManager->send($registrationMail);
    }
}

事件

当邮件发送时,我们触发名为 gos.mailer_bundle.mail_send (MailerEvents::MAIL_SEND) 的事件。

事件类

<?php
namespace Gos\Bundle\MailerBundle\Events;

use Symfony\Component\EventDispatcher\Event;

class OnEmailSendEvent extends Event
{
    protected $message;

    public function __construct(\Swift_Message $message)
    {
        $this->message = $message;
    }

    /**
     * @return \Swift_Message
     */
    public function getMessage()
    {
        return $this->message;
    }
}

运行测试

需要PHPUnit 3.5或更高版本以及Mock_Object包。要设置和运行测试,请按照以下步骤操作

  • 转到 fixture 的根目录
  • 运行:composer install --dev
  • 运行:phpunit

许可证

该项目受MIT许可证保护,有关更多信息,请参阅项目内的LICENSE文件。