julien-its / emails-queue-s5
symfony 5 的 Symfony Easy Emails Queue Bundle
2.0.2
2023-01-27 09:03 UTC
Requires
- php: >=7.0
- symfony/framework-bundle: >=5.0
- symfony/swiftmailer-bundle: ^3.2
This package is not auto-updated.
Last update: 2024-09-20 17:02:56 UTC
README
emails-queue-s5
功能
您可以使用此服务将您的电子邮件发送到队列系统。所有电子邮件都将存储在您的数据库中以保留它们的日志。直接发送电子邮件或使用 cron 通过队列发送电子邮件。在每次调用处理队列操作时,定义您想要发送多少封电子邮件。
安装
使用 composer 安装
安装前
在您的 composer.json 中添加
"extra": { "symfony": { "endpoint": [ "https://api.github.com/repos/julien-its/symfony-recipes/contents/index.json", "flex://defaults" ] } }
extra.symfony 键可能已经在您的 composer.json 中存在。在这种情况下,将 "endpoint" 键添加到现有的 extra.symfony 条目。
您现在可以通过 composer 安装它
$ composer require julien-its/emails-queue-s5
指令
安装完成后,
调整 /config/packages/emails_queue.yaml 中的参数
emails_queue: mode: '%env(EMAILS_QUEUE_MODE)%' debug_to: '%env(EMAILS_QUEUE_DEBUG_TO)%' debug_cc: '%env(EMAILS_QUEUE_DEBUG_CC)%'
使用 doctrine 在您的数据库中生成新表
$ php bin/console doctrine:migration:diff $ php bin/console doctrine:migration:migrate
创建一个新的电子邮件服务,您将在其中定义所有电子邮件方法。我们只添加了一个联系表单电子邮件的示例
<?php
namespace App\Services;
use \JulienIts\EmailsQueueBundle\Entity\EmailQueue;
class EmailService
{
const DEFAULT_SUBJECT = "My App";
protected $jitsEmailService;
public function __construct(\JulienIts\EmailsQueueBundle\Services\EmailService $jitsEmailService)
{
$this->jitsEmailService = $jitsEmailService;
}
public function contact($message)
{
$config = array(
'template' => 'EmailsQueueBundle:mail:contact.html.twig',
'templateVars' => array('message' => $message),
'emailFrom' => 'from@from.com',
'emailFromName' => 'My app',
'contextName' => 'contact',
'priority' => EmailQueue::HIGH_PRIORITY,
'subject' => self::DEFAULT_SUBJECT.' : Contact',
'emailTo' => 'toemail@to.com',
'emailsBcc' => 'contact@from.com;email2@email.com'
);
$this->jitsEmailService->createNewAndProcess($config);
}
}
注意,您可以将 contact.html 和电子邮件布局复制到自己的 appBundle 中以自定义它们
创建电子邮件队列时的两种可能性
$this->jitsEmailService->createNew($config);
$this->jitsEmailService->createNewAndProcess($config);
createNewAndProcess 将直接处理电子邮件队列并将其发送到您的邮件服务。
发送电子邮件
要发送电子邮件,请在控制器中调用您的服务
$message = array( 'name' => 'Julien Gustin', 'phone' => '+320484010203', 'message' => 'gustin.julien@gmail.com' ); $emailService->contact($message);
定义 cron 动作
如果您想要分批发送电子邮件,可以使用以下命令
php bin/console jits:queue:process --limit 30