sendgrid / sendgrid-php

The Official Twilio SendGrid PHP API Library
https://sendgrid.com
MIT License
1.49k stars 624 forks source link

Send a Single Email to Multiple Recipients #301

Closed thinkingserious closed 6 years ago

thinkingserious commented 7 years ago

Acceptance Criteria:

Reference:

CyberPunkCodes commented 7 years ago

This would pose quite difficult to build a one-size-fits-all method. If you look at my Yii2 SendGrid extension, it does this. In my example, I use Yii's ActiveRecord batch method to grab all the users from the database in batches of 500. The personalization object is filled with those 500 people. So if you had 2,000 users, it would only make 4 calls to the SendGrid API. 10,000 users, 20 calls. Very efficient 👍

There would almost have to be a helper that you fed ALL of the recipients into, that then splits them in batches (ie: 500 per loop). The problem you will run into, is if you had say 50,000 users in the database. What type of load would that put on the system? Loading the helper with 50k recipients to then process. I am not sure at what point you would reach memory errors and such. You don't really want one huge gigantic array of 50k people.

Yii uses ActiveRecord and efficiently can load batches. I am not really concerned if there was 50k users in the system, I think AR would handle it just fine.

Honestly, the best way is a cron job and to load all your recipients in a db table. Every few minutes, run and grab a few thousand, and process them (filling SendGrid personalization object with 500-1,000). You queue an email to 50k people, every minute the cron runs and grabs 5k (which makes 5-10 API calls). It would take about 10 minutes to mail to 50k people. - This would require a separate SendGrid PHP Queue library to be developed.

I guess the main question, is what is the point of this issue? It is easy to fill the SG personalization object with 500-1,000 people and kick off the email.. It is another story to handle 50k people and split it into micro-batches...

thinkingserious commented 7 years ago

Hi @WadeShuler,

We want to add some helpers to help make the most common use cases simpler. Please see the overarching project "Mail Helper Enhancement (v3 mail/send)." Here is how we implemented this in C#. The first step would be to propose the function signature.

With regards to batching, I love the idea of including batching as part of the helper, perhaps that's a parameter passed to this function.

With Best Regards,

Elmer

thedustyrover commented 7 years ago

Hi, just chiming in from the previous issue #269 and the Personalizations object mentioned there. One of the features I'd really like from a new API is to just completely separate the additional To-email inputs from the content of the email itself. In my example I use a number of substitution tags to fill in the email template, but these are going to be the exact same for every receiver.

AFAIK, both the Personalizations object and the previous SMTP API required duplicating all of these tags in a big array for every email sent which was just not necessary.

thinkingserious commented 7 years ago

Thanks for taking the time to provide some feedback @thedailynathan!

Please stay tuned for a proposal on how we will implement (to be posted here) as I'm sure your feedback will be valuable.

With Best Regards,

Elmer

CyberPunkCodes commented 7 years ago

I use a number of substitution tags to fill in the email template, but these are going to be the exact same for every receiver

@thedailynathan - Substitution tags are supposed to be unique to that personalization object. Example: Dear -firstname- or Your username is: -username-.

If your replacing a field that would be the same across all 1,000 emails, it should be done somewhere else.

Ideally, it would be done via sections. AFAIK, sections are broken and just don't work. Whether it is an issue with this repo, or the SendGrid API system itself, I am not sure.

I would have this handled inside your email content prior to sending (until sections work).

kylearoberts commented 7 years ago

Hey @WadeShuler,

Could you provide more details on how sections are broken? How have you tried using them? The more details we have the easier it should be to get to the bottom of the issue.

Thank,

Kyle

thinkingserious commented 6 years ago

This has been moved here.