Elementary Laravel:
Mail

Laravel provides a clean and simple API over the populat SwiftMailer library, allowing you to quickly get started sending mail.



Introduction

Laravel 5.3 comes with a new feature called Mailables where each type of email sent by your application is represented as a "mailable" class. In the previous lesson we have hooked our contact form with validation and upon successful validation, presented the user with a "success" message. In this lesson we will simulate sending an actual email from our contact form.

Mail & Local Development

Improve your skills! Laravel comes with drivers for many local and cloud based services for sending emails. Check the documentation to see how to use a specific driver.

Since we are in the development phase in our application, we don't want to actually send emails to live email addresses. To avoid doing so we will use the Log Driver.

Go to your local .env file and set a key/value for MAIL_DRIVER=log. Comment out all other keys that start with MAIL_. By doing so, all emails sent from our application will be written in the log file and not actually sent.

Mailables

Mailabes are stored in app/Mail.

Generate a new Mailable class

To create a new Mailable enter this command:

php artisan make:mail FeedbackReceived

This command will create a new file app/Mail/FeedbackReceived.php.

This is the current state of the repository at this moment 0469b561877cd6eb80667264d10480ade0792b26.

There are a few things that we need to configure in our new Mailable class:

  • Sender
  • View
  • Data

Configuring the Sender

First, we need to configure who the email is going to be "from". We do that by setting the from method inside the build method of the FeedbackReceived class.

public function build()
{
    return $this
        ->from('you@company.com')
        ->view('emails.contact');
}

You can change the from field to anything you want or which represents your business.

Configuring the View

In the code above, we have configured the sender and specified which template should be used when rendering the email's contents. We will now create a blank template file as specified.

Create a new folder in resources/views called emails and inside it create a file called contact.blade.php. Place the following code inside:

<h1>Thank you for contacting us! Your message has been received.</h1>

Setting the Data

So far, we are only sending the generic confirmation message to the user who has submitted the contact form. It would be nice if we could address the user by his name and display the message that he has sent us.

To do so, we have to set public properties on our FeedbackReceived class for name and comment:

public $name;
public $comment;

public function __construct($name, $comment)
{
    $this->name = $name;
    $this->comment = $comment;
}

Once the data has been set to a public property, it will be automatically available in our view as a variable. Let's modify our view template to include these variables:

<h1>Thank you for contacting us {{ $name }}! Your message has been received.</h1>

<p>{{ $comment }}</p>

Sending Mail

To send the actual email that we have configured in the previous chapter, we have to open the file routes/web.php and replace the TODO comment in Route::post('contact') with the following:

Mail::to($request->get('email'))
    ->send(new FeedbackReceived($request->get('name'), $request->get('comment')));

Don't forget to add the use statements above the route:

use App\Mail\FeedbackReceived;
use Illuminate\Support\Facades\Mail;

Now if you populate the contact form with data that passes validation, the email will be logged in storage/logs/laravel.log.

Check that log file to see if the email is logged there.

This is the current state of the repository at this moment 300445c0dc40d7e3099ee5602acfc98979b9a85a.

Congratulations! This marks the completion of the series Elementary Laravel. Thank you for reading this far. Let me know what you think of this series in the comments bellow.

By completing this series you will have a basic "elementary" understanding on how to use Laravel to create simple websites. The code for this series is available on Github and you are more than welcome to fork it, improve it and contribute to it.

Credits

  • Cover image Christmas Trees created by maria

Comments