Sendgrid-php: This code sent 200.000 emails, in less than a minute??

Created on 24 Nov 2016  路  26Comments  路  Source: sendgrid/sendgrid-php

My account was then suspended.

Screenshot: http://i.imgur.com/8nVYLPc.png

Looks completely fine to me. It grabs the emails from a database, verifies if they are OK then sends them an email.

It's also limited to max 1.000 emails in one instance. I only ran this script once, so I have no clue how I could send 200.000 emails in less than a minute time.

I believe the error is from sendgrid (smtp) not my code.. ideas? they claim its the code, which is taken from THIS github.

question

Most helpful comment

What version of the SendGrid library are you using and did you step through this code row-by-row before you let it run?

I've noticed, the latest SendGrid library v5.1.2 returns 202 Accepted instead of 200 OK by default. Unless you're doing something different to expect 200 in which case your code would never execute the update statement.

All 26 comments

What version of the SendGrid library are you using and did you step through this code row-by-row before you let it run?

I've noticed, the latest SendGrid library v5.1.2 returns 202 Accepted instead of 200 OK by default. Unless you're doing something different to expect 200 in which case your code would never execute the update statement.

Great catch @Dreyer!

Can you please verify @techpulse?

Hi.

I checked, and the version seems to be: 4.0.4?

@Octolus,

In that case, you are using the v2 mail send endpoint, which does return a 200 on successful completion.

A few thing to consider:

1)

Can you try re-running your script with some logging added without the actual send() call to see if you get a 200k loop?

2)

Have you tested that the method chaining to call getCode() works?

It looks like the documented method is as follows: https://github.com/sendgrid/sendgrid-php/tree/v4.0.4#getcode

I rather not, as my account were suspended due this. I did use the example, I'm pretty sure. No, i have not tested. I literally copy-pasted from here lol.

Hi @Octolus,

For item 1, I was thinking that you can do something like this:

  1. Remove lines 40, 41, 49, 50, 51, 53 and 55
  2. You can even comment out 45 and 46
  3. After echo $x++ add echo $mail['mail']

The idea is to make sure the logic is working since 200,000 emails were sent.

For item 2, I was thinking you would change line 40 to:

$res = $sendgrid->send($email);
if($res->getCode() == 200)

Thanks!

I will let you know once my sending limit have decreased. Currently it's around 11.000 :)

@Octolus,

To run the test, your sending limit will not matter because you will not actually make the call to SendGrid.

Hey @thinkingserious I have time/interest to test this now. Here is my code, does this look OK to test?

http://pastebin.com/nitaBvwY

Code returned 940 emails.

The last email:
[email protected] - 940

There are no duplicate entries either, so I have issues understanding how it could've sent thousands of emails to same user.

It's a foreach, not a loop. So it's really interesting why it sent so many emails.

@Octolus,

I would just leave this line in: echo $x++;, but just make it $x++;

Then, outside the loop do echo $x;

Thanks!

Would work the same way, but yes there are no duplicates. @thinkingserious

So I have zero clue, how it could send thousands of emails to these users.

What were the results? What was the final value of $x. Did the emails print as expected?

Nevermind, did not see your comment above.

I can only imagine the sendgrid smtp/api must've glitched up when this occurred. My code looks OK. It has checks to prevent the same user getting same email etc.

Do you know if all those 940 were sent emails the first time? Or did a bunch of duplicates just go out to the same user?

I do not know this sadly, as the logs were deleted from my account. The support have not answered me in over 15 days now, they just unlocked my account and just blamed it on my code.

But I got reports from customers, that some of them got "thousands" of emails. This could probaly be anywhere from 100-5000 per user. My sendgrid account said it sent over 200.000 but I am using a free account, so that shouldn't really be possible?

Small update.

I limited the database query to return 100 emails, where 93 was valid after checking them.

However, sendgrid shows that I sent 1.39k emails? Before I executed this, i had 45 emails.

It keeps going up now it's at 3.59K

I have log of all the emails that was sent to, exactly 93 emails.

My sendgrid account went from 45 to http://i.imgur.com/uwrfI1A.png and it keeps increasing. What the heck?

This is a scam, do you work for them @thinkingserious ? Obviously bug in their system. This code is perfectly fine, unless it glitches when in foreach loop. I will make it send me an email using foreach loop.

@thinkingserious & @Dreyer I managed to replicate the issue! Finally.

I looped it 10 times, it ended up as 100's of emails.

http://pastebin.com/UP1NCYFc

replace my email with yours of course

10 turned into 30.

Perhaps I have to unset $email in each loop.

After placing

$email = new SendGrid\Email();

and unset($email) inside the foreach/loop it works as expected. Is this supposed to be done this way? Seems kind of weird having to unset/call the class constantly. Never experienced such before.

Hi @Octolus,

Yes, I work for SendGrid as a Developer Experience engineer, with a focus on our open source libraries.

My guess is that since you are using the same object over and over, when you call AddTo, it keeps adding to that same object. So by unsetting it, you are starting fresh each time, since AddTo is adding to the To array instead of overwriting it.

is this a issue with MY code though @thinkingserious or?

In looking at the v2 docs, it seems clear that the AddTo function continues to add emails to the object.

So I would say our code is behaving as advertised.

If the function were named SetTo, then I would expect it to be overwritten.

What could have made the documentation better is an example of looping through a list of emails such as what you are doing.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

KayakinKoder picture KayakinKoder  路  5Comments

FilipLukac picture FilipLukac  路  4Comments

bjornmann picture bjornmann  路  3Comments

izhukovich picture izhukovich  路  4Comments

elshafey picture elshafey  路  4Comments