Skip to content

Issue Batch Sending to GSuite Email Aliases #183

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

Closed
ghost opened this issue Apr 28, 2020 · 2 comments
Closed

Issue Batch Sending to GSuite Email Aliases #183

ghost opened this issue Apr 28, 2020 · 2 comments

Comments

@ghost
Copy link

ghost commented Apr 28, 2020

  • Anymail version: 7.1.0
  • ESP: SendGrid
  • Your ANYMAIL settings (change secrets to "redacted")
INSTALLED_APPS = [
    ...
    "anymail",  # Transactional Email
    ...
]
ANYMAIL = {"SENDGRID_API_KEY": env.str("SENDGRID_API_KEY")}
EMAIL_BACKEND = "anymail.backends.sendgrid.EmailBackend"
  • Versions of Django, requests, python: 2.2.10 | 2.22.0 | 3.7.5

I'm having issues batch sending in a very specific case where two of the emails belong to the same user, specifically when they are alias's of the same user through G Suite.

I'll put in dummy domains to highlight the example. Assume I owned test.com and test2.com. I may have both of these emails configured in the same G Suite account such that I can send and receive from [email protected] and [email protected] by logging into gmail from [email protected].

In this scenario, I may want to send a batch email using anymail and SendGrid via the following:

from django.core.mail import EmailMessage

message = EmailMessage(to=["[email protected]", "[email protected]"])
message.template_id = "d-585141a716834e60b9bb2a599215c1c4"
message.merge_data = {
    "[email protected]": {"some_var": "Hello0"},
    "[email protected]": {"some_var": "Hello1"},
}
message.send()

message.send() returns 1, as expected. However, only one message gets delivered. Looking at my SendGrid dashboard, I sometimes see activity showing both messages, sometimes just one.

The use case here is for a monthly status update email for a set of users. It is possible that users will have multiple accounts using emails that use different domains but are actually aliased to the same G Suite (or other email provider) user.

While this may be a rare case, without a resolution, I was going to follow the Django guide on sending multiple emails instead of the Anymail batch send. Is there an alternative recommendation for best practices when using Anymail?

I'm suspicious this may be a G Suite or SendGrid but was hoping for feedback on if this could be an Anymail issue. If it's a limitation of Anymail I'd be happy to help contribute either to the docs or codebase to resolve the issue.

Appreciate the help!

@medmunds
Copy link
Contributor

So, this sounds really weird. There's no way for Anymail to know anything about G Suite aliasing—as far as Anymail's concerned, [email protected] and [email protected] are completely different email addresses, and Anymail will ask SendGrid to send to both of them.

And actually, there's no way for SendGrid to know about G Suite aliasing either. They should be sending a message to each different address, as asked. So checking your SendGrid activity feed was a great place to start:

Looking at my SendGrid dashboard, I sometimes see activity showing both messages, sometimes just one.

This is suspicious. I'd suggest looking at these, in order:

  1. Are all the "processed" events there, for each address you sent to? Those mean SendGrid received the API request from Anymail. If "processed" is missing for any address, there's likely a bug in Anymail or SendGrid's API. (And if so, it's probably peculiar to that specific address or its merge data. For example, SendGrid's API has a long-standing problem that makes it silently ignore certain valid email addresses. Anymail has a workaround for the known cases, but you may have discovered something new.)

  2. Are all the "delivered" events there? Those indicate SendGrid successfully handed off the message to Google's email servers. If "delivered" is missing for any addresses, there should be a corresponding error event (blocked, bounced, deferred, dropped) explaining what went wrong. (And if not, you'd need to follow up with SendGrid support. For example, they may have assigned you to server pool that is throttling delivery.)

  3. If you get this far, the message made it to Google, so the problem is that G Suite is dropping it, or at least putting it somewhere the recipient isn't realizing it's there:

    • Spam folder?
    • Gmail might combine the two similar messages into a single "conversation" and collapse what it thinks is duplicated content. I've seen this happen, and it's confusing to read. The recipient has to figure out to click the subtle "..." ("show trimmed content") icon.
    • Anything else will probably require working with G Suite support.

[You might be tempted to also look at SendGrid's "opened" events, but these can be misleading because of the way Gmail proxies tracking pixels and other images.]

It's unlikely sending each message separately, rather than batch send, would make a difference. But if you have a reproducible test case, it certainly can't hurt to compare. (Unless you're batching hundreds of emails at once, the performance probably won't be noticeably different.)

@ghost
Copy link
Author

ghost commented Apr 29, 2020

@medmunds, thanks for getting back to me so quickly.

1 / 2. I think I may have made a premature comment on messages not showing up on SendGrid. SendGrid activity seems to lag and so in hindsight, all messages to [email protected] and [email protected] were sent and SendGrid has the status as delivered.

I have had issues with opens and clicks in G Suite in the past. Historically, we've seen and overestimation of clicks and opens however now I'm seeing no opens or clicks. This might also be a function of the dummy template I'm using to test this.

Sorry about that but I appreciate the helpful information.

  1. Forgot to mention, I did check the spam folder with no luck. The combination seems plausible, however unfortunate. If I'm able to get any information from G Suite I'll report back in case it helps anyone else in the future running into this.

In the meantime, for anyone else having difficulties, I was able to do this to make this work by approaching in a slightly different way. Haven't run performance metrics yet but I don't think there's much of a hit as I'm only using one connection and executing the send method once.

from django.core import mail
from django.core.mail import EmailMessage

to_emails = ["[email protected]", "[email protected]"]
data = {
    "[email protected]": {"some_var": "Hello0"},
    "[email protected]": {"some_var": "Hello1"},
}
connection = mail.get_connection()
messages = []
for t in to_emails:
    d = {}
    d[t] = data.get(t)
    message = EmailMessage(to=[t])
    message.template_id = "d-585141a716834e60b9bb2a599215c1c4"
    message.merge_data = d.copy()
    messages.append(message)

num_sent = connection.send_messages(messages)
connection.close()

I'll close this issue, thanks again for your help.

@ghost ghost closed this as completed Apr 29, 2020
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant