Send email via Gmail SMTP using PHPMailer and Codeigniter 4
PHPMailer is the most useful library in PHP for send secure SMTP email from server. So every project must needs this library to send email from SMTP. In this tutorial we learn how to send email using Gmail SMTP with help of PHPmailer Codeigniter 4 and fix problem while using this library. so lets start.
- Install PHPMailer with composer
- Setting in Google account for send email
- write code for sending email with PHPMailer
1. Install PHPMailer with composer :
You need to run following command in your project’s root directory.
1 |
composer require phpmailer/phpmailer |
2. Setting in gmail account before sending email
- Firstly you need to do some security settings in Gmail account so you will go in Google Account Settings.
- Disable 2-step-verification
- Go to Myaccount -> security tab
- Turn on “Less secure app”
Now your google account is ready to sending SMTP email
3. Write code for sending email with phpmailer
Create the controller name with Home.php under app/Controllers/Home.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
<?php namespace App\Controllers; use App\Controllers\BaseController; use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; class Home extends BaseController { public function __construct(){} public function index(){ echo view('email'); } function send_mail($email,$subject,$message) { $email = $this->request->getVar('mailTo'); $subject = $this->request->getVar('subject'); $message = $this->request->getVar('message'); $mail = new PHPMailer(true); try { $mail->isSMTP(); //$mail->SMTPDebug = 2; $mail->Host = 'smtp.gmail.com'; // Specify main and backup server $mail->SMTPAuth = true; // Enable SMTP authentication $mail->Username = 'gmailaccount@gmail.com'; // SMTP username $mail->Password = 'XXXXXX'; $mail->SMTPSecure = 'tls'; $mail->Port = 25; $mail->Subject = $subject; $mail->Body = $message; $mail->SMTPOptions = array( 'ssl' => array( 'verify_peer' => false, 'verify_peer_name' => false, 'allow_self_signed' => true ) ); $mail->setFrom('gmailaccount@gmail.com', 'Your Name'); $mail->addAddress($email); $mail->isHTML(true); if(!$mail->send()) { return false; } else { return true; } } catch (Exception $e) { return "Message could not be sent. Mailer Error: {$mail->ErrorInfo}"; } } } |
app/Views/email.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<form action="<?php echo base_url('home/send_mail') ?>" 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="text" name="mailTo" value="" id="mailTo" 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> |
Now run your project with this command in your terminal:
1 |
php spark serve |
Open your browser and open this link:
http://localhost:8080
Thanks for visiting. If you have any problem or any of query you can contact feel free.