Laravel 9 — How to accept a file from a form and email it as an attachment

Davide Cariola
4 min readApr 1, 2022

Let’s take a platform that accepts job applications as a case study.

A user can send their data through a contact form and insert their CV, in a PDF format.
This PDF will then be delivered to a Human Resources officer so that a first screening can be carried out.

For this case study let’s assume we already have a Laravel 9 project with Bootstrap 5 scaffolding, togheter with a PublicController.

First step, let’s create a GET-type route that allows to display the contact form:

Let’s then manage the careers function in the PublicController:

Now let’s manage the view instead, creating a .blade.php file in resources\ views\ called careers where to manage a form, in addition to the rest of the frontend:

Perfect! Now we have our form!

A couple of notes for this:
• enctype=”multipart/form-data” is needed to allow the form to accept multimedia data;
• through the blade directive @error, we can manage the displaying of the validation errors of our form;
• with value=“{{old (‘input_name’)}}”, we can save the user entered data in the session, so that the user is not forced to re-enter everything if a validation error occurs.

Now that we’ve handled this part, let’s focus on our form action.

First of all, we want the request to respect certain rules: we must therefore create our custom Request:

This command will create a file called CvRequest.php in App\Http\Requests, which we have now to customize:

We are saying that all the fields are necessary, we have also inserted some rules for the strings sizes, but above all we have said that only pdf files can be attached.

We can go forth.

We know that after submitting our form, an email must be sent. So let’s go create it:

This command will create a Mailable object in App\Mail called CvMail.php, which we will now proceed to customize:

What are we doing?

We are saving the user inserted data in a public property and we are describing how our CvMail class object should be constructed using the __construct() function.

Then, in the build() function, we are going to specify that each CvMail class object must refer to a view called cv-email.blade.php which will be found in resources\views\mail. Mostly important, we’re stating that ther will be an attachment that our system will have to recover from storage: for this, we’re passing the path leading to the file to the attachFromStorage() function.

Now, let’s create the .blade.php file for the email. Just keep it simple for the sake of the tutorial:

Now that the structure of our email is also ready, let’s go to create a POST-type route in web.php:

Let’s go now to manage the function.

First of all, it must collect all the data of the user who filled out the form; then we must save the CV in our storage; then send an email to our HR.

Let’s do it step by step:

Test time: remember to connect a service such as mailtrap to our project, in the .env file.

We’re ready! Let’s simulate a user who sends us his CV.

And here is our attachment, which an HR can download without problems! Good luck for your job search, dear John Doe!

Let me know what you think! Follow me here on Medium for more Laravel tutorials and various tech-talk, on LinkedIn and on my site (soon also in english).

--

--

Davide Cariola

Backend and Laravel Specialist @ Aulab | Scrum Fundamentals Certified™ — follow me at davidecariola.it