Laravel Mailgun Setup
In this section, we are going to learn about Mailgun setup. We will use Laravel to do this. When we develop any application, several times, we require sending emails to users. We should know that without mail functionality, 90% of applications cannot work. So it is important for us to understand the integration of this functionality with the mobile application or web. This application will see the integration of email sending, Mailgun setup for Laravel, generation of Mailgun API key, and many more things.
Mailgun is a type of email delivery service, which provides an easy API for developers. Using this API, we can easily send out transactional emails from their website. The core of Laravel already contains the Mailgun service. In order to send the email from the website, the famous API is Mailgun will be used. Using Mailgun, we can very easily and quickly send an email, and we can also track that mail. The Mailgun API has a very important feature that is tracking email. Using this API, we are able to see the number of clicks on our mail and the number of people who open our mail. Just like a gun, a mailgun sends the mails. Mailgun has a very cool feature that is it is able to freely send 10,000 emails every month. Mailgun is used to send the emails by using their API or using the SMTP server. In our application, we will use Mailgun API to send the emails. As compare to configuration SMTP, this API is faster, scales better, and its setup takes less work.
In our Laravel application, we will see the setting of Mailgun. Here we will use Mailgun API to send the simple mail. If we are sending our email by using the Mailgun, we are going to get mail fast and save our loading time. For this, we will add configuration to it. For this, we will add the configuration of my Gmail account. Now we will use a file named .env, and then we will add the following code into it like this:
After that, we will add mailgun api configuration secret and domain. If we have not already signup into our mailgun account, we will use mailgun.com SignUp to create a new account. When we successfully completed registration, we will activate our mailgun account. Now we will click on the Domain button, and after that, we will click on a button named Add New Domain. After clicking on this button, we will see the following screen like this:
When we completely add the above Domain name, we will see the following screen. Now we will copy API Key and domain name, which is described as follows:
After this, we will open services.php and then we will add Mailgun configuration into it like this:
Now we have to test it by sending the mail. So we will first send an email by creating the test route like this:
After that, we will use a file named HomeController.php and add mail function into it like this:
Lastly, we will send an email by creating the email template file. Now, we will use our emails folder and create an email template file named mailEvent.blade.php like this:
The email template in Laravel will look like this: