In this tutorial, We will explain to you how to send email in CodeIgniter 4(Codeigniter 4 Send Email Example Tutorial). In this article, we are using the in-built email library of CodeIgniter 4.
Why Email is required, we are using it for sends new registered customers, forgot password, contact us, and invoices. Let us follow the below steps for send emails.
Overview
Step 1: Download Codeigniter
Step 2: Email Configurations
Step 3: Create Controller
Step 4: Create Routes
Step 5: Create Views Files
Step 6: Run The Application
Download Codeigniter
If you want to download or install CodeIgniter 4 then you can below Url.
How To Install Codeigniter 4 Using Manual, Composer, Git
Email configuration in Codeigniter
After the complete installation of Codeigniter 4. we have to mail configuration. now we will open the “app/Config/Email.php” file for Email Credentials for Gmail. See below changes in an Email.php file.
<?php namespace Config; use CodeIgniter\Config\BaseConfig; class Email extends BaseConfig { /** * @var string */ public $fromEmail; /** * @var string */ public $fromName; /** * @var string */ public $recipients; /** * The "user agent" * * @var string */ public $userAgent = 'CodeIgniter'; /** * The mail sending protocol: mail, sendmail, smtp * * @var string */ public $protocol = 'smtp'; /** * The server path to Sendmail. * * @var string */ public $mailPath = '/usr/sbin/sendmail'; /** * SMTP Server Address * * @var string */ public $SMTPHost = 'smtp.googlemail.com'; /** * SMTP Username * * @var string */ // Enter your email id from where you send email public $SMTPUser = 'Enter your email id'; /** * SMTP Password * * @var string */ // Enter your email's password public $SMTPPass = 'Enter your email's password'; /** * SMTP Port * * @var integer */ public $SMTPPort = 465; /** * SMTP Timeout (in seconds) * * @var integer */ public $SMTPTimeout = 60; /** * Enable persistent SMTP connections * * @var boolean */ public $SMTPKeepAlive = false; /** * SMTP Encryption. Either tls or ssl * * @var string */ public $SMTPCrypto = 'ssl'; /** * Enable word-wrap * * @var boolean */ public $wordWrap = true; /** * Character count to wrap at * * @var integer */ public $wrapChars = 76; /** * Type of mail, either 'text' or 'html' * * @var string */ public $mailType = 'html'; /** * Character set (utf-8, iso-8859-1, etc.) * * @var string */ public $charset = 'UTF-8'; /** * Whether to validate the email address * * @var boolean */ public $validate = false; /** * Email Priority. 1 = highest. 5 = lowest. 3 = normal * * @var integer */ public $priority = 3; /** * Newline character. (Use “\r\n” to comply with RFC 822) * * @var string */ public $CRLF = "\r\n"; /** * Newline character. (Use “\r\n” to comply with RFC 822) * * @var string */ public $newline = "\r\n"; /** * Enable BCC Batch Mode. * * @var boolean */ public $BCCBatchMode = false; /** * Number of emails in each BCC batch * * @var integer */ public $BCCBatchSize = 200; /** * Enable notify message from server * * @var boolean */ public $DSN = false; } ?>
Create Controller
In this step, first, we will create a “Contact.php” controller after that we will create an index() and sendmail() method in this controller. When you call to index method that time we will return the view files. when you call the sendMail() that time we will pass the form data and send the mail.
app/Controllers/Contact.php
<?php namespace App\Controllers; use App\Models\FormModel; use CodeIgniter\Controller; class Contact extends Controller { public function index() { return view('contact'); } function sendMail() { $to = $this->request->getVar('email'); $subject = $this->request->getVar('subject'); $message = $this->request->getVar('message'); $email = \Config\Services::email(); $email->setTo($to); $email->setFrom('xpertphp2018@gmail.com', 'Contact Us'); $email->setSubject($subject); $email->setMessage($message); if ($email->send()) { $msg = 'Email successfully sent'; } else { $data = $email->printDebugger(['headers']); //print_r($data); $msg ='Email send failed'; } return redirect()->to( base_url('contact') )->with('msg', $msg); } }
Create Routes
Add the following route code in the “app/config/Routes.php” file.
$routes->get('/contact', 'Contact::index');
Create Views Files
Finally, we will create a contact.php in the app/views directory. In this file, we will create the contact form using the bootstrap. for that you can see our example.
app/views/contact.php
<!DOCTYPE html> <html lang="en"> <head> <title>Codeigniter 4 Send Email Example Tutorial - XpertPhp</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <?php if (session('msg')) : ?> <div class="alert alert-info alert-dismissible"> <?= session('msg') ?> <button type="button" class="close" data-dismiss="alert"><span>×</span></button> </div> <?php endif ?> <form method="post" action="<?php echo base_url('contact/sendMail') ?>"> <div class="form-group"> <label>Your Email</label> <input type="text" name="email" class="form-control"> </div> <div class="form-group"> <label>Your Subject</label> <input type="text" name="subject" class="form-control"> </div> <div class="form-group"> <label>Message</label> <textarea rows="6" type="text" name="message" class="form-control"></textarea> </div> <button type="submit" class="btn btn-primary btn-block">Send</button> </form> </div> </body> </html>
Run The Application
We can start the server and run this example using the below command.
php spark serve
Now we will run our example using the below Url in the browser.
http://localhost:8080/contact