kernel
[ class tree: kernel ] [ index: kernel ] [ all elements ]

Source for file BitMailer.php

Documentation is available at BitMailer.php

  1. <?php
  2.  
  3. /**
  4.  * @version $Header$
  5.  *
  6.  *  +----------------------------------------------------------------------+
  7.  *  | Copyright ( c ) 2008, bitweaver.org
  8.  *  +----------------------------------------------------------------------+
  9.  *  | All Rights Reserved. See below for details and a complete
  10.  *  | list of authors.
  11.  *  | Licensed under the GNU LESSER GENERAL PUBLIC LICENSE.
  12.  *  | See http://www.gnu.org/copyleft/lesser.html for details
  13.  *  |
  14.  *  | For comments, please use phpdocu.sourceforge.net standards!!!
  15.  *  | -> see http://phpdocu.sourceforge.net/
  16.  *  +----------------------------------------------------------------------+
  17.  *  | Authors: nick <nick@sluggardy.net>
  18.  *  +----------------------------------------------------------------------+
  19.  *
  20.  *  BitMailer class
  21.  *
  22.  *  This is a base class to derive more capabale mailing services
  23.  *
  24.  * @author   nick <nick@sluggardy.net>
  25.  * @version  $Revision$
  26.  * @package  kernel
  27.  */
  28.  
  29. /**
  30.  * Initialization
  31.  */
  32. require_once( LIBERTY_PKG_PATH . 'LibertyBase.php' );
  33.  
  34. /**
  35.  * BitMailer
  36.  * 
  37.  * @package  kernel
  38.  */
  39.  class BitMailer extends LibertyBase {
  40.     /**
  41.      * Sends an email to the specified recipients.
  42.      * This is a convenience method for packages
  43.      * to be able to use the email sending features
  44.      * found in this package.
  45.      *
  46.      * $pSubject - The Subject of the Email
  47.      * $pBody - The Body of the Email
  48.      * $pRecipients - An associative array with keys for email and optionally login and real_name
  49.      **/
  50.  
  51.     function sendEmail($pSubject, $pBody, $pRecipients, $pHeaders=array() ){
  52.         global $gBitSystem;
  53.         $message = $pHeaders;
  54.         $message['subject'] = $pSubject;
  55.         if( is_string( $pBody ) ){
  56.             $message['message'] = $pBody;
  57.         }elseif( is_array( $pBody ) ){
  58.             $message = array_merge( $message, $pBody );
  59.         }
  60.         $mailer = $this->buildMailer($message);
  61.  
  62.         if( is_string( $pRecipients ) ) {
  63.             $pRecipients = array( array( 'email' => $pRecipients ) );
  64.         }
  65.  
  66.         foreach ($pRecipients as $to) {
  67.             if( !empty($to['email'] ) ) {
  68.                 if (isset($to['real_name']) || isset($to['login'])) {
  69.                     $mailer->AddAddress( $to['email'], empty($to['real_name']) ? $to['login'] : $to['real_name'] );
  70.                 } else {
  71.                     $mailer->AddAddress( $to['email'] );
  72.                 }
  73.                 if( !$mailer->Send() ) {
  74.                     bit_error_log( $mailer->ErrorInfo );
  75.                 }
  76.                 $mailer->ClearAddresses();
  77.             }
  78.         }
  79.         return $mailer->MessageID;
  80.     }
  81.  
  82.     /**
  83.      * Returns a PHPMailer with everything set except the recipients
  84.      *
  85.      * $pMessage['subject'] - The subject
  86.      * $pMessage['message'] - The HTML body of the message
  87.      * $pMessage['alt_message'] - The Non HTML body of the message
  88.      */
  89.     function buildMailer($pMessage) {
  90.         global $gBitSystem, $gBitLanguage;
  91.  
  92.         require_once( UTIL_PKG_PATH.'phpmailer/class.phpmailer.php' );
  93.  
  94.         $mailer = new PHPMailer();
  95.         $mailer->From     = !empty( $pMessage['from'] ) ? $pMessage['from'] : $gBitSystem->getConfig( 'bitmailer_sender_email', $gBitSystem->getConfig( 'site_sender_email', $_SERVER['SERVER_ADMIN'] ) );
  96.         $mailer->FromName = !empty( $pMessage['from_name'] ) ? $pMessage['from_name'] : $gBitSystem->getConfig( 'bitmailer_from', $gBitSystem->getConfig( 'site_title' ) );
  97.         if( !empty( $pMessage['sender'] ) ) {
  98.             $mailer->Sender = $pMessage['sender'];
  99.         }
  100.         $mailer->Host     = $gBitSystem->getConfig( 'bitmailer_servers', $gBitSystem->getConfig( 'kernel_server_name', '127.0.0.1' ) );
  101.         $mailer->Mailer   = $gBitSystem->getConfig( 'bitmailer_protocol', 'smtp' ); // Alternative to IsSMTP()
  102.         $mailer->CharSet  = 'UTF-8';
  103.         if( $gBitSystem->getConfig( 'bitmailer_smtp_username' ) ) {
  104.             $mailer->SMTPAuth = TRUE;
  105.             $mailer->Username = $gBitSystem->getConfig( 'bitmailer_smtp_username' );
  106.         }
  107.         if( $gBitSystem->getConfig( 'bitmailer_smtp_password' ) ) {
  108.             $mailer->Password = $gBitSystem->getConfig( 'bitmailer_smtp_password' );
  109.         }
  110.         $mailer->WordWrap = $gBitSystem->getConfig( 'bitmailer_word_wrap', 75 );
  111.         if( !$mailer->SetLanguage( $gBitLanguage->getLanguage(), UTIL_PKG_PATH.'phpmailer/language/' ) ) {
  112.             $mailer->SetLanguage( 'en' );
  113.         }
  114.  
  115.         if( !empty( $pMessage['x_headers'] ) && is_array( $pMessage['x_headers'] ) ) {
  116.             foreach( $pMessage['x_headers'] as $name=>$value ) {
  117.                 /* Not sure what this is intended to do
  118.                    but nothing seems to use it yet but boards
  119.                    that I am hacking on now. 29-11-08
  120.                    XOXO - Nick
  121.                 if( !$mailer->set( $name, $value ) ) {
  122.                     $mailer->$name = $value;
  123.                     bit_error_log( $mailer->ErrorInfo );
  124.                 }
  125.                 */
  126.                 $mailer->AddCustomHeader($name.":".$value);
  127.             }
  128.         }
  129.  
  130.         $mailer->ClearReplyTos();
  131.         $mailer->AddReplyTo( $gBitSystem->getConfig( 'bitmailer_from' ) );
  132.         if (empty($pMessage['subject'])) {
  133.             $mailer->Subject = $gBitSystem->getConfig('site_title', '').
  134.                 (empty($pMessage['package']) ? '' : " : ".$pMessage['package']).
  135.                 (empty($pMessage['type']) ? '' : " : ".$pMessage['type']);
  136.         }
  137.         else {
  138.             $mailer->Subject = $pMessage['subject'];
  139.         }
  140.  
  141.         if (!empty($pMessage['message'])) {
  142.             $mailer->Body    = $pMessage['message'];
  143.             $mailer->IsHTML( TRUE );
  144.             if (!empty($pMessage['alt_message'])) {
  145.                 $mailer->AltBody = $pMessage['alt_message'];
  146.             }
  147.             else {
  148.                 $mailer->AltBody = '';
  149.             }
  150.         }
  151.         elseif (!empty($pMessage['alt_message'])) {
  152.             $mailer->Body = $pMessage['alt_message'];
  153.             $mailer->IsHTML( FALSE );
  154.         }
  155.  
  156.         return $mailer;
  157.     }
  158.  
  159.  
  160. }
  161.  
  162. ?>

Documentation generated on Wed, 29 Jul 2015 13:55:53 +0000 by phpDocumentor 1.5.0-lsces