[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/vtlib/Vtiger/ -> Mailer.php (source)

   1  <?php
   2  /*+**********************************************************************************
   3   * The contents of this file are subject to the vtiger CRM Public License Version 1.0
   4   * ("License"); You may not use this file except in compliance with the License
   5   * The Original Code is:  vtiger CRM Open Source
   6   * The Initial Developer of the Original Code is vtiger.
   7   * Portions created by vtiger are Copyright (C) vtiger.
   8   * All Rights Reserved.
   9   ************************************************************************************/
  10  require_once ('modules/Emails/class.phpmailer.php');
  11  include_once ('include/utils/CommonUtils.php');
  12  include_once ('config.inc.php');
  13  include_once ('include/database/PearDatabase.php');
  14  include_once ('vtlib/Vtiger/Utils.php');
  15  include_once ('vtlib/Vtiger/Event.php');
  16  
  17  /**
  18   * Provides API to work with PHPMailer & Email Templates
  19   * @package vtlib
  20   */
  21  class Vtiger_Mailer extends PHPMailer {
  22  
  23      var $_serverConfigured = false;
  24  
  25      /**
  26       * Constructor
  27       */
  28  	function __construct() {
  29          $this->initialize();
  30      }
  31  
  32      /**
  33       * Get the unique id for insertion
  34       * @access private
  35       */
  36  	function __getUniqueId() {
  37          global $adb;
  38          return $adb->getUniqueID('vtiger_mailer_queue');
  39      }
  40  
  41      /**
  42       * Initialize this instance
  43       * @access private
  44       */
  45  	function initialize() {
  46          $this->IsSMTP();
  47  
  48          global $adb;
  49          $result = $adb->pquery("SELECT * FROM vtiger_systems WHERE server_type=?", Array('email'));
  50          if($adb->num_rows($result)) {
  51              $this->Host = $adb->query_result($result, 0, 'server');
  52              $this->Username = decode_html($adb->query_result($result, 0, 'server_username'));
  53              $this->Password = decode_html($adb->query_result($result, 0, 'server_password'));
  54              $this->SMTPAuth = $adb->query_result($result, 0, 'smtp_auth');
  55              
  56              // To support TLS
  57              $hostinfo = explode("://", $this->Host);
  58              $smtpsecure = $hostinfo[0];
  59              if($smtpsecure == 'tls'){
  60                  $this->SMTPSecure = $smtpsecure;
  61                  $this->Host = $hostinfo[1];
  62              }
  63              // End
  64              
  65              if(empty($this->SMTPAuth)) $this->SMTPAuth = false;
  66  
  67              $this->ConfigSenderInfo($adb->query_result($result, 0, 'from_email_field'));
  68  
  69              $this->_serverConfigured = true;
  70  //            $this->Sender= getReturnPath($this->Host);
  71          }
  72      }
  73  
  74      /**
  75       * Reinitialize this instance for use
  76       * @access private
  77       */
  78  	function reinitialize() {
  79          $this->ClearAllRecipients();
  80          $this->ClearReplyTos();
  81          $this->Body = '';
  82          $this->Subject ='';
  83          $this->ClearAttachments();
  84      }
  85  
  86      /**
  87       * Initialize this instance using mail template
  88       * @access private
  89       */
  90  	function initFromTemplate($emailtemplate) {
  91          global $adb;
  92          $result = $adb->pquery("SELECT * from vtiger_emailtemplates WHERE templatename=? AND foldername=?",
  93              Array($emailtemplate, 'Public'));
  94          if($adb->num_rows($result)) {
  95              $this->IsHTML(true);
  96              $usesubject = $adb->query_result($result, 0, 'subject');
  97              $usebody = decode_html($adb->query_result($result, 0, 'body'));
  98  
  99              $this->Subject = $usesubject;
 100              $this->Body    = $usebody;
 101              return true;
 102          }
 103          return false;
 104      }
 105      /**
 106      *Adding signature to mail
 107      */
 108  	function addSignature($userId) {
 109          global $adb;
 110          $sign = nl2br($adb->query_result($adb->pquery("select signature from vtiger_users where id=?", array($userId)),0,"signature"));
 111          $this->Signature = $sign;
 112      }
 113  
 114  
 115      /**
 116       * Configure sender information
 117       */
 118  	function ConfigSenderInfo($fromemail, $fromname='', $replyto='') {
 119          if(empty($fromname)) $fromname = $fromemail;
 120  
 121          $this->From = $fromemail;
 122          //fix for (http://trac.vtiger.com/cgi-bin/trac.cgi/ticket/8001)
 123                  $this->FromName = decode_html($fromname); 
 124          $this->AddReplyTo($replyto);
 125      }
 126  
 127      /**
 128       * Overriding default send
 129       */
 130  	function Send($sync=false, $linktoid=false) {
 131          if(!$this->_serverConfigured) return;
 132  
 133          if($sync) return parent::Send();
 134  
 135          $this->__AddToQueue($linktoid);
 136          return true;
 137      }
 138  
 139      /**
 140       * Send mail using the email template
 141       * @param String Recipient email
 142       * @param String Recipient name
 143       * @param String vtiger CRM Email template name to use
 144       */
 145  	function SendTo($toemail, $toname='', $emailtemplate=false, $linktoid=false, $sync=false) {
 146          if(empty($toname)) $toname = $toemail;
 147          $this->AddAddress($toemail, $toname);
 148          if($emailtemplate) $this->initFromTemplate($emailtemplate);
 149          return $this->Send($sync, $linktoid);
 150      }
 151  
 152      /** Mail Queue **/
 153      // Check if this instance is initialized.
 154      var $_queueinitialized = false;
 155  	function __initializeQueue() {
 156          if(!$this->_queueinitialized) {
 157              if(!Vtiger_Utils::CheckTable('vtiger_mailer_queue')) {
 158                  Vtiger_Utils::CreateTable('vtiger_mailer_queue',
 159                      '(id INT NOT NULL PRIMARY KEY,
 160                      fromname VARCHAR(100), fromemail VARCHAR(100),
 161                      mailer VARCHAR(10), content_type VARCHAR(15), subject VARCHAR(999), body TEXT, relcrmid INT,
 162                      failed INT(1) NOT NULL DEFAULT 0, failreason VARCHAR(255))',
 163                      true);
 164              }
 165              if(!Vtiger_Utils::CheckTable('vtiger_mailer_queueinfo')) {
 166                  Vtiger_Utils::CreateTable('vtiger_mailer_queueinfo',
 167                      '(id INTEGER, name VARCHAR(100), email VARCHAR(100), type VARCHAR(7))',
 168                      true);
 169              }
 170              if(!Vtiger_Utils::CheckTable('vtiger_mailer_queueattachments')) {
 171                  Vtiger_Utils::CreateTable('vtiger_mailer_queueattachments',
 172                      '(id INTEGER, path TEXT, name VARCHAR(100), encoding VARCHAR(50), type VARCHAR(100))',
 173                      true);
 174              }
 175              $this->_queueinitialized = true;
 176          }
 177          return true;
 178      }
 179  
 180      /**
 181       * Add this mail to queue
 182       */
 183  	function __AddToQueue($linktoid) {
 184          if($this->__initializeQueue()) {
 185              global $adb;
 186              $uniqueid = self::__getUniqueId();
 187              $adb->pquery('INSERT INTO vtiger_mailer_queue(id,fromname,fromemail,content_type,subject,body,mailer,relcrmid) VALUES(?,?,?,?,?,?,?,?)',
 188                  Array($uniqueid, $this->FromName, $this->From, $this->ContentType, $this->Subject, $this->Body, $this->Mailer, $linktoid));
 189              $queueid = $adb->database->Insert_ID();
 190              foreach($this->to as $toinfo) {
 191                  if(empty($toinfo[0])) continue;
 192                  $adb->pquery('INSERT INTO vtiger_mailer_queueinfo(id, name, email, type) VALUES(?,?,?,?)',
 193                      Array($queueid, $toinfo[1], $toinfo[0], 'TO'));
 194              }
 195              foreach($this->cc as $ccinfo) {
 196                  if(empty($ccinfo[0])) continue;
 197                  $adb->pquery('INSERT INTO vtiger_mailer_queueinfo(id, name, email, type) VALUES(?,?,?,?)',
 198                      Array($queueid, $ccinfo[1], $ccinfo[0], 'CC'));
 199              }
 200              foreach($this->bcc as $bccinfo) {
 201                  if(empty($bccinfo[0])) continue;
 202                  $adb->pquery('INSERT INTO vtiger_mailer_queueinfo(id, name, email, type) VALUES(?,?,?,?)',
 203                      Array($queueid, $bccinfo[1], $bccinfo[0], 'BCC'));
 204              }
 205              foreach($this->ReplyTo as $rtoinfo) {
 206                  if(empty($rtoinfo[0])) continue;
 207                  $adb->pquery('INSERT INTO vtiger_mailer_queueinfo(id, name, email, type) VALUES(?,?,?,?)',
 208                      Array($queueid, $rtoinfo[1], $rtoinfo[0], 'RPLYTO'));
 209              }
 210              foreach($this->attachment as $attachmentinfo) {
 211                  if(empty($attachmentinfo[0])) continue;
 212                  $adb->pquery('INSERT INTO vtiger_mailer_queueattachments(id, path, name, encoding, type) VALUES(?,?,?,?,?)',
 213                      Array($queueid, $attachmentinfo[0], $attachmentinfo[2], $attachmentinfo[3], $attachmentinfo[4]));
 214              }
 215          }
 216      }
 217  
 218      /**
 219       * Function to prepares email as string
 220       * @return type
 221       */
 222      public function getMailString() {
 223          return $this->MIMEHeader.$this->MIMEBody;
 224      }
 225  
 226      /**
 227       * Dispatch (send) email that was queued.
 228       */
 229  	static function dispatchQueue(Vtiger_Mailer_Listener $listener=null) {
 230          global $adb;
 231          if(!Vtiger_Utils::CheckTable('vtiger_mailer_queue')) return;
 232  
 233          $mailer = new self();
 234          $queue = $adb->pquery('SELECT * FROM vtiger_mailer_queue WHERE failed != ?', array(1));
 235          if($adb->num_rows($queue)) {
 236              for($index = 0; $index < $adb->num_rows($queue); ++$index) {
 237                  $mailer->reinitialize();
 238  
 239                  $queue_record = $adb->fetch_array($queue, $index);
 240                  $queueid = $queue_record['id'];
 241                  $relcrmid= $queue_record['relcrmid'];
 242  
 243                  $mailer->From = $queue_record['fromemail'];
 244                  $mailer->From = $queue_record['fromname'];
 245                  $mailer->Subject=$queue_record['subject'];
 246                  $mailer->Body = decode_html($queue_record['body']);
 247                  $mailer->Mailer=$queue_record['mailer'];
 248                  $mailer->ContentType = $queue_record['content_type'];
 249  
 250                  $emails = $adb->pquery('SELECT * FROM vtiger_mailer_queueinfo WHERE id=?', Array($queueid));
 251                  for($eidx = 0; $eidx < $adb->num_rows($emails); ++$eidx) {
 252                      $email_record = $adb->fetch_array($emails, $eidx);
 253                      if($email_record[type] == 'TO')     $mailer->AddAddress($email_record[email], $email_record[name]);
 254                      else if($email_record[type] == 'CC')$mailer->AddCC($email_record[email], $email_record[name]);
 255                      else if($email_record[type] == 'BCC')$mailer->AddBCC($email_record[email], $email_record[name]);
 256                      else if($email_record[type] == 'RPLYTO')$mailer->AddReplyTo($email_record[email], $email_record[name]);
 257                  }
 258  
 259                  $attachments = $adb->pquery('SELECT * FROM vtiger_mailer_queueattachments WHERE id=?', Array($queueid));
 260                  for($aidx = 0; $aidx < $adb->num_rows($attachments); ++$aidx) {
 261                      $attachment_record = $adb->fetch_array($attachments, $aidx);
 262                      if($attachment_record['path'] != '') {
 263                          $mailer->AddAttachment($attachment_record['path'], $attachment_record['name'],
 264                                                  $attachment_record['encoding'], $attachment_record['type']);
 265                      }
 266                  }
 267                  $sent = $mailer->Send(true);
 268                  if($sent) {
 269                      Vtiger_Event::trigger('vtiger.mailer.mailsent', $relcrmid);
 270                      if($listener) {
 271                          $listener->mailsent($queueid);
 272                      }
 273                      $adb->pquery('DELETE FROM vtiger_mailer_queue WHERE id=?', Array($queueid));
 274                      $adb->pquery('DELETE FROM vtiger_mailer_queueinfo WHERE id=?', Array($queueid));
 275                      $adb->pquery('DELETE FROM vtiger_mailer_queueattachments WHERE id=?', Array($queueid));
 276                  } else {
 277                      if($listener) {
 278                          $listener->mailerror($queueid);
 279                      }
 280                      $adb->pquery('UPDATE vtiger_mailer_queue SET failed=?, failreason=? WHERE id=?', Array(1, $mailer->ErrorInfo, $queueid));
 281                  }
 282              }
 283          }
 284      }
 285  }
 286  
 287  /**
 288   * Provides API to act on the different events triggered by send email action.
 289   * @package vtlib
 290   */
 291  abstract class Vtiger_Mailer_Listener {
 292  	function mailsent($queueid) { }
 293  	function mailerror($queueid) { }
 294  }
 295  
 296  ?>


Generated: Fri Nov 28 20:08:37 2014 Cross-referenced by PHPXref 0.7.1