# Failed email status only after three attempts

**URL:** <https://forum.mautic.org/t/failed-email-status-only-after-three-attempts/23522>\
**Category:** General Discussion\
**Created:** [April 3, 2022, 9:49am UTC](https://forum.mautic.org/t/failed-email-status-only-after-three-attempts/23522 "2022-04-03T09:49:13Z")\
**Posts on this page:** 2\
**Page:** 2

<div class="post-metadata">

**Author:** ![m.abumusa](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/m.abumusa/32/741_2.png) [@m.abumusa](https://forum.mautic.org/u/m.abumusa)\
**Post date:** [April 23, 2022, 9:02am UTC](https://forum.mautic.org/t/failed-email-status-only-after-three-attempts/23522/21 "2022-04-23T09:02:42Z")

</div>

Thanks, @silavapi for tagging me on this topic.

I will describe how things work and what kind of changes we bring to M5, I work a lot on the email bundle and I can support on this issue.

The current implementation of M4, has two ways to handle email sending:

1. **Immediate** : where the calls are happening to the transport that you want to send through directly, and the errors are handled by that transport. For example, if you use SendGrid API in the immediate mode, if the API authentication fails, the send will not be counted, and it will not be retried, nor the contact will be marked as DNC.  
[mautic/SendGridWrapper.php at 9cb69551fbff6046a1e1476aa23d2219e6e41c36 · mautic/mautic · GitHub](https://github.com/mautic/mautic/blob/9cb69551fbff6046a1e1476aa23d2219e6e41c36/app/bundles/EmailBundle/Swiftmailer/SendGrid/SendGridWrapper.php#L29)

This sending mode has advantages of handling large batch sizes in a single API call, and direct API calls to the MTA.

It is disadvantage,that it consume lots of memory from PHP process, it timeouts, and may result in sending duplicates.

**The queue mode** : it store the emails as files in the `var/spool` folder, each email is saved as a file and processes each file in a single run. **This mode is the source of all the issues**

**How it works?**

When you select the queue mode, **the queue transport** is selected which basically takes the email message and converts it into a file that is stored on the disk, regardless of the status of the MAT. therefore Mautic marks all the emails as sent when you use the queue mode because it set the files on disk for the cron job to process them later. **It fails in one case and emails are not marked as sent** when the disk is full, has writing permissions issues, etc.

> <https://github.com/mautic/mautic/blob/9cb69551fbff6046a1e1476aa23d2219e6e41c36/app/bundles/EmailBundle/Swiftmailer/Transport/SpoolTransport.php>

> <https://github.com/mautic/mautic/blob/9cb69551fbff6046a1e1476aa23d2219e6e41c36/app/bundles/EmailBundle/Swiftmailer/Spool/DelegatingSpool.php>

The issue of DNC, happens here

> <https://github.com/mautic/mautic/blob/9cb69551fbff6046a1e1476aa23d2219e6e41c36/app/bundles/EmailBundle/Command/ProcessEmailQueueCommand.php>

This command connects to the real transport (MTA), which tries to send, so if the API request fails for some reason like authentication error, rate limit issues, etc. the command will mark the file as `.tryagain` and retry up to 3 times before it marks the contact as DNC, even if the email was not sent.

Of course this implementation is not logical and DNCs should happen using webhooks and callbacks by MTA.

The good news is that we will be moving away form `swiftmailer` to a new mailing system that uses `symfony/mailer/` and `symfony/messeneger`. you can see the PR here

> <https://github.com/mautic/mautic/pull/10742>
>
> \<!-- ## Which branch should I use for my PR?
> 
> Assuming that:
> 
> a = current ma…jor release
> b = current minor release
> c = future major release
> 
> \* a.x for any features and enhancements (e.g. 4.x)
> \* a.b for any bug fixes (e.g. 4.0, 4.1, 4.2)
> \* c.x for any features, enhancements or bug fixes with backward compatibility breaking changes (e.g. 5.x) --\>
> 
> | Q | A
> | -------------------------------------- | ---
> | Bug fix? (use the a.b branch) | \[ Ni
> | New feature/enhancement? (use the a.x branch) | 5.x
> | Deprecations? | \[\]
> | BC breaks? (use the c.x branch) | 5.x
> | Automated tests included? | \[\] 
> | 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#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 "4.x" branch.
> \--\>
> 
> \#### Description:
> First Attempt to move to Symfony mailer instead of Swiftmailer.
> 
> 
> \<!--
> Please write a short README for your feature/bugfix. This will help people understand your PR and what it aims to do. 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:
> 
> \<!--
> This part is really important. If you want your PR to be merged, take the time to write very clear, annotated and step by step test instructions. Do not assume any previous knowledge - testers may not be developers.
> \--\>
> 1. Open this PR on Gitpod or pull down for testing locally (see docs on testing PRs \[here\](https://contribute.mautic.org/contributing-to-mautic/tester))
> 2.
> 
> \<!--
> If you have any deprecations, list them here along with the new alternative.
> If you have any backwards compatibility breaks, list them here.
> \--\>
> 
> 
> \<a href="https://gitpod.io/#https://github.com/mautic/mautic/pull/10742"\>\<img src="https://gitpod.io/button/open-in-gitpod.svg"/\>\</a\>

Next Tuesday we will have a call about the new changes in the mailing systems, so anyone who likes to join please message me on slack with your email and I will add you.

I hope this covers the issue and help you better understand how EmailBundle works

---

<div class="post-metadata">

**Author:** ![silavapi](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/silavapi/32/7424_2.png) [@silavapi](https://forum.mautic.org/u/silavapi)\
**Post date:** [July 15, 2022, 12:39pm UTC](https://forum.mautic.org/t/failed-email-status-only-after-three-attempts/23522/22 "2022-07-15T12:39:45Z")

</div>

Hi folks!

We have a PR to mark emails as failed if they fail, and not set DNC:

> <https://github.com/mautic/mautic/pull/11285>
>
> \<!-- ## Which branch should I use for my PR?
> 
> Assuming that:
> 
> a = current ma…jor release
> b = current minor release
> c = future major release
> 
> \* a.x for any features and enhancements (e.g. 4.x)
> \* a.b for any bug fixes (e.g. 4.0, 4.1, 4.2)
> \* c.x for any features, enhancements or bug fixes with backward compatibility breaking changes (e.g. 5.x) --\>
> 
> | Q | A
> | -------------------------------------- | ---
> | Bug fix? (use the a.b branch) | \[\]
> | New feature/enhancement? (use the a.x branch) | \[\]
> | Deprecations? | \[\]
> | BC breaks? (use the c.x branch) | \[\]
> | Automated tests included? | \[\] 
> | 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#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 "4.x" branch.
> \--\>
> 
> \#### Description:
> 
> \<!--
> Please write a short README for your feature/bugfix. This will help people understand your PR and what it aims to do. 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:
> 
> On email failed we set contact to DNC. 
> But failed send email could have a lot of reasons (smtp timeout etc). 
> Then I suggest mark email as failed and do not set contact as DNC.
> 
> \<!--
> This part is really important. If you want your PR to be merged, take the time to write very clear, annotated and step by step test instructions. Do not assume any previous knowledge - testers may not be developers.
> \--\>
> 1. Open this PR on Gitpod or pull down for testing locally (see docs on testing PRs \[here\](https://contribute.mautic.org/contributing-to-mautic/tester))
> 2. Hard to tell how to reproduce. Need make failed send email
> 3. Probably code review and unit tests are enoiught
> 
> \<!--
> If you have any deprecations, list them here along with the new alternative.
> If you have any backwards compatibility breaks, list them here.
> \--\>
> 
> 
> \<a href="https://gitpod.io/#https://github.com/mautic/mautic/pull/11285"\>\<img src="https://gitpod.io/button/open-in-gitpod.svg"/\>\</a\>

We would value testing and your thoughts!

Here’s how to test:

> **[Tester](https://contribute.mautic.org/contributing-to-mautic/tester#the-easy-way-using-gitpod)**
>
> . Every new feature and bug fix that is released in Mautic has undergone testing and code review by members of the community before it makes it into a release.We are always looking out for people to help us with these processes. Even if you can spare...

[Previous page](https://forum.mautic.org/t/failed-email-status-only-after-three-attempts/23522.md?page=1)
