CMS  Version 3.9
EmailManager Class Reference

Takes an email template and an obj of any DataItem class and sends email to a list of recipients after substituting merge codes for values from the obj. More...

Public Member Functions

 EmailManager ($item, $emailTemplate, $onSendComplete="")
 
 mergeEmail ()
 Use MergeCodeManager, which shares functionality with TextLookup. More...
 
 getRecipients ()
 
 sendEmail ()
 
 sendOneEmail ($recipient)
 

Static Public Member Functions

static registerTransport ($mode, $class)
 Registers an email transport handler. More...
 
static createTransport ($mode=null)
 
static getTransportModes ()
 
static registerSMTPTransport ()
 
static registerSettingsFormExtension ()
 
static settingsExtension ($form)
 
static send ($source, $name, $onSendComplete="")
 Sends an email template, based on the supplied DataItem (or array of DataItems) More...
 
static setDefaults ()
 
static upgradeComponent ($version)
 
static scanDebugLogs ()
 
static registerSerializationHandler ()
 

Public Attributes

 $emailTemplate
 
 $item
 
 $recipients
 
 $message
 
 $subject
 
 $emailFrom
 
 $emailName
 The name of the email sender (optional) More...
 
 $emailTemplateName
 The name of the email template. More...
 
 $onSendComplete
 
 $attachments
 optional - array of files to attach to the email More...
 

Static Public Attributes

static $transports = null
 

Detailed Description

Takes an email template and an obj of any DataItem class and sends email to a list of recipients after substituting merge codes for values from the obj.

For example: message text "Dear [first_name]" becomes Dear John Smith

@item: obj of DataItem class that has the field values to be used in the email (e.g., user, program, order).

@emailTemplate: instance of DataItem EmailTemplate. This can be an instance constructed on the fly rather than retrieved from the db. If constructed, the following fields must be set: recipients subject message class_name

@onSendComplete: optional callback function to record the email that was sent in a log.

The following var are set in function mergeEmail after the MergeCodeManager performs the substitutions:

recipients subject message

Definition at line 393 of file email_manager.inc.

Member Function Documentation

◆ createTransport()

static EmailManager::createTransport (   $mode = null)
static
Parameters
string$mode(optional) the specific email transport required. Defaults to the selected email transport.
Returns
AbstractEmailTransport

Definition at line 417 of file email_manager.inc.

418  {
419  if (EmailManager::$transports == null)
420  {
421  ComponentManager::fireEvent("RegisterEmailTransport");
422  }
423 
424  if (!$mode)
425  {
426  $mode = Settings::getValue("email", "transport_mode");
427  }
428 
430  return new $class;
431  }
static fireEvent($event, $parameter=null, $mustBeConsumed=false)
Fire an event to all subscribers as detailed in their manifests.
static $transports
static getValue($component, $name)
Retrieve the value of the specified Setting.
Definition: settings.inc:104
$mode

◆ EmailManager()

EmailManager::EmailManager (   $item,
  $emailTemplate,
  $onSendComplete = "" 
)

Definition at line 471 of file email_manager.inc.

472  {
473  $this->item = clone($item);
474  $this->item->filter = null;
475  $this->emailTemplate = $emailTemplate;
476  $this->emailTemplateName = ($this->item) ? $this->item->format($emailTemplate->name) : $emailTemplate->name;
477  $this->emailFrom = ($this->item) ? $this->item->format($emailTemplate->sender_email) : $emailTemplate->sender_email;
478  $this->mergeEmail();
479  $this->onSendComplete = $onSendComplete;
480  }
mergeEmail()
Use MergeCodeManager, which shares functionality with TextLookup.

◆ getRecipients()

EmailManager::getRecipients ( )

Definition at line 520 of file email_manager.inc.

