Mails

Templating

Mail Templating involves the use of template files to handle mails. These template files can contain series of placeholders that are used to obtain variables that are later expected to be injected or passed across to them. These template files can later be loaded, rendered and forwarded as mail using either the content() or the sendmail() method. When handling template files, there are few things to take note

  • Template files can be used to update default header configuration settings
  • Placeholders are supplied as double curly brackets along with a dollar sign i.e {{ $var }} where $var is the name of injected variable.
  • Variables can be injected as local or global variables. Local variables are injected using the inject() method

Configuring Headers - Template File
Aside from the setup() method, the template file also be used to configure headers. This is useful when handling html or txt files. The @ symbol is used to process this just as displayed below.
Sample: Template File With Headers
  <setup type="config">
    @site_name 	: 	{mysite}
    @site_mail 	: 	{mysite@osomething.com}
    @client_name: 	{user name}
    @client_mail: 	{user@something.com}
    @file 	: 	{[user/var/res/myfile.jpg] [myfile.php]}
    @file 	: 	{[myfile/var/filename.php]}
  </setup>

  
  <!DOCTYPE html>
  <html lang="en">
  <head>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Document</title>
  </head>
  <body>
      
  </body>
  </html>  
  
  
In the above, the setup data will be used as mail headers when forwarding a mail. Every parameter is just as similar to the $webmail parameters discussed in setup. However, when handling multiple files, multiple files should be placed in their own specific angle brackets within the curly brackets and separated only by a space just as relayed above. It is not encouraged to do this often.
{{ }} Template File - Placeholders

Placeholders are accesssed in 5 different ways:

  • As a injected local variable
  • As a global variable
  • As a post request
  • As a get request
  • As a post or get request
Sample: Mailer File

    // ...Define some default request variables
    $_GET['type'] = 'isGet';

    $_POST['method'] = 'isPost';
    
    //.. Define a global variable
    $globalvar = 'Foo';

    // Initialize mailer class
    $mailer = new Mailer;
    
    // Set or load the mailer configuration parameters
    $mailer->server('mail.server');

    $mailer->setup('mail.setup');

    //Inject some local variables
    $mailer->inject(['email' => 'foo@site.com']);

    Load template file
    $mail->sendmail('mail.template');

Sample: Template File
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Mail Template File</title> </head> <body> Hi there, this is a message from {{$globalvar}} This however is an injected local variable {{$email}} The type of this message is {{get:type}} The method of this message is {{post:method}} Choosing any request is as easy as {{method}} or {{type}} </body> </html>
The examples above shows how the placeholders can be used to access global, local or request variables.

Global variables are naturally accessed with {{$name}} where $name is the name of the global variable

Local variables are injected with inject() method and accessed with the similarly as the global variables. Global variables however overides local variables.

Post request values are accessed with the post keyword i.e {{post:key}}

where key is the post key

Get request values are accessed with the get keyword i.e {{get:key}}

where key is the get key

The get or post request values can be obtained generally as {{key}}. By default, this variable i.e key will be obtained depending on the current request. However, the get request has the higest power here. This means that if a key exists in post and equally get, then the get request key will be picked.