Laravel with Mailtrap.io and Sendgrid to send emails.
Send emails with Laravel is very easy with mailables, it has many uses like contact forms, to confirm subscriptions, for newsletters, to send reports, ect.
In this article I want to talk about different options to create Mailables and how to use it in real world with Sendgrid free plan service.
Create a Mailable
We can create a View with a form to get data from the form request and send it as a “Contact form”, in the folder resources/views we can create a file called “contact.blade.php”, inside that file we can create a classic contact form structure:
Add the routes
We need to add a route to load the form and other route of type “POST” to process the data from the form request, add in routes/web.php
Create a view for the contact form.
I use a “view” method to go directly to the contact form, and we need to create a controller for “ContactController” to handle the request, in terminal we can create a controller with the make controller command as a resource method
Edit the ContactController.php
What we do in the “ContactController”?
The request and validate the data with helper “validate()”, then we add the custom validation rules and use a “Mail” facades with method “to()”, and add as first param the mail to send the email and a chain method with “send()” method or in this case I use “queue()” method, this method works with a special configuration, but in every case you can use “send()” or “queue()” as you want.
The send or queue methods needs a mailable class as param, and if the request pass the validation and Mail is sent, the method returns a string “Email send”.
Edit the mailable
Now we have a mailable created with the artisan command to create a mail, we can edit the file in “App/Mail” directory
In “build()” method we can add “replyTo” or “subject” by adding values to properties “$this->replyTo()” or “$this->subject()”, in this case I change the public property “$subject” changing it value and in build method I access to public property “$this->replyTo” but you can use both ways, you can change the property value in build method, in constructor or outside the methods.
I create a public property “public $content” and in constructor I give a value to the property, I made it in this way to pass content variable as data to mailable class when mailable class is initialized, like “new MailableClass( $content )”.
Then we need to return a view to handle the design of the email, in this case I create a contact-form email view file in “resources/views/emails/contact-form.blade.php” directory
Now we have the email template to send, we only need to configure Mail settings to send the email, at this point we have 3 options.
- Set email for Logs to get a preview.
- Set email for development ( in this case I prefer mailtrap ).
- Set email for production (I will use sendgrid).
Set email for logs
To add settings for emails Laravel provides an “.env” file, that we need to edit
Now you can access to your browser in “yourproject/contact”, you would access to your contact form view and then you need to filled with data and submit, now you can go to “storage/logs” directory and you will find log file, in very bottom of the file you can see a preview in code of your email
Set email for Develpment with Mailtrap.io
you only need to add “smtp” as driver, and add your username and password of mailtrap.io you can create a free account, and try again to submit another contact form.
If everything is correct you can access to your mailtrap.io inbox and you will see something like:
You can now use mailtrap to set all your emails for development.
Set email for Production
First we need to install a package https://github.com/s-ichikawa/laravel-sendgrid-driver to install it you only need the next command
When the package is installed, we need to add the service in “config/service.php” directory
So with the changes the service.php file looks like
Now we need to change the “.env” file again to add the SendGrid Values, you can sign in with a free account in https://app.sendgrid.com and get 100 monthly emails for free or get a payment plan for more.
The username and password to “signin” or “register” are going to be used in “.env” file, and to generate an “apikey” you can go to settings on sidebar and click on “API keys” option, then press the blue button “Create API Key”, you can set any name to your api key, and in “API key permissions” by default is selected “Full access” so create it with “Full access”
Edit “.env” file with the next values:
MAIL_PORT will change from “25” or “587” for unencrypted/TLS connections and 465 for SSL connections.
Now you need to save the mail settings and try again in your browser to submit the contact form, if everything is ok, you can go to your email account and see the mail, if you do not see anything, go to “SPAM” section and you will find it, just mark this email as “Not Spam” to get the email and next emails to inbox.