521  {
522  $validRecipients = array();
523 
524  trace("EmailManager:getRecipients {$this->recipients}", 3);
525  if($this->recipients)
526  {
527  $recipients = explode(",", $this->recipients);
528  if(count($recipients) > 0)
529  {
530  foreach($recipients as $recipient)
531  {
532  if(preg_match('/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i', $recipient))
533  $validRecipients[] = $recipient;
534  else
535  trace("EmailManager:getRecipients invalid recipient $recipient", 3);
536  }
537  }
538  }
539 
540  return $validRecipients;
541  }
$email recipients
Definition: mail_to.inc:53

◆ getTransportModes()

static EmailManager::getTransportModes ( )
static

Definition at line 433 of file email_manager.inc.

434  {
435  if (EmailManager::$transports == null)
436  {
437  ComponentManager::fireEvent("RegisterEmailTransport");
438  }
439 
440  return array_keys(EmailManager::$transports);
441  }

◆ mergeEmail()

EmailManager::mergeEmail ( )

Use MergeCodeManager, which shares functionality with TextLookup.


Definition at line 508 of file email_manager.inc.

509  {
510  $emailText = $this->emailTemplate->recipients . $this->emailTemplate->subject . $this->emailTemplate->message;
511  $mgr = new MergeCodeManager($this->item, $emailText, $this->emailTemplate->class_name);
512 
513  $fields = array("recipients", "subject", "message");
514  foreach($fields as $field)
515  $this->$field = $mgr->searchAndReplace($this->emailTemplate->$field);
516 
517  $this->message = $this->cleanupTemplateOutput($this->message);
518  }
$email message

◆ registerSerializationHandler()

static EmailManager::registerSerializationHandler ( )
static

Definition at line 702 of file email_manager.inc.

703  {
704  SerializationManager::registerHandler("email_templates", "Email Templates", new SimpleSerializationHandler(EmailTemplate));
706  return true;
707  }
registerHandler($component, $title, $handler)
Registers a serialization handler for a component.
Provides a simple implementation of a SerializationHandler that can serialize a single DataItem class...

◆ registerSettingsFormExtension()

static EmailManager::registerSettingsFormExtension ( )
static

Definition at line 448 of file email_manager.inc.

449  {
451  }
Takes an email template and an obj of any DataItem class and sends email to a list of recipients afte...
static settingsExtension($form)
static registerExtension($component, $handler)

◆ registerSMTPTransport()

static EmailManager::registerSMTPTransport ( )
static

Definition at line 443 of file email_manager.inc.

444  {
445  EmailManager::registerTransport("SMTP", 'SMTPEmailTransport');
446  }
static registerTransport($mode, $class)
Registers an email transport handler.

◆ registerTransport()

static EmailManager::registerTransport (   $mode,
  $class 
)
static

Registers an email transport handler.

Parameters
string$modeidentifying mode name for the transport handler
string$classname of the class providing the transport. This must derive from AbstractEmailTransport

Definition at line 402 of file email_manager.inc.

403  {
405  {
406  EmailManager::$transports = array();
407  }
408 
410  }

◆ scanDebugLogs()

static EmailManager::scanDebugLogs ( )
static

Definition at line 669 of file email_manager.inc.

670  {
671  $path = Settings::getValue("email", "debugging_mode_output_path");
672 
673  $logs = array();
674 
675  trace("EmailManager::Scanning $path", 3);
676 
677  $handle = opendir($path);
678 
679  if(!$handle)
680  return;
681 
682  $idx = 0;
683  while(false !== ($file = readdir($handle)))
684  {
685  // omit "." and ".." in the directory
686  if (!preg_match("/(^\.{1,2}$)/i", $file))
687  {
688  $log = new EmailDebugLog();
689  $log->log_id = $idx;
690  $log->filename = $file;
691  $log->date = date("F d, Y g:ia", (filemtime($path . DIRECTORY_SEPARATOR . $file)));
692  $logs[$log->log_id] = $log;
693  $idx++;
694  }
695  }
696 
697  closedir($handle);
698 
699  return $logs;
700  }
if(! $page) $path
Definition: page.inc:57
$file
Definition: delete.inc:47

