Environment variables for sending email in cPanel, laravel

Added on Fri, 23 Aug 2019

This is something I always end up googling from time to time. Here are few things I needed to understand. As I get started with laravel app, most of my apps have something to do with email in some way. The default values for this in a new laravel app will be:
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
If we were to send email from a laravel app in cPanel, first thing is MAIL_DRIVER. smtp is the default value and it is what we need to write.
Next variable is MAIL_HOST. For cPanel, you can set it to your domain name or if that doesn’t work, add smtp in the front like so: smtp.example.com
Set MAIL_PORT to 587 and create one email from cPanel and add a secure password. It should
Environment variables in .env file should look like:
MAIL_DRIVER=smtp
MAIL_HOST=smtp.example.com
MAIL_PORT=587
MAIL_USERNAME=mail@example.com
MAIL_PASSWORD=yoursecretpassword
MAIL_ENCRYPTION=tls
Conclusion
And now we should be able to send email from an laravel app
Mail::to($to)->send(new ContactMail($contact));
Thank you for reading. Please clap if it helped you in some way.