vsavritsky/mail-bundle

邮件管理器

安装: 324

依赖者: 0

建议者: 0

安全: 0

星星: 0

关注者: 1

分支: 4

类型:symfony-bundle

0.6.3 2023-01-12 10:55 UTC

This package is auto-updated.

Last update: 2024-09-12 14:35:00 UTC


README

Build Status codecov Codacy Badge

这是一个适用于Symfony 3.4+的Bundle,用于构建使用和自定义的HTML邮件。您可以在发送之前将邮件保存到您的数据库中,除了您的数据库提供商外,最终使用您自己的提供商发送邮件,默认为SwiftMailer

特性

  • 在发送前将您的邮件保存到您的数据库中
  • 使用twig构建您自己的邮件模板
  • 将您的邮件模板保存到您的数据库中
  • 轻松与您的数据库提供商集成
  • 轻松与您的邮件提供商集成
  • 使用symfony命令一次性发送所有邮件

安装

使用composer,需要

composer require extellient/mail-bundle

然后在您的kernel中启用它

// app/AppKernel.php Symfony 3.4+
public function registerBundles()
{
    $bundles = array(
        //...
        new Extellient\MailBundle\MailBundle(),
        //...
    );
// config/bundles.php Symfony 4+

return [
    //...
    Extellient\MailBundle\MailBundle::class => ['all' => true],
    //...
];

现在您必须更新您的数据库以获取两个表(MailMailTemplate

#Symfony 3.4+
php bin/console doctrine:migrations:update

配置

您需要配置默认邮件。

# app/config/services.yml Symfony 3.4+
# config/package/extellient_mail.yaml Symfony 4+
extellient_mail:
    mail_address_from: '<your-email@address.com>'
    mail_alias_from: '<your-email@address.com>'
    mail_reply_to: '<your-email@address.com>'

默认配置使用Doctrine桥接数据库,Twig进行模板化,SwiftMailer发送邮件。如果您想使用默认配置,则不需要创建此文件

# app/config/extelient_mail.yml Symfony 3.4+
# config/package/extelient_mail.yml Symfony 4+
extellient_mail:
    mail_service_provider: 'Extellient\MailBundle\Provider\Mail\DoctrineMailProvider' #The database provider to get mails
    mail_template_service_provider: 'Extellient\MailBundle\Provider\Template\DoctrineMailTemplateProvider' # The database provider to get templates
    mail_sender_service_provider: 'Extellient\MailBundle\Sender\SwiftMailSender' #The Mail provider that will be use to send mails

用法

将第一个模板插入到您的数据库中

INSERT INTO `mail_template` (`id`, `created_at`, `updated_at`, `mail_subject`, `mail_body`, `code`) VALUES (1, '2018-03-14 09:44:28', '2018-04-20 15:11:38', 'Reset your password', '<p>Hello,<br /><br />{{link_password_reset}}', 'reset_password'),
// src/controller/HomeController.php

<?php


namespace App\Controller;


use Extellient\MailBundle\Services\MailTemplating;
use Extellient\MailBundle\Services\Mailer;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Routing\Annotation\Route;

/**
 * Class HomeController
 * @package App\Controller
 */
class HomeController extends Controller
{
    /**
     * Create your mail from a template
     * @Route("/", name="home")
     * @param MailTemplating $mailTemplating
     */
    public function indexAction(MailTemplating $mailTemplating)
    {
        $mail = $mailTemplating->createEmail('your_template', 'your-email@your-email.com', [
            'variable_twig' => 'test'
        ]);
        $mailTemplating->getMailService()->save($mail);
    }

    /**
     * Create your mail without a template
     * @Route("/mail", name="home")
     * @param Mailer $mailer
     */
    public function mailAction(Mailer $mailer)
    {
        $mail = $mailer->createEmail('subject', 'body', 'your-email@your-email.com');
        $mailer->save($mail);
    }

}

之后访问此页面,检查您的表Mail中的数据,您应该在其中看到第一条记录

发送所有邮件

此命令将发送您表Mail中的所有邮件,其中sent_date = null

php bin/console extellient:mail:send