Install the App?

Sending email with PHPMailer

DESCRIPTION The application described in this section allows you to send an email using the PHPMailer API.

PREREQUISITE The ZnetDK for Mobile Starter Application is installed and configured (go to the Get Started page and follow the given procedure).

STEPS Only four steps are required:
  • Step 1: Installing PHPMailer,
  • Step 2: Coding the controller that sends an email,
  • Step 3: Coding the view that displays an email entry form,
  • Step 4: Adding a new item to the App's navigation menu.

DEMONSTRATION See live demo....

Step 1 - Installing PHPMailer

To add the PHPMailer API (version 6.5.1 at the time of writing this article) to your Starter Application:

  • Download PHPMailer as a ZIP file from the official github page,
  • Unzip the downloaded ZIP file content into the INSTALL_DIR/applications/default/app/ directory.
  • Rename the new INSTALL_DIR/applications/default/app/PHPMailer-master/ directory to INSTALL_DIR/applications/default/app/phpmailer/.
    The src/ subdirectory must exist into the new INSTALL_DIR/applications/default/app/phpmailer/ directory.

Step 2 - Coding the controller sendmailctrl.php

The PHP script sendmailctrl.php is responsible for sending by email the message entered in the HTML form implemented in the next step. It is created into the INSTALL_DIR/applications/default/app/controller/ directory.

  • The PHPMailer classes are loaded manually through the require PHP statement. The use statement eliminates the need to specify the namespace as prefix when instantiating a new PHPMailer object.
  • The custom app\controller\SendMailCtrl class is derived from the \AppController ZnetDK class to be identified as an application controller.
  • The action_send() method, a controller's action, is called on submit of the input form implemented in step 3.
  • The form values sent in HTTP through POST parameters are read using a ZnetDK \Request object.
  • To simplify the code shown below, the PHPMailer send() method uses the PHP's mail() function to send emails. However, it is strongly recommended to favor the use of the SMTP protocol to send emails (see PHPMailer documentation for SMTP implementation).
  • If an error occurs while sending an email, an error message is returned in the response of the HTTP request by calling the \Response::setFailedMessage() method.
  • Otherwise if the email is sent without error, a success message is returned by calling the \Response::setSuccessMessage() method.

Controller sendmailctrl.php

<?php
namespace app\controller;

use PHPMailer\PHPMailer\PHPMailer;

require 'app/phpmailer/src/Exception.php';
require 'app/phpmailer/src/PHPMailer.php';
require 'app/phpmailer/src/SMTP.php';

class SendMailCtrl extends \AppController {

    static protected function action_send() {
        $request = new \Request();
        $mail = new PHPMailer();
        $mail->setFrom($request->from);
        $mail->addAddress($request->to);
        $mail->Subject = $request->subject;
        $mail->Body = $request->body;
        $mail->AltBody = $request->body;
        $response = new \Response();
        if (!$mail->send()) {
            $response->setFailedMessage(NULL, 'Mailer Error: ' . $mail->ErrorInfo);
        } else {
            $response->setSuccessMessage(NULL, 'Email sent successfully.');
        }
        return $response;
    }

}
PHP

Step 3 - Coding the view sendmailview.php

The view sendmailview.php must be installed into the INSTALL_DIR/applications/default/app/view/ folder.

  • The input form id="sendmail-form" is declared with the data-zdk-submit attribute to specify the controller and the action to call on form submit.
  • To connect the input form to the remote PHP controller action specified via the the data-zdk-submit attribute, the ZnetDK method znetdkMobile.form.make() is called in JavaScript.
    So, when the submit button is clicked, the data entered in the form are sent in AJAX through POST parameters to the app\controller\SendMailCtrl::action_send() PHP method described in the previous chapter.

View sendmailview.php

<form id="sendmail-form" class="w3-container" data-zdk-submit="sendmailctrl:send">
    <div class="w3-section">
        <label class="zdk-required"><b>From</b></label>
        <input class="w3-input w3-border w3-margin-bottom" type="email" name="from" placeholder="Sender email" required>
        <label class="zdk-required"><b>Recipient</b></label>
        <input class="w3-input w3-border w3-margin-bottom" type="email" name="to" placeholder="Recipient email" required>
        <label class="zdk-required"><b>Subject</b></label>
        <input class="w3-input w3-border w3-margin-bottom" type="text" name="subject" placeholder="Subject of the message" required>
        <label class="zdk-required"><b>Body</b></label>
        <textarea class="w3-input w3-border" name="body" rows="4" placeholder="Body of the message"></textarea>
        <button class="w3-button w3-block w3-green w3-section w3-padding" type="submit">Submit</button>
    </div>
</form>
<!-- Input form initialization -->
<script>
    var myForm = znetdkMobile.form.make('#sendmail-form');
</script>
HTM5 JS

Step 4 - Adding a "Send mail" item to the navigation menu

Finally, to give users access to the sendmailview.php view, a menu item definition is added into the menu.php script of the Web App (see Get Started | New menu items for more information).

The menu.php script is located into the INSTALL_DIR/applications/default/app/ folder.

Script menu.php

static public function initAppMenuItems() {
    
    // ... Here, some menu items...
    
    \MenuManager::addMenuItem(NULL, 'sendmailview', 'Send mail', 'fa-paper-plane-o');
    
    // ... Here, other menu items...
    
}
PHP

Search

Sort order