求助:如何在Mautic中实现特定邮件的即时发送
Hey there! Let's work through this Mautic immediate email delivery issue together— I’ve tackled similar scenarios before, so here’s a actionable approach to get those specific emails sent right away instead of waiting for the cron job:
Step 1: Target the Right Event & Identify Emails
First, make sure you’re listening to the correct event and can reliably pick out the emails that need immediate delivery. Mautic’s mautic.email_on_send event fires right before emails are queued, so this is your sweet spot. Add a unique identifier to your target emails (like a custom field, specific subject keyword, or tagged recipient list) so your listener can spot them.
Example listener logic to catch the event:
public function onEmailSend(EmailSendEvent $event) { $email = $event->getEmail(); $recipients = $event->getRecipients(); // Use your own condition here—e.g., check for a custom field or subject keyword if ($email->getCustomFieldValue('send_immediately') === true) { // Stop the default queue logic from running $event->stopPropagation(); // Trigger immediate send $this->sendEmailInstantly($email, $recipients); } }
Step 2: Build the Immediate Send Logic
Instead of letting Mautic queue the email, reuse its core mailer services to send the message directly. Inject Mautic’s mailer helper and transport into your listener, then call the send method bypassing the queue:
use Mautic\EmailBundle\Helper\MailerHelperInterface; use Mautic\EmailBundle\Model\TransportInterface; use Psr\Log\LoggerInterface; class ImmediateEmailListener { private $mailerHelper; private $transport; private $logger; public function __construct( MailerHelperInterface $mailerHelper, TransportInterface $transport, LoggerInterface $logger ) { $this->mailerHelper = $mailerHelper; $this->transport = $transport; $this->logger = $logger; } private function sendEmailInstantly(Email $email, array $recipients) { foreach ($recipients as $recipient) { try { // Create the email message using Mautic's helper $message = $this->mailerHelper->createMessage($email, $recipient); // Send immediately without queuing $this->transport->send($message); } catch (\Exception $e) { // Log errors for debugging $this->logger->error("Immediate email failed for {$recipient['email']}: " . $e->getMessage()); } } } }
Step 3: Register the Listener with High Priority
Make sure your listener runs before Mautic’s default queue handler by setting a high priority in your service configuration (e.g., services.yaml):
services: App\EventListener\ImmediateEmailListener: arguments: - '@mautic.helper.mailer' - '@mautic.email.transport' - '@logger' tags: - { name: kernel.event_listener, event: mautic.email_on_send, method: onEmailSend, priority: 100 }
Step 4: Test & Troubleshoot
- Add your identifier to a test email (e.g., set the
send_immediatelycustom field to true) and trigger it. - Check Mautic’s logs to confirm the listener fired, the condition passed, and no errors occurred during sending.
- If it’s not working, verify:
- Your service is properly registered (clear cache with
bin/console cache:clearif needed) - The priority is high enough to beat the default queue listener
- Your condition correctly identifies the target emails
- Your service is properly registered (clear cache with
内容的提问来源于stack exchange,提问作者Henrique Rodrigues

