Slow mailing with Laravel?

Asked

Viewed 602 times

1

I’m doing some mass mail testing but I’m finding it a bit slow the process being done with Laravel. The reason I was finding sending it slow is that I worked in a company where on average 50~60,000 emails were sent around 10 to 12 hours. To test the procedure I’m using only 10 and in a first test it took 45 seconds to finish the process.

STRUCTURE OF THE PROJECT

Laravel 5.4.*

Postgresql database to store the name and emails of pseudo subscribers, basically I use 1 only table with 5 fields (ID, NAME, EMAIL, CREATED_AT, UPDATED_AT).

Sending the emails are being done with the Laravel queue system using the redis. I took the initiative to use the PHPREDIS extension contrary to the standard that already comes with the Laravel that is the Predis, I did this because the performance is superior.

FLOW

Link on the home page calling a route

<div class="links">
    <a href="{{Route('enviarNews')}}">Enviar News</a>
</div>

The route calls this controller, which in turn calls a Job as you can see

<?php

namespace App\Http\Controllers;

use App\Jobs\MailJob;

class MailController extends Controller{

    public function enviarNews(){
        dispatch(new MailJob());

        return 'Sua newsletter está em processo de preparo e sera enviada em breve.';
    }

}

To Job, I’m calling the mail class.

<?php    
namespace App\Jobs;    
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;    
use Illuminate\Support\Facades\Mail;
use App\MailModel;
use App\Mail\MailSend;   

class MailJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * Create a new job instance.         
     * @return void
     */
    public function __construct()
    {
    }    
    /**
     * Execute the job.         
     * @return void
     */
    public function handle()
    {
        $mails = MailModel::all();            
        foreach ($mails as $mail){
            Mail::to($mail->email)->send(new MailSend($mail));
        }
    }
}

Mailsend and View class

<?php    
namespace App\Mail;    
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\MailModel;

class MailSend extends Mailable
{
    use Queueable, SerializesModels;    
    /**
     * Create a new message instance.
     * @return void
     */        
    public $mail;        
    public function __construct(MailModel $mail)
    {
        $this->mail = $mail;
    }    
    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->from("[email protected]", "Firemail Testes")
        ->subject("Testes de Envio em Massa")
        ->view('mail.news1');
    }
}

View

<p>Ola {{$mail->name}}, estamos testando o envio de emails em massa!</p>

That’s what’s being done, anything in my code might be slowing down sending? By the accounts I did, with this current metric could only send about 19200 emails per day, well below the one I mentioned above and taking advantage, this slowness can be caused by my SMTP server? for these tests I am using the SMTP2GO?

  • Can it also be on my internet? (However on average it runs at 30mb).

  • What can I do to improve this process and leave as soon as possible?

  • Could someone help me to set the question? I have a huge problem creating questions here because the code always gets broken.

  • My question: Has this report of slowness occurred now or has it always been slow? Are you only sending text or attachment as well?

  • So Everson, I’m testing this question of sending emails with Laravel, you understand? I made a small system where I sent confirmation emails to the user in some requests and in this system the emails took about 3 seconds to be sent. So to answer your question, you’ve always been slow that way

  • I suggest that you first do a telnet test (https://wiki.locaweb.com.br/pt-br/Teste_de_mail_sendinge_receiving via_Telnet) with the authentication data, to be based on whether sending directly to the smtp server takes that 3 seconds you report. In case it takes time the problem is probably in the server, otherwise it will be in your code. And with this we will be checking if there is any problem.

  • Okay, I was studying the part and I read something in the documentation that said more or less the following: "API-based drivers, like Mailgun and Sparkpost, are usually simpler and faster than SMTP servers." I will also be testing this other configuration that they recommend and I will again give answers about the sending time that was required. Thanks for the support Everson!

  • SMTP method will always, ALWAYS, ALWAYS be slow. Regardless of the code used, language or computational power, since the slowness is found on the SMTP server, not on its infrastructure. I recommend using API for these questions, Mail Queue or specific services for this, such as Sendgrid, Mandrill and etc.

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.