Mails : authorize

authorizing mails


The authorize() method either prevents or allows a mail to be sent. When set to true, mail will be allowed for forwarding. This can be useful when trying to preview the mail content output expected to be forwarded as mail.

Some few examples below provides a better insight to how this is done.

Sample: Authorizing mails

    <?php

      $mailer = new Mailer;

      ... //other codes here
      
      $mailer->content('mail.template'); // can be defined in $mailer->sendmail()  
      
      $mailer->authorize(true);
      
      if($mailer->authorized()){

          $mailer->sendmail(); // returns true if configuration parameters are set and mail attempted       

      }
      
    
Note: when authorized, the sendmail() returns true only if the mail is successfully configured and an attempt is made to forward the mail. However, when authorize(false) is used, the sendmail returns true only if the configuration parameters are set. The example below reveals this.
Sample: Preventing mail forwarding

    <?php

      $mailer = new Mailer;

      ... //other codes here
      
      $mailer->content('mail.template'); // can be defined in $mailer->sendmail()  
      
      $mailer->authorize(false);
      
      if($mailer->sendmail()){
      
        // run this block if parameters are configured
        // note: no attempt is ever made here to send mails 
             
      }
    
The sendmail() method is not a true way of checking if mail is successfully sent. This should be handled using the sent() method. The authorize() method can be harvested to only send mails online by using the online constant which returns true on live environment. An example is shown below:
Sample: Authorizing mails in live environment

    <?php

      $mailer = new Mailer;

      ... //other codes here
      
      $mailer->authorize(online);
      
      if($mailer->authorized()){

          $mailer->sendmail('mail.template'); // attempt to send mail  

      } else {
      
          // oops! mail can only be forwarded online
               
      }