◆ send()

static EmailManager::send (   $source,
  $name,
  $onSendComplete = "" 
)
static

Sends an email template, based on the supplied DataItem (or array of DataItems)

Parameters
mixed$sourceeither a single DataItem, or an array of DataItems
string$namethe identifier of the email template
callable$onSendCompletecallback handler to be called after all generated emails have been sent
Returns
number|boolean

Definition at line 593 of file email_manager.inc.

594  {
596 
597  if($emailTemplate)
598  {
599  if (is_array($source))
600  {
601  $sendingItem = new JoinResult();
602  foreach($source as $item)
603  {
604  $cl = get_class($item);
605  $sendingItem->$cl = $item;
606  }
607  }
608  else
609  {
610  $sendingItem = $source;
611  }
612 
613  trace("Sending email '$name'", 3);
614  $emailManager = new EmailManager($sendingItem, $emailTemplate, $onSendComplete);
615  $rtn = $emailManager->sendEmail();
616  trace("EmailManager:: rtn code $rtn", 3);
617  }
618  else
619  {
620  $rtn = 0;
621  trace("EmailManager::no template found", 3);
622  }
623  return $rtn;
624  }
$name
Definition: upload.inc:54
EmailManager($item, $emailTemplate, $onSendComplete="")
static getEmailTemplate($name)
Retrieves the named email templated.

◆ sendEmail()

EmailManager::sendEmail ( )

Definition at line 550 of file email_manager.inc.

551  {
552  $recipients = $this->getRecipients();
553  $rtn = false;
554 
555  if(count($recipients) > 0)
556  {
557  foreach($recipients as $recipient)
558  {
559  $rtn = $this->sendOneEmail($recipient);
560  }
561  }
562  else
563  {
564  trace("EmailManager:: Warning - no valid recipients", 3);
565  }
566  return $rtn;
567  }
sendOneEmail($recipient)

◆ sendOneEmail()

EmailManager::sendOneEmail (   $recipient)

Definition at line 569 of file email_manager.inc.

570  {
571  trace("EmailManager:: sending email to recipient $recipient", 3);
572 
573  $email = new EmailHandler($recipient, $this->subject, $this->message, $this->emailFrom, $this->emailName, $this->attachments);
574 
575  $rtn = $email->send();
576 
577  if ($this->onSendComplete AND is_callable($this->onSendComplete))
578  {
579  call_user_func($this->onSendComplete, $this, $recipient, $rtn);
580  }
581 
582  EmailLog::logEmail($this, $recipient, $rtn);
583  return $rtn;
584  }
static logEmail($mgr, $toAddr, $rtn)
Record email sent in a log.
Definition: email_log.inc:31
$email subject

◆ setDefaults()

static EmailManager::setDefaults ( )
static

Definition at line 626 of file email_manager.inc.

