We help IT Professionals succeed at work.

Sending Email by getting email address from database

emi_sastra
emi_sastra asked
on
Hi All,

I want to email to several hundreds of users from database using Laravel.

I use gmail SMTP.

I try below code, by I get error. 500 - Internal Server Error.

                    foreach($users as $user){
                        $data = array(
                            'username' => $user->name,
                            'useremail' => $user->email,
                            'message' => $broadcast->message,
                            'subject' => $broadcast->subject,
                            'senderemail' => $sender->email,
                            'sendername' => $sender->name,
                            'attachment' => public_path('files').'/'.$broadcast->file,
                        );

                        //
                            sendBroadcastEmail::dispatchNow($data);

Open in new window


What is wrong with those code ?

Thank you.
Comment
Watch Question

David FavorFractional CTO
Distinguished Expert 2019

Commented:
To debug the 500 error...

Provide the full error from your Apache logs.

This will tell you the actual line of the problem.

Remember, only you know what services the class sendBroadcastEmail might provide.
David FavorFractional CTO
Distinguished Expert 2019

Commented:
Aside: You have one other challenge... Deliverability...

If the sendBroadcastEmail class lives in an SDK provided by a Mail Relay Service, like MailGun, then you'll be able to send mail + likely have high deliverability.

If this class runs native/local using your machine IPs, then you must first warmup your IP pool before you'll be able to deliver any mail.

Minimal steps for this...

1) Correct DNS PTR records for all IPs in your sending pool.

2) Correct DNS SPF records for all IPs in your sending pool.

3) Correct DNS DKIM selector record for your domain.

4) Correct DKIM message signing, using OpenDKIM or some similar system.

5) Correct DMARC DNS record, so you receive nightly reports telling you if #1-#4 are all correct.

6) Warmup your IP pool, which will take days to months, depending on email volume.

7) Monitor MTA message for mail send + stop all sends to any Mailbox Provider returning a status line about throttling or that your message is spam... because, whether you think your message is spam or not, if some Mailbox Provider like Oath (owns Yahoo/AOL addresses) says your message is spam, you must stop sending your message to Oath immediately or you'll eventually end up on RBLs.

8) Sending email is easy.

Having high deliverability is complex, requiring a massive amount of technical intelligence + also content construction intelligence.

9) Important: you're only ever one email away from being out of business... depending on the structure of your email + how you manage #7.

Author

Commented:
Hi David,

500 - Internal Server Error.
There is a error with the resource you are looking for, and it can not be displayed.

- Provide the full error from your Apache logs.
Where to find it?

Thank you.

Author

Commented:
Is there any problem with my code ?

Thank you.
David FavorFractional CTO
Distinguished Expert 2019

Commented:
1) No clue  about your code problem. To know for sure would require 100% of your code running somewhere.

Context is everything.

2) Apache log location varies between Distros. You'll usually find your log files somewhere like...

/var/log/apache2

/var/log/apache

/var/log/httpd

Open in new window

David FavorFractional CTO
Distinguished Expert 2019

Commented:
Note: If the Apache error log shows the problem is deep inside the sendBroadcastEmail class, you'll contact the owner of this code for assistance.

Author

Commented:
i just try it.

HTTP Error 500.0 - Internal Server Error
C:\PHP\php-cgi.exe - The FastCGI process exceeded configured activity timeout

What is the problem ?

Thank you.

Author

Commented:
- Note: If the Apache error log shows the problem is deep inside the sendBroadcastEmail class, you'll contact the owner of this code for assistance.
Those code are created by me.

Thank you.

Author

Commented:
There is no logs generated.

Thank you.
Hi,

You can change the ActivityTimeout setting

Locate the fcgiext.ini  file  %windir%\system32\inetsrv\fcgiext.ini
there is  a ActivityTimeout parameter  into this file that you can change
make sure that it is not commented
Restart the web service

activityTimeout can be also set in Apache / IIS
Some installation need to modify using this file C:\Windows\System32\inetsrv\config\applicationHost.config

This is depending of the OS and if this is local or hosting panel but just check where you can change the ActivityTimeout

Author

Commented:
I think changing ActivityTimeout  will not help.
Perhaps my code should be changed?

Thank you
Did you fix the 500 error and get the timeout error after or the 500 error generate the timeout error?

Author

Commented:
Hi  lenamtl,

- Did you fix the 500 error and get the timeout error after or the 500 error generate the timeout error?
I fixed the 500 error and get timeout error.

Thank you.
Ok then try to change the timeout value, this should fix the problem.
Don't forget to restart the web services
Scott FellDeveloper & EE Moderator
Fellow 2018
Most Valuable Expert 2013

Commented:
You have a couple of similar possible solutions provided as far as a time out issue.  However, one is for windows and the other is for linux. There are some assumptions being made.

Can you tell us what server you are on (linux, windows)? and what database you are using? We know you are using Laravel and you can set error logging https://laravel.com/docs/5.0/errors and you can also just view your php error log on the server.  Getting the detail errors will help here.

Do make sure your smtp settings are correct to start with.  Also you are using gmail. Make sure you have smtp turned on and are sending within the limits https://support.google.com/mail/answer/81126?hl=en and https://support.google.com/mail/answer/22839?hl=en

You would really be better off using a third party smtp service like sendgrid or mailgun. Doing so allows you to just send all the data via an api and they take care of rest.  As mentioned, your deliverable rate will increase by a lot.

Author

Commented:
Hi Scott,

I use windows server and MS SQL Server 2012.

I use gmail Smtp and it is fine, since for few emails it works.

Yes, third party smtp I think will be solved the problem.

Thank you.

Author

Commented:
Hi All,

I also need to know whethere my code is fine or not ?

Thank you.
Scott FellDeveloper & EE Moderator
Fellow 2018
Most Valuable Expert 2013

Commented:
The issue you are having probably .has nothing to do with the code.  We have tried to give you some hints including not using gmail.

A possible issue is sending out too many emails through your gmail account. Did you check to see if that is the case? You said, "I use gmail Smtp and it is fine, since for few emails it works."  That is not true because you may have it a limit or perhaps your emails were marked as spam and Google prevented emails going out in this manner https://support.google.com/mail/answer/22839?hl=en.  This is one area you need to verify on your own.  You also said, "Yes, third party smtp I think will be solved the problem."  Did you try to use sendgrid or another service to see if the same error exists?

"I fixed the 500 error and get timeout error."  
What as the initial fix for the 500 error?

Now you are getting a time out error. What line of code is the time out error occurring?

"I also need to know whethere my code is fine or not ?"

Your code as it is looks seems fine. However, without knowing the class  sendBroadcastEmail::dispatchNow($data); it is hard to tell.
David FavorFractional CTO
Distinguished Expert 2019

Commented:
If I understand you're comments above.

1) You fixed your email send problem by using a relay service.

2) Since the relay service immediately completes submissions, this means your FPM PHP processes now run quickly.

3) Since PHP processes run quickly, no timeout occurs now, so now your 500 errors are fixed too.

Be sure to mention if I've correctly understood your updates.

Author

Commented:
Hi David,

Problem still there.
Do you have sample code for sending email using sendgrid ?

Thank you.