How To Send Email Using Mailtrap In CodeIgniter 4

How To Send Email Using Mailtrap In CodeIgniter 4

Avatar photoPosted by

Good day my fellow dev, today we will show you how to send emails using mailtrap in CodeIgniter 4. Most web app has a sending email feature and during the development, there is always email testing, and that’s the mailtrap comes in. This tutorial will show you how to create an email and set up the mailtrap on your laravel app. Let’s have a few discussions.

We will be using CodeIgniter 4 in this tutorial. CodeIgniter is one of the most powerful PHP frameworks. It is an open-source web framework that is used for rapid web development. It is popular for its exceptional performance, small footprint, requires nearly zero configuration, thorough documentation, and many more. If you want to learn more about CodeIgniter just visit their documentation here.

Mailtrap is used as a dummy SMTP server, it catches all the test emails that are being sent and can be viewed on its virtual inboxes. With mailtrap, email testing is safer and more manageable and avoids spamming real customers.

Step 1: Install CodeIgniter 4

For us to install CodeIgniter 4 we can install via composer or directly download CodeIgniter 4 here:

Install via composer:

composer create-project codeigniter4/appstarter ci-4-mailtrap

Step 2: Change CodeIgniter Environment

The default environment of CodeIgniter is production, it is a safety feature to add security in case settings are messed up when it goes live. For us to change the environment we will rename or copy the file env to .env. Once it is renamed, open the file and uncomment and change the CI_ENVIRONMENT value from production to development, and then register an account in mailtrap and copy the username and password:

image 13 Binaryboxtuts
CI_ENVIRONMENT = development
email.protocol = 'smtp'
email.SMTPHost = ''
email.SMTPPort = 2525
email.SMTPUser = '********'
email.SMTPPass = '********'
email.SMTPCrypto = 'tls'
email.fromEmail = ''

Step 3: Create A Controller

A Controller is the one responsible for receiving Request and returning Response. We will now create our controller, run this command to create a controller:

php spark make:controller SendEmailController

After creating a controller add this method:


namespace App\Controllers;

use App\Controllers\BaseController;

class SendEmailController extends BaseController
    public function index()
        $email = \Config\Services::email();
        $email->setSubject('Email Test');
        $email->setMessage('A sample email using mailtrap.');

Step 4: Register Route

Now we will be registering the route for merging the PDF files. Open the file /app/Config/Routes.php and update the routes:


$routes->get('/send-email', 'SendEmailController::index');

Step 5: Run the Application

Now that we have completed the steps above we will now run the app. To run the app, execute this command:

	php spark serve

After successfully running your app, open this URL in your browser: