I have a Laravel app that I’m serving in Digital Ocean through Laravel Forge, I am using the email server from my old shared server from HostGator, my configuration is as follows:
MAIL_DRIVER=smtp MAIL_HOST=cloud232.hostgator.com MAIL_FROM_ADDRESS=noreply@********* MAIL_FROM_NAME=noreply MAIL_USERNAME=noreply@************ MAIL_PASSWORD=******************** MAIL_ENCRYPTION=tls MAIL_PORT=587
THIS WORKS CORRECTLY, I AM ABLE TO SEND EMAILS.
And I have a method that filters the database with one passed argument and then sends the emails to the filtered customers.
I call this method from a URL using cron jobs on forge (not the Laravel scheduler but an entry in the cron jobs tables).
As I’ve been using this for the past few months, now I have added another method that calls the same function but filters the DB in different way (and the collection returns correctly), but the new method could not send any email because I get a connection refused error like this one:
Connection could not be established with host cloud232.hostgator.com [Connection refused #111]
And this is the stack trace:
#0 /home/forge/prestamos.ikiwi.net.ar/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/StreamBuffer.php(58): Swift_Transport_StreamBuffer->establishSocketConnection() #1 /home/forge/prestamos.ikiwi.net.ar/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/AbstractSmtpTransport.php(143): Swift_Transport_StreamBuffer->initialize(Array) #2 /home/forge/prestamos.ikiwi.net.ar/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Mailer.php(65): Swift_Transport_AbstractSmtpTransport->start() #3 /home/forge/prestamos.ikiwi.net.ar/vendor/laravel/framework/src/Illuminate/Mail/Mailer.php(484): Swift_Mailer->send(Object(Swift_Message), Array) #4 /home/forge/prestamos.ikiwi.net.ar/vendor/laravel/framework/src/Illuminate/Mail/Mailer.php(259): IlluminateMailMailer->sendSwiftMessage(Object(Swift_Message)) #5 /home/forge/prestamos.ikiwi.net.ar/vendor/laravel/framework/src/Illuminate/Mail/Mailable.php(159): IlluminateMailMailer->send(Object(IlluminateSupportHtmlString), Array, Object(Closure)) #6 /home/forge/prestamos.ikiwi.net.ar/vendor/laravel/framework/src/Illuminate/Support/Traits/Localizable.php(19): IlluminateMailMailable->IlluminateMail{closure}() #7 /home/forge/prestamos.ikiwi.net.ar/vendor/laravel/framework/src/Illuminate/Mail/Mailable.php(160): IlluminateMailMailable->withLocale(NULL, Object(Closure)) #8 /home/forge/prestamos.ikiwi.net.ar/vendor/laravel/framework/src/Illuminate/Mail/Mailer.php(275): IlluminateMailMailable->send(Object(IlluminateMailMailer)) #9 /home/forge/prestamos.ikiwi.net.ar/vendor/laravel/framework/src/Illuminate/Mail/Mailer.php(229): IlluminateMailMailer->sendMailable(Object(AppMailCompleteRegistration)) #10 /home/forge/prestamos.ikiwi.net.ar/vendor/laravel/framework/src/Illuminate/Mail/PendingMail.php(127): IlluminateMailMailer->send(Object(AppMailCompleteRegistration)) #11 /home/forge/prestamos.ikiwi.net.ar/app/Http/Controllers/CustomerController.php(100): IlluminateMailPendingMail->send(Object(AppMailCompleteRegistration)) #12 /home/forge/prestamos.ikiwi.net.ar/app/Http/Controllers/CustomerController.php(90): AppHttpControllersCustomerController::sendMail(2, 3, Object(IlluminateDatabaseEloquentCollection)) #13 /home/forge/prestamos.ikiwi.net.ar/app/Http/Controllers/CustomerController.php(26): AppHttpControllersCustomerController::sendReminder(3, 599) #14 [internal function]: AppHttpControllersCustomerController::thirdReminder() #15 /home/forge/prestamos.ikiwi.net.ar/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(54): call_user_func_array(Array, Array) #16 /home/forge/prestamos.ikiwi.net.ar/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(45): IlluminateRoutingController->callAction('thirdReminder', Array) #17 /home/forge/prestamos.ikiwi.net.ar/vendor/laravel/framework/src/Illuminate/Routing/Route.php(219): IlluminateRoutingControllerDispatcher->dispatch(Object(IlluminateRoutingRoute), Object(AppHttpControllersCustomerController), 'thirdReminder') #18 /home/forge/prestamos.ikiwi.net.ar/vendor/laravel/framework/src/Illuminate/Routing/Route.php(176): IlluminateRoutingRoute->runController() #19 /home/forge/prestamos.ikiwi.net.ar/vendor/laravel/framework/src/Illuminate/Routing/Router.php(680): IlluminateRoutingRoute->run() #20 /home/forge/prestamos.ikiwi.net.ar/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(30): IlluminateRoutingRouter->IlluminateRouting{closure}(Object(IlluminateHttpRequest)) #21 /home/forge/prestamos.ikiwi.net.ar/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php(41): IlluminateRoutingPipeline->IlluminateRouting{closure}(Object(IlluminateHttpRequest)) #22 /home/forge/prestamos.ikiwi.net.ar/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(163): IlluminateRoutingMiddlewareSubstituteBindings->handle(Object(IlluminateHttpRequest), Object(Closure)) #23 /home/forge/prestamos.ikiwi.net.ar/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): IlluminatePipelinePipeline->IlluminatePipeline{closure}(Object(IlluminateHttpRequest)) #24 /home/forge/prestamos.ikiwi.net.ar/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php(58): IlluminateRoutingPipeline->IlluminateRouting{closure}(Object(IlluminateHttpRequest)) #25 /home/forge/prestamos.ikiwi.net.ar/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(163): IlluminateRoutingMiddlewareThrottleRequests->handle(Object(IlluminateHttpRequest), Object(Closure), 60, '1') #26 /home/forge/prestamos.ikiwi.net.ar/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): IlluminatePipelinePipeline->IlluminatePipeline{closure}(Object(IlluminateHttpRequest)) #27 /home/forge/prestamos.ikiwi.net.ar/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(104): IlluminateRoutingPipeline->IlluminateRouting{closure}(Object(IlluminateHttpRequest)) #28 /home/forge/prestamos.ikiwi.net.ar/vendor/laravel/framework/src/Illuminate/Routing/Router.php(682): IlluminatePipelinePipeline->then(Object(Closure)) #29 /home/forge/prestamos.ikiwi.net.ar/vendor/laravel/framework/src/Illuminate/Routing/Router.php(657): IlluminateRoutingRouter->runRouteWithinStack(Object(IlluminateRoutingRoute), Object(IlluminateHttpRequest)) #30 /home/forge/prestamos.ikiwi.net.ar/vendor/laravel/framework/src/Illuminate/Routing/Router.php(623): IlluminateRoutingRouter->runRoute(Object(IlluminateHttpRequest), Object(IlluminateRoutingRoute)) #31 /home/forge/prestamos.ikiwi.net.ar/vendor/laravel/framework/src/Illuminate/Routing/Router.php(612): IlluminateRoutingRouter->dispatchToRoute(Object(IlluminateHttpRequest)) #32 /home/forge/prestamos.ikiwi.net.ar/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(176): IlluminateRoutingRouter->dispatch(Object(IlluminateHttpRequest)) #33 /home/forge/prestamos.ikiwi.net.ar/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(30): IlluminateFoundationHttpKernel->IlluminateFoundationHttp{closure}(Object(IlluminateHttpRequest)) #34 /home/forge/prestamos.ikiwi.net.ar/vendor/fideloper/proxy/src/TrustProxies.php(57): IlluminateRoutingPipeline->IlluminateRouting{closure}(Object(IlluminateHttpRequest)) #35 /home/forge/prestamos.ikiwi.net.ar/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(163): FideloperProxyTrustProxies->handle(Object(IlluminateHttpRequest), Object(Closure)) #36 /home/forge/prestamos.ikiwi.net.ar/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): IlluminatePipelinePipeline->IlluminatePipeline{closure}(Object(IlluminateHttpRequest)) #37 /home/forge/prestamos.ikiwi.net.ar/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): IlluminateRoutingPipeline->IlluminateRouting{closure}(Object(IlluminateHttpRequest)) #38 /home/forge/prestamos.ikiwi.net.ar/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(163): IlluminateFoundationHttpMiddlewareTransformsRequest->handle(Object(IlluminateHttpRequest), Object(Closure)) #39 /home/forge/prestamos.ikiwi.net.ar/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): IlluminatePipelinePipeline->IlluminatePipeline{closure}(Object(IlluminateHttpRequest)) #40 /home/forge/prestamos.ikiwi.net.ar/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): IlluminateRoutingPipeline->IlluminateRouting{closure}(Object(IlluminateHttpRequest)) #41 /home/forge/prestamos.ikiwi.net.ar/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(163): IlluminateFoundationHttpMiddlewareTransformsRequest->handle(Object(IlluminateHttpRequest), Object(Closure)) #42 /home/forge/prestamos.ikiwi.net.ar/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): IlluminatePipelinePipeline->IlluminatePipeline{closure}(Object(IlluminateHttpRequest)) #43 /home/forge/prestamos.ikiwi.net.ar/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php(27): IlluminateRoutingPipeline->IlluminateRouting{closure}(Object(IlluminateHttpRequest)) #44 /home/forge/prestamos.ikiwi.net.ar/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(163): IlluminateFoundationHttpMiddlewareValidatePostSize->handle(Object(IlluminateHttpRequest), Object(Closure)) #45 /home/forge/prestamos.ikiwi.net.ar/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): IlluminatePipelinePipeline->IlluminatePipeline{closure}(Object(IlluminateHttpRequest)) #46 /home/forge/prestamos.ikiwi.net.ar/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php(62): IlluminateRoutingPipeline->IlluminateRouting{closure}(Object(IlluminateHttpRequest)) #47 /home/forge/prestamos.ikiwi.net.ar/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(163): IlluminateFoundationHttpMiddlewareCheckForMaintenanceMode->handle(Object(IlluminateHttpRequest), Object(Closure)) #48 /home/forge/prestamos.ikiwi.net.ar/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): IlluminatePipelinePipeline->IlluminatePipeline{closure}(Object(IlluminateHttpRequest)) #49 /home/forge/prestamos.ikiwi.net.ar/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(104): IlluminateRoutingPipeline->IlluminateRouting{closure}(Object(IlluminateHttpRequest)) #50 /home/forge/prestamos.ikiwi.net.ar/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(151): IlluminatePipelinePipeline->then(Object(Closure)) #51 /home/forge/prestamos.ikiwi.net.ar/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(116): IlluminateFoundationHttpKernel->sendRequestThroughRouter(Object(IlluminateHttpRequest)) #52 /home/forge/prestamos.ikiwi.net.ar/public/index.php(55): IlluminateFoundationHttpKernel->handle(Object(IlluminateHttpRequest)) #53 {main}
How can this happen if it is exactly the same code??
foreach ($customers as $customer) { try { Mail::to($customer->email)->send(new CompleteRegistration($step, $reminderCount, $customer)); } catch (Exception $e) { Log::error($e->getMessage()); } }
Thanks in advance.
Advertisement
Answer
According to HostGator documentation:
Any time you’re sending a message no matter how large your e-mail list is you must throttle it. We recommend you throttle it to at the very least sending 1 email every 8 seconds. (Sending 1 every 8 seconds would send 450 emails within 1 hour, keeping you below the 500 outgoing email limit.) If the mailing list software you’re using does not allow you to throttle you must switch to an application or script that will. We recommend PHPList, which can be found in your CPanel, under Quickinstall.
IMPORTANT: If you do not throttle and you try sending 500 emails, the server will try sending all 500 in 1 second which is not possible on our shared servers. This will cause a very high load on the server and the entire server will be sluggish, potentially affecting your sites and service, until this sending process is completed. It is our job to keep the server up and running without being sluggish or experiencing issues. Anyone who causes the server’s load to go high will be suspended and the process will be terminated. If you choose not to throttle, you will most likely be suspended for crashing the server.
I began to try with smaller mailing lists, and it turns out that I’m able to send no more than 7 emails per process, so the way to solve this is to throttling the send process.
The reason why it didn’t happen with the other old processes that I have is that I run them with very short time between them, so the list is never big enough, for this thread that I opened, I run the new method once a day, so the list gets bigger and bigger.
As I am working with Laravel I will think a solution combining cron jobs, jobs and queues, thank you very much for you effort and answers, hope this thread will help other people.