Explore a step-by-step guide on sending emails using the PHP Flight Framework. Learn how to integrate PHPMailer, set up SMTP, and enhance your web application’s functionality with this comprehensive email-sending example.

Start by installing the PHPMailer library—it’s the first thing you need to do.

The PHPMailer library is a comprehensive class for creating and sending emails in PHP.

composer require phpmailer/phpmailer

SMTP Sandbox Account for PHP Flight Framework

Test the mail function effortlessly with a free SMTP sandbox account from MailMug.net.

First, create an account then copy the credentials from the dashboard > settings page.

SMPT Sandbox account for php flight

Sending Emails with PHP Flight Sample code

<?php 

require 'vendor/autoload.php';

use PHPMailer\PHPMailer\PHPMailer;

class initMailer extends PHPMailer
{
    function __construct($host, $port, $username, $password, $from_name){
      parent::__construct();
      $this->isHTML(false);
      $this->isSMTP();
      $this->Host = $host;
      $this->SMTPAuth = true;
      $this->Username = $username;
      $this->Password = $password;
      $this->Port = $port;
      $this->From = $from_name;
      $this->FromName = $from_name;
    }
}

Flight::route('/test', function(){
  $email = Flight::smtpMailer(false);
  $email->SMTPDebug = 2;
  $email->AddAddress( 'to@mail.com', 'To Mail Owner Name' );
  $email->isHTML(true);
  $email->Subject = 'Some subject';
  $email->Body = '<strong>Hello</strong> World letter';
  var_dump($email->Send());
});

$options = [ 'smtp.mailmug.net', '2525', 'lz5ebosxdq0uruxb', 'kbs6m4hifn8di3gi', 'test@mailmug.net' ];
Flight::register('smtpMailer', 'initMailer', $options);
Flight::start();

Here initMailer class is helping to initialize SMTP credentials to the Mailer class. The Flight::register method allows to reinitialize the initMailer class.

Set $email->SMTPDebug = 0 to disable the debug data output.

By admin

Leave a Reply

Your email address will not be published. Required fields are marked *