In Laravel, sending multiple emails and attaching several documents is one of the key features in every application. Being able to write several lines of code and be able to send an email, has its own satisfaction. Well, Laravel just like any other framework today can do so. My aim in this project was to emulate what any email service provider today can do. For instance, be able to send an email, carbon copy (CC), have a subject and body, and attach several documents regardless of the type of the file (PDF, Image, SVG’s, etc).
See here to view my code. Image from cloudways.com.
- Composer – is a tool for dependency management in PHP. It is a great tool to have mostly since you can do a lot with it
- Git – Git is a free and open-source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. If you are using Windows git will help you like when you right-click you can open the terminal on any directory.
- Laravel – Laravel is a web application framework with an expressive, elegant syntax. They’ve already laid the foundation — freeing you to create without sweating the small things.
- PHP – PHP is a popular general-purpose scripting language especially suited for web development. Which is fast, flexible, and pragmatic. So, PHP powers everything from your blog to the most popular websites in the world.
- XAMPP – XAMPP is a completely free, easy to install Apache distribution containing MariaDB, PHP, and Perl. Because XAMPP is an open source package, is set up to be incredibly easy to install and to use.
- Browser – a computer program with a graphical user interface for displaying HTML files, it is used to navigate the World Wide Web.
NOTE: For this tutorial, I used a Xampp web server.
Step by Step Procedure
Laravel has its own server shipped with it. Since we are going to save all emails sent, we require a local server. Hence there is clear documentation online on how to install the above-mentioned software on their official sites depending on the operating system that you are using.
To check if you have installed xampp on:
Windows – Clicking the start button and search “XAMPP” xampp control panel is supposed to appear from your search history.
Linux – Run the following command. This command starts xampp web server
sudo /opt/lampp/lampp start
To check if you have composer installed, open your terminal and run the command, composer. The terminal outputs the following
Once done with the setup, we will be able to code our small app sending multiple emails with attachments using laravel. Sending an email from your own application is always interesting but for beginners, it’s always a real pain. For beginners, they need to figure out what to loop, configuration, and all that stuff.
Step 1: Installing Laravel
To install laravel just run the following command. projectName is the name of the project/folder.
composer create-project laravel/laravel projectName
Once laravel has been installed you can change the directory to the project folder. To access the newly installed laravel run the following command
php artisan serve
Another way to access is by moving your project to the htdocs folder.
Step 2: Creating a Model, Migration and Controller.
To create the above files you only need to run the below command. Once created, we are going to start with the controller.
php artisan make:model Attachment -mc
Step 3: Creating Database and Updating Migration
Before we get started we need to create a database and update our .env file. Once you are done, head over to the database/migration and open your newly created migration. Update it as follows:
Once done run the following command
php artisan migrate.
This creates all unmigrated migrations and pushes them to your database.
Step 4: Updating already created controller.
Once done head over to your controllers. Our first method inside our controller will be the one to access our view file. So it will look as follows
In the above method, we are returning the view welcome (which is shipped with laravel). The next step is to create a send method. Its code will be as follows. Since we are saving all emails send, the index method is returning our view file, all users saved and emails stored.
The send method is the one I am using to send emails and store the data. First I am validating all inputs. Then I save the data. I wanted to count all emails that are being sent, and that is where the count is coming in. For each loop, it’s counting one. It will count all emails in the cc input. I had set the count to be one because of the primary email.
After saving, we are now able to send emails. The view()->share() we are making the variable $emailData available globally. In Laravel, we use the Mail::send function to send emails. To be able to achieve our main goal which is to send all emails with attachments that have been selected, the most important part is to make sure to loop through all files. You might encounter an error when trying to send. I had previously written a blog about how to address that error. In case you encounter click this link. The model file is a connection to your migration. In ORM(object-relational mapper), each model represents a table which we call migrations.
Step 5: Updating .env File
Your .env file is supposed to look as follows. We are mostly updating settings for mail. I have used google mail to do the task. For Google to be able to send email from a third party, you need to authorize it. To do so, on your Gmail profile and click manage your google account, head over to security, and then turn on less secure app access.
Step 6: Updating our welcome view file.
On your welcome.blade.php file update it as follows.
Once done, create a mails.blade.php file since we require it to send to be able to send the emails. This view file will be just a simple HTML file. you can customize it to your liking though.
Step 7: Routes
Update your routes as follows. Note that, laravel 8 routes are very different from other versions of laravel
Step 8: Serving our application.
To serve Laravel just run the following command
php artisan serve
This creates a default port 8000 and creates a clickable link for you which is http://localhost:8000/. Once you click the link you will be able to access our application. It is supposed to look as follows.
Now you can send emails. Make sure your user’s table has some valid emails.
There are a lot of learning tools you can use to equip you which may include;
- Laravel Docs – The no 1 website stuck you supposed to reference first before searching elsewhere.
- PHP – For any PHP related problem always refer here
- Stack Overflow – This is a community of developers, you can find some answers and some may never work for you but its a good place to check out.
To be able to complete this blog project, I used the above tools. I recommend the use of google search to the maximum level. If by any chance you’re experiencing errors it is okay to ask for help from colleagues or developers communities around the globe.
Sending emails using laravel can sometimes be challenging if you’re a beginner. Once you completely understand how to do it, things become easier, and you become less of a beginner. If you are building that for production I would recommend you use cron jobs or queues. For more information, you can read on it on Laravel docs.
The above processes will show you how to attach multiple attachments, and carbon copy emails when sending an email to several users at once.