Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Different behaviour of send_messages when SMTP backend is used as context manager #96

Closed
amureki opened this issue Aug 22, 2024 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@amureki
Copy link
Member

amureki commented Aug 22, 2024

When using this package's TrackingSMTPEmailBackend, I noticed the following behavioural differences:

If I am sending several emails via:

from django.core import mail
messages = [] # list of `emark.message.MarkdownEmail` emails
mail.get_connection().send_messages(messages)

The internal flow for these looks like the following:

  1. Each of the email gets rendered
  2. We are opening an SMTP connection
  3. Emails are sent
  4. Connection is closed

However, things are different if I am sending the very same emails, but with using a context manager:

from django.core import mail
messages = [] # list of `emark.message.MarkdownEmail` emails
with mail.get_connection() as connection:
    connection.send_messages(messages)
  1. We are opening an SMTP connection
  2. Each of the email gets rendered
  3. (if the rendering time is beyond SMTP timeout, connection drops)
  4. Emails are sent
  5. Connection is closed

And since the rendering (with included tracking) is a heavy operation, I am experiencing SMTP timeout error when sending more than 10 emails.

@amureki amureki added the bug Something isn't working label Aug 22, 2024
@amureki amureki changed the title Different behaviour of send_messages when used as context manager Different behaviour of send_messages when SMTP backend is used as context manager Aug 22, 2024
@codingjoe
Copy link
Member

Uff, that's a tough one. This isn't really based on anything the package does. Django doesn't call the message method on email instances prior either. Connection timeouts, would be an issue that could face Django either. I believe the only good approach is to prevent users from passing mails that aren't rendered forcing them to render messages in advance.

Or, we submit this to Django and have them use lazy connections or whatever they do, with a connection that's hasn't been entered.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants