vsavritsky / mail-bundle
邮件管理器
0.6.3
2023-01-12 10:55 UTC
Requires
- php: >=8.1
- doctrine/doctrine-bundle: ^2
- doctrine/orm: ^2.6
- psr/log: ^2.0||^3.0
- symfony/mailer: v6.2.2
Requires (Dev)
- phpunit/phpunit: ~6|~7|~8|~9
Suggests
- twig/twig: ^1.0||^2.0||^3.0
README
这是一个适用于Symfony 3.4+的Bundle,用于构建使用
特性
- 在发送前将您的邮件保存到您的数据库中
- 使用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], //... ];
现在您必须更新您的数据库以获取两个表(Mail,MailTemplate)
#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