To use this site please enable javascript on your browser! Bryce Andy | Sparkpost as an Alternative to Gmail with Laravel 5.7

Sparkpost as an Alternative to Gmail with Laravel 5.7


Bryce on Dec 5, 2018

Sparkpost

If you are like me and have used Gmail sending emails using Laravel, you might have realized after sometime you needed to tweak up your email settings or laravel config to make it work again, because it falls into various SwiftMailer exceptions.

This is unpleasant especially for a business or a blog sending multiple emails at a time or even worse with automated emails failing in your system. Thats why with Laravel we have plenty of email delivery services, and now we are gonna talk about a particular one I like, as you might have guessed it, Sparkpost.

 

Create an account with Sparkpost

First of all make sure you have a working domain such as mywebsite.com because we aren't dependant on Gmail anymore. In my case I get my domains from Namecheap, but you can get yours anywhere you prefer. Head over Sparkpost's website and create an account.

Spark Sign Up

After verifying your email you will be prompted to input your domain (example.com) which will be used to send emails and lastly select the API option as the preferred option to deliver emails, you will thereafter receive an API key which you have to copy it and never lose it.

There are a couple of checkpoints you need to go through so as you can start using this service. All of them are easy but the most important one is verifying ownershhip of the domain you provided.

 

Verifying Domain Ownership

Domain names for just 88 cents!

On the left sidebar you will find a section named Sending Domains and there you will add a domain or find the one you first put in the previous section. Click the domain and you will see DNS settings listed each with three records: TXT/CNAME, Host, Value

If you have access to DNS records of your domain then go ahead and add a TXT record with the hoost name given and value. Then back to Sparkpost and click verify TXT record or CNAME record, according to your selection.

This method isn't always straightforward and if you don't have your DNS record use the other alternative where you just input the email that you want to send emails with, for example 'info@yourwebsite.com'. After that they will send a verification email and we can proceed sending.

 

Laravel Configuration

Now open your config/services.php file and paste the API key we initially obtained when creating an account on the SPARKPOST_SECRET variable. Then head over to the config/mail.php and make your config values look like this:

<?php

return [

    'driver' => env('MAIL_DRIVER', 'sparkpost'),

    'host' => env('MAIL_HOST', 'smtp.sparkpostmail.com'),

    'port' => env('MAIL_PORT', 587),

    'from' => [
        'address' => env('MAIL_FROM_ADDRESS', 'hello@bryceandy.com'),
        'name' => env('MAIL_FROM_NAME', 'bryceandy'),
    ],

    'encryption' => env('MAIL_ENCRYPTION', 'starttls'),

    'username' => env('MAIL_USERNAME', 'SMTP_Injection'),

    'password' => env('SPARKPOST_SECRET'),

    'sendmail' => '/usr/sbin/sendmail -bs',

    'markdown' => [
        'theme' => 'default',

        'paths' => [
            resource_path('views/vendor/mail'),
        ],
    ],

];

Remember to substitute the sparkpost_secret variable with the API key and for security reasons it is better to store somewhere secure instead of committing it to a public repository such as Git.

Now we can start sending emails. If you missed on my post on how to send emails then have a look here.

Conclusion

It is relatively cheap and easy sending emails with dedicated email delivery services like Sparkpost rather than being dependant on Google. For inquiry or any questions relating to web development contact me through hello@bryceandy.com and comment on what you think about this post.

 


Tags




Bryce
Bryce is a web developer based in Dar. Apart from writing code, you can find him in the gym, writing informative articles or playing football.


Copyright © Bryce Andy 2018