mpp/message-bundle

Symfony Message Bundle

v3.0.1 2024-05-31 10:30 UTC

README

安装

要安装此包,请运行以下命令

$ composer require mpp/message-bundle

检查以下行 Mpp\MessageBundle\MppMessageBundle::class => ['all' => true], 是否已添加到 config/bundles.php

如何使用

首先,您需要在 config/packages/mpp_message.yaml 中配置消息配置

mpp_message:
    messages:
        message_1:
            from: 'no-reply@test.com'
            subject: 'Message subject 1'
            template_txt: 'email/message_1_template.txt.twig'
            template_html: 'email/message_1_template.html.twig'
            attachments: # This parameter is optional
                -
                    file: 'https://fr.wikipedia.org/wiki/GitHub#/media/Fichier:Octicons-mark-github.svg' #You can use path internal or external with url
                    name: 'github.svg'
                    mime_type: 'image/svg+xml'
        
        message_2:
            from: 'no-reply@test.com'
            subject: 'Message subject 2'
            template_txt: 'email/message_2_template.txt.twig'
            template_html: 'email/message_2_template.html.twig'
        
        #...

然后,您可以使用 MessageProvider 来发送消息,如下所示

//...
use Mpp\MessageBundle\Provider\MessageProvider;

//...
protected MessageProvider $messageProvider;
//...

public function __construct(MessageProvider $messageProvider)
{
    $this->messageProvider = $messageProvider;
}
//...

    // In a function where you want to send message
    $transportOptions = [];
    $contents = [];
    $this->messageProvider->send('message_1', $transportOptions, $contents);

//...

如何测试

config/routes/dev/mpp_message.yaml 中添加此配置

mpp_message:
    resource: "@MppMessageBundle/Controller/*.php"
    type: annotation
    prefix: /test

然后,查看 config/packages/mpp_message.yaml 文件,在 template/email 中创建两个文件 message_1_template.html.twigmessage_1_template.txt.twig,结构如下

Hello {{ fullname|default('John Doe') }} ! =)

现在,您可以使用 GET 方法测试以下 URL(仅在 dev 环境中有效):http://your-url-project/test/message/send/message_1 URL 路径的最后部分 message_1 是要发送的消息标识符。

如果您想使用自定义的 transportOptionscontents 参数进行测试,可以通过查询参数提供它们,如下所示:http://your-url-project/test/message/send/message_1?transportOptions[recipientC][]=recipient@demo.fr&contents[fullname]=Test