julien-its/emails-queue-s6

Symfony Easy Emails Queue Bundle for symfony 6

安装: 84

依赖: 0

建议者: 0

安全: 0

星标: 0

关注者: 2

分支: 1

开放问题: 0

类型:symfony-bundle

2.2.5 2024-08-22 10:51 UTC

README

emails-queue-s65

特性

一个可以用来将你的邮件发送到队列系统的服务。所有你的邮件都会存储在你的数据库中以便保留日志。你可以直接发送邮件或通过队列使用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中存在。在这种情况下,向现有的extra.symfony条目添加“endpoint”键。

现在你可以通过composer安装它了

$ composer require julien-its/emails-queue-s6

说明

一旦安装完成,

使用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中以便个性化

创建emailQueue时的两种可能性

$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