Reputation:
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
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:
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:
composer require "guzzlehttp/guzzle=~5.0"
config/services.php
file matches the below configuration so that we can keep our details safe and just store them in the .env
filei.e
'mailgun' => [
'domain' => env('MAILGUN_DOMAIN'),
'secret' => env('MAILGUN_SECRET'),
],
.env
file and populate them with the correct valuese.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
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