How to Send Email From Laravel, and Why We Need 3rd Party Providers For It

Sending email is a typical function for most web-projects: notifications, password reminders, invoices are done via email. But in recent years we've faced a technical problem. It's not about just sending emails anymore, it's about delivering them successfully. Disclaimer: this is an article mostly aimed at web-project clients, not developers. But if you are a developer, feel free to send this article to your clients, to explain why email sending is more complicated than they think.

Why email sending is not simple anymore

Reasons why emails may fail to be delivered:
  • Technical error of your programming script
  • Blocked by your hosting provider while sending
  • Reached the email sending rate limit set by your hosting provider
  • Blocked by recipient server because of "bad reputation" of your hosting provider server
  • Went to SPAM or another automatically filtered folder
  • Went to GMail tab like "Promotions"
  • etc.
Except for error in your script, most of those failures have one reason: your hosting provider's server isn't set up for properly sending email. Technically, it can send them, but it can't give any guarantee about delivery. You see, in recent years email clients stood up for a big fight against spam and unsolicited email. They introduced a lot of automatic blocking mechanisms, based on the reputation of the sender. The most common example is when server's IP is being blacklisted for the amount of email it is sending. That is what usually happens if you try to send 100+ emails at the same time programmatically from a simple "shared" hosting server. These "horror stories" are mostly related to the case if you try to send email from your hosting, just using native PHP functions like mail(). This is where third-party providers come into play.

Third-party email sending providers

To solve most of the problems listed above, you can use a service to deliver email for you. Whenever you want to send an email, you just call that service's function, and they take care of the deliverability for you, usually promising 99%+ guarantee. They do that by providing their own whitelisted servers which have a really good sending reputation, for sending good quality email. They care about that reputation a lot, so the also fight against spammers in their own system. So if you try to send spam or unsolicited email via their servers, guess what - they will block your account and your domain name then gets blacklisted. These are the most popular services: Important notice - those services are not free. For large amount of email, that is. Each of them has its own "free tier" limit - like, Mailgun allows 10,000 emails per month (but you still need to put in your credit card, it just won't be charged until the limit is reached). Let's look at the prices for the lowest tiers, as it stands in July 2018*:
Provider Free Tier Lowest Price after Free Tier
Mailgun 10,000 emails per month $5 per 10,000 emails per month
Sparkpost 15,000 emails per month $9 per 50,000 emails per month
Amazon SES - $0.10 per 1,000 emails
Sendgrid 100 emails per day $9.95 per 100,000 emails per month
Mandrill (part of Mailchimp) - $40 per 50,000 emails per month
Mailjet 6,000 emails per month $8.04 per 30,000 emails per month
SendInBlue 300 emails per day 19 EUR per 40,000 emails per month
* Notice 1: you can't compare these prices directly, cause each provider has different tiers with different formulas and additional charges. ** Notice 2: please don't go for the cheapest option, cause it may cause worse deliverability. *** Notice 3: we don't guarantee these prices, so please check at the time you will be reading this article.
It's not only about the price. You will have to not only register on their website, but also verify your domain name, which you will use for sending, like [something]@laraveldaily.com. You will have to update DNS records into your domain settings. It's quite technical, here's an example instruction from Mailgun. It may look really complicated, but that's the only way these services can actually provide quality email delivery.

How to use third-party providers in Laravel

There are three providers which are supported by default in Laravel:
  • Mailgun
  • Sparkpost
  • Amazon SES
For all of those, all you need to do is get some credentials from them, and put them into .env file, or into config/services.php (not recommended):
'mailgun' => [
    'domain' => 'your-mailgun-domain',
    'secret' => 'your-mailgun-key',
],
'sparkpost' => [
    'secret' => 'your-sparkpost-key',
],
'ses' => [
    'key' => 'your-ses-key',
    'secret' => 'your-ses-secret',
    'region' => 'ses-region',  // e.g. us-east-1
],
And then you use default Laravel Mail::send() functionality, that's it! I personally use Mailgun and have no problem with it, so can recommend to you, too. If you want to use any service outside of those three, you need to find their documentation on integration with Laravel. It probably will be some API calls, or getting you SMTP details, so you would use Laravel's smtp driver. Final tip: you can use services for temporary email testing which send all the emails into a "virtual inbox", no matter the recipient. For our team we use Mailtrap.io.
avatar
Ebenezer Narh-ngwah

Very useful, thanks

👍 1
avatar

good 👍 👍

avatar

Very useful, thanks

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 58 courses (1054 lessons, total 46 h 42 min)
  • 78 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord

Recent Premium Tutorials