PHP Programming Tutorial 1 - php codeigniter4 configuration in xampp or wamp
PHP Programming Tutorial 2 - CodeIgniter 4 Application Structure
PHP Programming Tutorial 3 - What is MVC in Codeigniter 4
PHP Programming Tutorial 4 - Codeigniter 4 Environment Variables
PHP Programming Tutorial 5 - Controllers in CodeIgniter 4
PHP Programming Tutorial 6 - Views in CodeIgniter 4
PHP Programming Tutorial 7 - Models in CodeIgniter 4
PHP Programming Tutorial 8 - Remove index.php from URL in CodeIgniter 4
PHP Programming Tutorial 9 - Include header and footer in CodeIgniter 4
PHP Programming Tutorial 10 - Session in CodeIgniter 4
PHP Programming Tutorial 11 - URI Routing in CodeIgniter 4
PHP Programming Tutorial 12 - Highlight Menu using URI Segment in CodeIgniter 4
PHP Programming Tutorial 13 - Connect to Database in CodeIgniter 4
PHP Programming Tutorial 14 - Insert data in Database using CodeIgniter 4
PHP Programming Tutorial 15 - Retrieve data from Database using CodeIgniter 4
PHP Programming Tutorial 16 - Update data in Database using CodeIgniter 4
PHP Programming Tutorial 17 - Delete data in Database using CodeIgniter 4
PHP Programming Tutorial 18 - Generate PDF in CodeIgniter 4
PHP Programming Tutorial 19 - Send Email using PHPMailer in CodeIgniter 4
PHP Programming Tutorial 20 - Upload a File in CodeIgniter 4
🔰 PHP Programming Tutorial 19 - Send Email using PHPMailer in CodeIgniter 4 🔰
In this example, I am going to show you how to send Email using PHPMailer in CodeIgniter. For sending Email in CodeIgniter, we use an external library PHPMailer. By using composer you can download this library into your project. You need to run following command in your project's root directory. Example: Here, we are using 3 files for generating PDF file: Open your browser and refer following link:
Overview
composer require phpmailer/phpmailer
Controller: Email.php app\Controllers\Email.php View: compose.php app\Views\compose.php
app/Config/Routes.php
$routes->add('email/compose', 'Email::compose'); $routes->post('email/send-email', 'Email::send_email');
app/Controllers/Email.php
<?php namespace App\Controllers; use App\Controllers\BaseController; use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; class Email extends BaseController { public function __construct() { } public function compose() { echo view('compose'); } public function send_email() { $email = $this->request->getPost('email'); $subject = $this->request->getPost('subject'); $message = $this->request->getPost('message'); $mail = new PHPMailer(true); try { $mail->isSMTP(); $mail->Host = 'host'; //smtp.google.com $mail->SMTPAuth = true; $mail->Username = 'username'; $mail->Password = 'password'; $mail->SMTPSecure = 'tls'; $mail->Port = 587; $mail->Subject = $subject; $mail->Body = $message; $mail->setFrom('username', 'display_name'); $mail->addAddress($email); $mail->isHTML(true); if(!$mail->send()) { echo "Something went wrong. Please try again."; } else { echo "Email sent successfully."; } } catch (Exception $e) { echo "Something went wrong. Please try again."; } } }
app/Views/compose.php
<form action="<?php echo base_url('email/send-email') ?>" class="form-horizontal" id="add_email_form" autocomplete="off" method="post" accept-charset="utf-8"> <div class="form-group"> <label for="inputName" class="col-sm-2 control-label">Recipient Email</label> <div class="col-sm-10"> <input type="hidden" name="id" value="9" id="id" /> <input type="hidden" name="source_id" value="2" id="source_id" /> <input type="text" name="email" value="" id="email" placeholder="Recipient Email Address" class="form-control" required /> </div> </div> <div class="form-group"> <label for="inputName" class="col-sm-2 control-label">Subject</label> <div class="col-sm-10"> <input type="text" name="subject" value="" id="subject" placeholder="Subject" class="form-control" required /> </div> </div> <div class="form-group"> <label for="inputName" class="col-sm-2 control-label">Message</label> <div class="col-sm-10"> <textarea class="form-control" name="message" id="message"></textarea> </div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="submit" class="btn btn-info">Send Email</button> </div> </div> </form>
How to run?
😉Subscribe and like for more videos:
https://www.youtube.com/@chiragstutorial
💛Don't forget to, 💘Follow, 💝Like, 💖Share 💙&, Comment
0 Comments
Leave a message