c975l/email-bundle

用于发送邮件并在数据库中存储的包


README

EmailBundle 执行以下操作

  • 可选将邮件存储在数据库中
  • 使用 Symfony Mailer 发送邮件
  • 允许具有良好角色的用户查看发送的邮件
  • 定义一个模板,这些模板应被覆盖以完全集成到网站中
  • 允许附加一个或多个文件

EmailBundle 专用网页.

EmailBundle API 文档.

包安装

步骤 1:下载包

使用 Composer 安装库

    composer require c975l/email-bundle

步骤 2:启用包

然后,通过将其添加到项目中的 app/AppKernel.php 文件中注册的包列表来启用包

<?php
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = [
            // ...
            new c975L\EmailBundle\c975LEmailBundle(),
        ];
    }
}

步骤 3:配置包

检查依赖配置

c975LEmailBundle 使用 c975L/ConfigBundle 来管理配置参数。使用路由 "/email/config" 和适当的用户角色来修改它们。

如果您不使用 Messenger 请记住在 config/packages/messenger.yaml 中禁用内容或正确配置。

步骤 4:启用路由

然后,通过将其添加到项目中的 app/config/routing.yml 文件中来启用路由

c975_l_email:
    resource: "@c975LEmailBundle/Controller/"
    type: annotation
    prefix: /
    #Multilingual website use the following
    #prefix: /{_locale}
    #defaults:   { _locale: '%locale%' }
    #requirements:
    #    _locale: en|fr|es

步骤 4:创建 MySql 表

您可以使用 php bin/console make:migration 来创建迁移文件,如 Symfony 的 Doctrine 文档 中所述,或者使用 /Resources/sql/emails.sql 来创建 emailsemails_archives 表。 DROP TABLE 已注释,以避免意外删除。它还将创建存储过程 sp_EmailsArchive() 和事件 e_monthly_archives,以存档超过 90 天的邮件。如果您不想使用此功能,请删除它们。

步骤 5:创建 MySql 表

如果您想使用 Symfony Messenger 来通过 Doctrine 发送消息,请查看以下链接。如果您想使用异步,还可以查看 StackOverflow 上的此答案

使用方法

创建一个 Twig 模板,例如 templates/emails/description.html.twig,并包含以下内容

{# If you want to use the template provided by c975LEmailBundle you have to extend its layout #}
{% extends "@c975LEmail/emails/layout.html.twig" %}

{% block email_content %}
    <p>
        {{ 'label.description'|trans }} : <strong>{{ object.description }}</strong>
    </p>
{# You can include files #}
    {% include 'YOUR_FILE_PATH' %}
{% endblock %}

然后,在您的控制器中添加以下代码来创建、插入到数据库并发送您的邮件

<?php
// src/Controller/AnyController.php

use c975L\EmailBundle\Service\EmailServiceInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

class AnyController extends AbstractController
{
    public function anyFunction(Request $request, EmailServiceInterface $emailService)
    {
        // ...

        //Build your email
        $body = $this->renderView('emails/description.html.twig', array(
            //Data needed for your template
            ));
        $emailData = array(
            'subject' => 'YOUR_SUBJECT',
            'sentFrom' => $emailService->getParameter('c975LEmail.sentFrom'),
            'sentTo' => 'contact@example.com',
            'sentCc' => 'contact@example.com', //optional
            'sentBcc' => 'contact@example.com', //optional
            'replyTo' => 'contact@example.com', //optional
            'body' => $body,
            'attach' => array(
                array($filePath, $filename, $contentType),
            ), //optional
            'ip' => $request->getClientIp(), //optional
            );

        //Sends email
        $emailSent = $emailService->send($emailData, [saveDatabase ? true|false(default)]);

        //You can test if email has been sent
        if ($emailSent) {
            //Do what you need...
        } else {
            //Do what you need...
        }

        // ...
    }
}

电子邮件消息模板

如果您想覆盖/禁用 fullLayout.html.twig 模板中定义的块,创建您的 templates/bundles/c975LEmailBundle/emails/layout.html.twig 并使用以下代码

{% extends "@c975LEmail/emails/fullLayout.html.twig" %}

{# Overide a block #}
{% block noSpam %}
    {# You can also use {{ parent() }} #}
    {# YOUR_OWN_TEXT #}
{% endblock %}

{# Disable a block #}
{% block logo %}
{% endblock %}

查看 templates/emails/fullLayout.html.twig 以了解所有可用块。

页脚模板

您应该在 templates/emails/footer.html.twig 中覆盖模板,并在您的 templates/bundles/c975LEmailBundle/emails/footer.html.twig 中指示所有需要在发送的邮件底部显示的数据。

仪表板使用和发送显示消息

您可以通过仪表板查看发送的电子邮件。

为此,只需在您的应用程序中创建以下结构 templates/bundles/c975LEmailBundle/,然后在其中复制文件 layout.html.twig 以覆盖现有的Bundle文件,然后应用您需要的更改。

layout.html.twig 中,它主要将扩展您的布局并定义特定变量,例如

{% extends 'layout.html.twig' %}
{# or extends 'emails/layout.html.twig' #}

{# Defines specific variables #}
{% set title = 'Email (' ~ title ~ ')' %}

{% block content %}
    {% block email_content %}
    {% endblock %}
{% endblock %}

如果这个项目 帮助您缩短开发时间,您可以通过顶部:“赞助”按钮赞助我:)