by Jairon Landa
In this tutorial, I will show you how to send an email by using SMTP with CodeIgniter 4. I assume you already have a basic CodeIgniter 4 framework. For testing purpose, we are using Mailtrap.io (https://mailtrap.io/). It is safe and easy to configure. It is free for new users who want to evaluate or intend to apply it on a personal side project.
Related
2. Basic CodeIgniter 4 CRUD with MySQL
Github: https://github.com/Jaironlanda/ci4-smtp-mailtrap
Register mailtrap.io account here: https://mailtrap.io/
Select Inboxes and click button Add Project.
Enter appropriate inbox name and click Save button.
Example: ci4-test
The new inbox is successfully created, next go to inbox settings to get dummy credentials and configuration.
Select SMTP settings tab, at credentials panel SMTP it shows Host, Port, Username, and Password, and more. We use this credential to test CodeIgniter 4 email SMTP.
We store email credential in .env
file. The reason why I am using .env
file because it is secure and easy to manage if you want to move your project to production mode.
Open your .env
file
Add this code at bottom .env
file
Note: you can give any variable name in
.env
file. More info here
Next, set up SMTP settings in .env
file.
Note: please use your credential that provided by mailtrap.io, this example just for demo purpose.
Example:
email_config_protocol = 'smtp'
email_config_SMTPHost = 'smtp.mailtrap.io'
email_config_SMTPPort = 2525
email_config_SMTPUser = '2f4297dbe06ffc'
email_config_SMTPPass = 'e3834c7c4ff539'
email_config_senderMail = '2f4297dbe06ffc'
email_config_senderName = 'Test Email'
Declare email Services in app/Controllers/BaseController.php
Create a Controller app/Controllers/Home.php
Your mailtrap should receive email from CodeIgniter.
You learn how to setup and configure CodeIgniter 4 SMTP email and test it with Mailtrap.io.
Backend and Frontend developer, a.k.a full-stack developer.
Kg Bundung, Tuaran