627  {
628  trace("EmailManager::setDefaults", 3);
629 
630  global $config;
631 
632  Settings::setDefaultValue("email", "transport_mode", "SMTP", "String", "Specifies the mail transport for system generated emails", "Mail Transport", null, 0);
633 
634  Settings::setDefaultValue("email", "use_debugging_mode", false, "Boolean", "Enable local logging of generated emails (instead of sending via SMTP)", "Debugging Mode", null, 0);
635 
636  $defaultPath = $config['uploadbase'] . DIRECTORY_SEPARATOR . "email_debug_logs";
637 
638  if (!file_exists($defaultPath)) mkdir($defaultPath);
639 
640  Settings::setDefaultValue("email", "debugging_mode_output_path", $defaultPath, "String", "Folder that will store the logged emails in debugging mode", "Debugging Mode", null, 1);
641  Settings::setDefaultValue("email", "email_from", "andy@sonjara.com", "String", "Default From address for generated emails", "Contact Details", null, 1);
642  Settings::setDefaultValue("email", "email_contact", "andy@sonjara.com", "String", "Default To address for generated emails", "Contact Details", null, 2);
643  Settings::setDefaultValue("email", "email_name", "Andy Green", "String", "Default Name of email contact for generated emails", "Contact Details", null, 3);
644  Settings::setDefaultValue("email", "default_contact_us_recipient", "", "String", "Default email address(es) that will receive messages from the Contact Us form. These are used if no Contact Topics are defined", "Contact Details", null, 4);
645  Settings::setDefaultValue("email", "use_captcha", "Never", String, "Specify when to use a Captcha to verify the user", "Contact Details", "Never\nAnonymous Only\nAlways", 5);
646  Settings::setDefaultValue("email", "HTML_email_base_url", "", String, "", "Contact Details", null, 6);
647 
648  Settings::setDefaultValue("email", "override_PHP_settings", false, Boolean, "Use these server settings instead of the global settings in your php.ini file", "Server Settings", 0);
649  Settings::setDefaultValue("email", "mail_server", "localhost", "String", "Host name or IP address of your upstream SMTP server", "Server Settings", "", 1);
650  Settings::setDefaultValue("email", "mail_server_port", 25, Number, "Port to use on the upstream SMTP server", "Server Settings", "", 2);
651  Settings::setDefaultValue("email", "username", "", String, "SMTP Account username (if authentication is required)", "Server Settings", "", 3);
652  Settings::setDefaultValue("email", "password", "", String, "SMTP Account password (if authentication is required)", "Server Settings", "", 4);
653 
654  Settings::setDefaultValue("email", "log_messages", false, Boolean, "Log messages in the database after sending", "Message Logging", "", 1);
655  Settings::setDefaultValue("email", "log_message_bodies", false, Boolean, "Specifies whether to store the message text when logging an email message. ".
656  "It is recommended to leave this unchecked to conserve space in the database.", "Message Logging", "", 2);
657  }
static setDefaultValue($component, $name, $value, $field_type="String", $annotation="", $category="", $options="", $weight=0)
Sets the default value of the given component setting.
Definition: settings.inc:174
global $config
Definition: import.inc:4

◆ settingsExtension()

static EmailManager::settingsExtension (   $form)
static

Definition at line 453 of file email_manager.inc.

454  {
456  $options = array_combine($transports, $transports);
457  $form->dropdown("transport_mode", "Selected Transport Mode", $options);
458  }
$form
static getTransportModes()

◆ upgradeComponent()

static EmailManager::upgradeComponent (   $version)
static

Definition at line 659 of file email_manager.inc.

660  {
661  $mgr = new EmailUpgradeManager();
662  $mgr->upgrade($version);
663  }

Member Data Documentation

◆ $attachments

EmailManager::$attachments

optional - array of files to attach to the email

Definition at line 469 of file email_manager.inc.

◆ $emailFrom

EmailManager::$emailFrom

Definition at line 465 of file email_manager.inc.

◆ $emailName

EmailManager::$emailName

The name of the email sender (optional)

Definition at line 466 of file email_manager.inc.

◆ $emailTemplate

EmailManager::$emailTemplate

Definition at line 460 of file email_manager.inc.

◆ $emailTemplateName

EmailManager::$emailTemplateName

The name of the email template.

Definition at line 467 of file email_manager.inc.

◆ $item

EmailManager::$item

Definition at line 461 of file email_manager.inc.

◆ $message

EmailManager::$message

Definition at line 463 of file email_manager.inc.

◆ $onSendComplete

EmailManager::$onSendComplete

Definition at line 468 of file email_manager.inc.

◆ $recipients

EmailManager::$recipients

Definition at line 462 of file email_manager.inc.

◆ $subject

EmailManager::$subject

Definition at line 464 of file email_manager.inc.

◆ $transports

EmailManager::$transports = null
static

Definition at line 395 of file email_manager.inc.


The documentation for this class was generated from the following file: