user5694966
user5694966

Reputation:

Performance issue in sending email in Laravel 5.1

I am using below code to send email in Laravel 5.1

Mail::send('Project.Emails.Award', $data, function($message) 
{
    $message
            ->to('EmailAddress', 'EmailAddress')
            ->subject('hi');
});

Here the problem is the above function takes min 5 seconds to complete the processing.

Question : Is there deliver async method s that I don't need to wait for the response ?

Upvotes: 12

Views: 1598

Answers (2)

James
James

Reputation: 16359

I'm posting this at the request of Helper, and to demonstrate an API approach to this.

As they're after something free to use, Mailgun is probably the best option as you get 10,000 emails free each month and then pay a small fee for each email after that.

Laravel ships with the drivers ready to integrate in to Mailgun already and so to get started is actually really easy.

First off you just need to register for a Mailgun account and set up your domain:

  1. Register for Mailgun
  2. Set up your domain on Mailgun
  3. Verify the domain and update your DNS records to prove ownership and to allow SPF and DKIM authentication

Once that's done you just need to configure your application to use it. This guide covers the configuration of Mailgun on Laravel quite well, but essentially you:

  1. Add Guzzle if you don't already have it (as you are sending your emails via an API to Mailgun to be queued and sent) composer require "guzzlehttp/guzzle=~5.0"
  2. Ensure your config/services.php file matches the below configuration so that we can keep our details safe and just store them in the .env file

i.e

'mailgun' => [
    'domain' => env('MAILGUN_DOMAIN'),
    'secret' => env('MAILGUN_SECRET'),
  ],
  1. Add the necessary fields to our .env file and populate them with the correct values

e.g

MAIL_DRIVER=mailgun
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=587
MAIL_USERNAME=XXX
MAIL_PASSWORD=XXX
MAIL_ENCRYPTION=tls
MAILGUN_DOMAIN=THE-DOMAIN-SETUP-IN-MAILGUN
MAILGUN_SECRET=THE-API-KEY-FOR-DOMAIN

Once you've set it all up, then you can simply use the Mail::send() command to send off emails like you were before - however it will be instant now (depending on your server - mine is pretty basic and sends emails instantly):

Mail::send('Project.Emails.Award', $data, function($message) 
{
    $message
            ->to('EmailAddress', 'EmailAddress')
            ->subject('hi');
});

Upvotes: 1

Marcel Gwerder
Marcel Gwerder

Reputation: 8520

Depending on which mail driver you are using or you have to use there may be other options to improve performance. However the most effective way to keep the UI responsive is queueing the mail messages.

With your code this would be as simple as:

Mail::queue('Project.Emails.Award', $data, function($message) 
{
    $message
            ->to('EmailAddress', 'EmailAddress')
            ->subject('hi');
});

You though need to have queueing set up and you won't be able to do this properly on some managed servers.

Upvotes: 8

Related Questions