# Script to replace the mautic:emails:send cronjob

**URL:** <https://forum.mautic.org/t/script-to-replace-the-mauticsend-cronjob/29624>\
**Category:** General Discussion\
**Created:** [October 11, 2023, 6:02pm UTC](https://forum.mautic.org/t/script-to-replace-the-mauticsend-cronjob/29624 "2023-10-11T18:02:15Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![growthlab](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/growthlab/32/2029_2.png) [@growthlab](https://forum.mautic.org/u/growthlab)\
**Post date:** [October 11, 2023, 6:02pm UTC](https://forum.mautic.org/t/script-to-replace-the-mauticsend-cronjob/29624/1 "2023-10-11T18:02:15Z")

</div>

I’ve always had issues with my mautic:emails:send cronjob. If I put the send limit too low my emails don’t go out fast enough. If I put the limit too high I exceed the Amazon SES rate limit of 14 emails per second.

So I made this script that does the best of both worlds… you can run it every 30sec-1min and it will check if there are mails to send. If no mails need to be sent it just exits without running mautic. If there are mails that need to go out, it will send EXACTLY 14 emails, wait 1 sec, send 14 emails, and repeat in a loop until all mails are sent.

So this means you can just set and forget a single cronjob every minute to run this script and it will always send emails and maintain the sending within your rate limit. Amazon SES is 14, but you can configure the script to use whatever number you prefer.

For large mail jobs (i.e. 100,000 emails) you can run it from the terminal in interactive mode and it will provide visual output and an ETA of when the emails will be done sending.

Enjoy and feel free to send me any bugs or suggestions for improvement.

> **[GitHub - mrlazycoder/mautic-smartsend: An intelligent script to send Mautic...](https://github.com/mrlazycoder/mautic-smartsend)**
>
> An intelligent script to send Mautic emails in a controlled manner, providing real-time feedback and ensuring efficient email delivery. - GitHub - mrlazycoder/mautic-smartsend: An intelligent scrip...

---

<div class="post-metadata">

**Author:** ![joeyk](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/joeyk/32/11164_2.png) [@joeyk](https://forum.mautic.org/u/joeyk)\
**Post date:** [October 11, 2023, 7:35pm UTC](https://forum.mautic.org/t/script-to-replace-the-mauticsend-cronjob/29624/2 "2023-10-11T19:35:55Z")

</div>

Hi, thank you, this is pretty awesome.  
Special thanks for sharing the whole script.  
I wanted to ask - does this script support multi-thread?  
A small server can send 2-3 emails / sec and by running 3-4 threads it can be optimized. Does this sxript also does it? Thx!

---

<div class="post-metadata">

**Author:** ![growthlab](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/growthlab/32/2029_2.png) [@growthlab](https://forum.mautic.org/u/growthlab)\
**Post date:** [October 11, 2023, 8:16pm UTC](https://forum.mautic.org/t/script-to-replace-the-mauticsend-cronjob/29624/3 "2023-10-11T20:16:49Z")

</div>

It does not, but it could be. My understanding is that mautic sets a lock when sending emails so I don’t know if it is good practice to override this and make mautic send concurrently. It _COULD_ be done, but not sure if it _SHOULD_ be done.

Also the mechanism to avoid the rate limit is not intelligent right now “sleep 1”. If I was using multithreading, I’d probably need to base everything on timestamps. It would need to be reworked.

---

<div class="post-metadata">

**Author:** ![joeyk](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/joeyk/32/11164_2.png) [@joeyk](https://forum.mautic.org/u/joeyk)\
**Post date:** [October 11, 2023, 9:46pm UTC](https://forum.mautic.org/t/script-to-replace-the-mauticsend-cronjob/29624/4 "2023-10-11T21:46:03Z")

</div>

Thank you for the tip!  
Could you plz tell me with what setup do you have 14 emails / sec? cpu/ core

---

<div class="post-metadata">

**Author:** ![growthlab](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/growthlab/32/2029_2.png) [@growthlab](https://forum.mautic.org/u/growthlab)\
**Post date:** [October 12, 2023, 1:53pm UTC](https://forum.mautic.org/t/script-to-replace-the-mauticsend-cronjob/29624/5 "2023-10-12T13:53:58Z")

</div>

I’m running this on a VM inside Proxmox currently configured with 32 cores. Host has 2 x Xeon E5-2698 v3. I could probably get away with less.

---

<div class="post-metadata">

**Author:** ![joeyk](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/joeyk/32/11164_2.png) [@joeyk](https://forum.mautic.org/u/joeyk)\
**Post date:** [October 12, 2023, 2:59pm UTC](https://forum.mautic.org/t/script-to-replace-the-mauticsend-cronjob/29624/6 "2023-10-12T14:59:14Z")

</div>

Okay. So this is a big gun. Your problem is to try to send slow 🙂  
Again: thank you so much for the script! I’ll take a look, maybe multi thread is also an option here.

---

<div class="post-metadata">

**Author:** ![sfahrenkrog1](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/sfahrenkrog1/32/4583_2.png) [@sfahrenkrog1](https://forum.mautic.org/u/sfahrenkrog1)\
**Post date:** [October 13, 2023, 8:39am UTC](https://forum.mautic.org/t/script-to-replace-the-mauticsend-cronjob/29624/7 "2023-10-13T08:39:02Z")

</div>

Hey Joey

There was a merged pull request regarding this multi threading sending:

> <https://github.com/mautic/mautic/pull/9089>
>
> \<!--
> Any PR related to Mautic 2 issues is not relavant anymore, please consider… upgrading your code to the Mautic 3 series (staging/3.0 branch).
> \--\>
> | Q | A
> | -------------------------------------- | ---
> | Branch? | staging
> | Bug fix? | no
> | New feature? | yes
> | Deprecations? | no
> | BC breaks? | no
> | Automated tests included? | yes
> | Related user documentation PR URL | mautic/mautic-documentation#... 
> | Related developer documentation PR URL | mautic/developer-documentation#... 
> | Issue(s) addressed | Fixes #... 
> 
> \<!--
> Additionally (see https://contribute.mautic.org/contributing-to-mautic/developer/code/pull-requests#step-5-work-on-your-pull-request):
> - Always add tests and ensure they pass.
> - Bug fixes must be submitted against the lowest maintained branch where they apply
> (lowest branches are regularly merged to upper ones so they get the fixes too.)
> - Features and deprecations must be submitted against the staging branch.
> \--\>
> 
> \<!--
> Please write a short README for your feature/bugfix. This will help people understand your PR and what it aims to do.
> \--\>
> \#### Description:
> This PR is fixes the failed tests for PR https://github.com/mautic/mautic/pull/8617
> 
> \> Add lock-name option on 'mautic:emails:send' command. This option allow to have serval email send process running in same time with different lock name.
> \> 
> \> A swiftmailler bug is fixed in this PR swiftmailer/swiftmailer#1270 to avoid send the same email twice. This modification should come with next M3 composer update.
> \> 
> 
> \<!--
> If you are fixing a bug and if there is no linked issue already, please provide steps to reproduce the issue here.
> \--\>
> 
> \#### Steps to test this PR:
> 1. Load up \[this PR\](https://mautibox.com)
> 2. 
> 
> \<!--
> If you have any deprecations, list them here along with the new alternative.
> If you have any backwards compatibility breaks, list them here.
> \--\>

Relevant part is this info from kuzmany:

 ![Bildschirmfoto 2023-10-13 um 10.33.21](https://us1.discourse-cdn.com/flex020/uploads/mautic/original/2X/5/563f286ac245c1af2640bdbfdfb3ff46d4adaa35.png)

The thing is I don’t know if this ever happened in mautic swiftmailer:

> Also need fix in swiftmailer [Add flock lock on message during queue flushing by Dcoutelle · Pull Request #1270 · swiftmailer/swiftmailer · GitHub](https://github.com/swiftmailer/swiftmailer/pull/1270/files)

So maybe a patched is needed.

This would be the patch:  
[https://patch-diff.githubusercontent.com/raw/swiftmailer/swiftmailer/pull/1270.diff](https://patch-diff.githubusercontent.com/raw/swiftmailer/swiftmailer/pull/1270.diff)

So I think this patch is still needed (Because I apply in mautic 4.4.10 and it was applied cleanly)

To apply just do in the mautic root:

```auto
cd vendor/swiftmailer/swiftmailer
curl -L https://patch-diff.githubusercontent.com/raw/swiftmailer/swiftmailer/pull/1270.diff | git apply -v

```

Should output something like:

> Checking patch lib/classes/Swift/FileSpool.php…
> 
> Applied patch lib/classes/Swift/FileSpool.php cleanly.

---

<div class="post-metadata">

**Author:** ![joeyk](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/joeyk/32/11164_2.png) [@joeyk](https://forum.mautic.org/u/joeyk)\
**Post date:** [October 13, 2023, 8:59am UTC](https://forum.mautic.org/t/script-to-replace-the-mauticsend-cronjob/29624/8 "2023-10-13T08:59:12Z")

</div>

Thank you, Zdenyo was kind enough to send this to me before - and the key is really installing the swiftmailer patch you embedded there.

We use this in production, and have to say it works perfectly. I was sending so fast, got warned by Amazon SES after the first try.

I think it is also important not to run the related cronjob as a root, because then file locks are disregarded 😃 I have seen some bad setups where this was an issue.

---

<div class="post-metadata">

**Author:** ![carlosatfongo](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/carlosatfongo/32/10223_2.png) [@carlosatfongo](https://forum.mautic.org/u/carlosatfongo)\
**Post date:** [October 27, 2023, 7:11pm UTC](https://forum.mautic.org/t/script-to-replace-the-mauticsend-cronjob/29624/10 "2023-10-27T19:11:06Z")

</div>

We added this patch to our system, tried running a basic .sh file with

```auto
sudo php /srv/mautic/bin/console mautic:emails:send & sudo php /srv/mautic/bin/console mautic:emails:send --lock-name=process2 & sudo php /srv/mautic/bin/console mautic:emails:send --lock-name=process3

```

Based off of rudimentary telemtry (atop command) It looks like it ran all 3 processes at the same time, but on the same thread (logical thread: 0). Is it not supposed to use 3 separate CPU threads?

EDIT - October 30th: Managed to fix this by adding some lock\_mode parameters and adding quotations to the lock names. New script:

```auto
sudo php /srv/mautic/bin/console mautic:emails:send --lock_mode=flock --lock-name="1" & sleep 2 & sudo php /srv/mautic/bin/console mautic:emails:send --lock_mode=flock --lock-name="2" & sleep 2 & sudo php /srv/mautic/bin/console mautic:emails:send --lock_mode=flock --lock-name="3"

```

---

<div class="post-metadata">

**Author:** ![carlosatfongo](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/carlosatfongo/32/10223_2.png) [@carlosatfongo](https://forum.mautic.org/u/carlosatfongo)\
**Post date:** [October 27, 2023, 8:20pm UTC](https://forum.mautic.org/t/script-to-replace-the-mauticsend-cronjob/29624/11 "2023-10-27T20:20:43Z")

</div>

Tried running your 1.0.1 script using:

```auto
sudo .mautic_smartsend.sh

```

and here’s the **output** , i replaced my user with something generic.

```auto
myuser@something:/srv$ sudo ./mautic_smartsend.sh
Detected PHP Version: 8.0.30
Detected Mautic Version: 4.4.10
Error: Mautic command failed.=============
Mautic provided the following error message:             
Sending in this cycle: 16                        
 [2023-10-27 20:12:19] Processing default mailer spool... 
 1 emails sentsh time: Calc...

```

This…seems like a **Task failed successfully** situation? I still received the email that i had queued up lol
