Example #1
0
 public function send($transports = array())
 {
     $output = false;
     $queuedMessageInfo = $this->getData();
     try {
         $options = array('intro' => Util::lavnn('intro', $queuedMessageInfo, ''), 'manual' => Util::lavnn('manual', $queuedMessageInfo, 0), 'signature' => Util::lavnn('signature', $queuedMessageInfo, 'team'), 'language' => Util::lavnn('language', $queuedMessageInfo, 'en'), 'sender' => Util::lavnn('sender', $queuedMessageInfo, 0));
         if (count($transports) > 0) {
             $options['transport'] = $transports[0];
         }
         if (count($transports) > 1) {
             $options['alternativeTransport'] = $transports[1];
         }
         $output = MailUtil::mail($queuedMessageInfo['email'], $queuedMessageInfo['subject'], $queuedMessageInfo['message'], $options);
     } catch (\Exception $ex) {
         $env = Runtime::getInstance()->config['ENV'];
         $adminAlert = new AdminAlert();
         $adminAlert->insert(array('title' => '[' . $env . '] Failed email sending attempt to address "' . $queuedMessageInfo['email'], 'description' => print_r($queuedMessageInfo, 1), 'context' => print_r($ex->getTrace(), 1)));
     }
     return $output;
 }
Example #2
0
 /**
  * Sends mail to external email address. wraps standard php function with useful options:
  *   'intercept': expects an email address to send.
  *      All MAMP mails are intercepted by default.
  *      To prevent intercepting on local machine, Runtime context can be used (explicitly set 'intercept' context item to false)
  *   'format': expects either 'text' or 'html' ('html' is used by default)
  *
  * @param $to
  * @param $subj
  * @param $body
  * @param array $options
  *
  * @return bool
  */
 public static function mail($to, $subj, $body, $options = array())
 {
     $config = Config::getInstance();
     $env = $config->get('ENV');
     // Intercept message
     if (in_array($env, array('MAMP', 'TEST')) && Runtime::getInstance()->getContextItem('intercept', true)) {
         // on MAMP, intercept every outgoing message, if not explicitly disabled in Runtime context
         // on TEST, add admin email(s) to original address, if not explicitly disabled in Runtime context
         $intercept = Util::lavnn('adminEmail', $config->getEnvConfig(), $config->get('adminEmail'));
         if ($env == 'TEST') {
             $intercept = $to . ',' . $intercept;
         }
         $subj = '[' . $env . '] ' . $subj;
     } else {
         $intercept = Util::lavnn('intercept', $options, '');
     }
     // generate a unique hash for the mail
     $now = DateTimeUtil::fixTime();
     $toAsString = is_array($to) ? join(',', $to) : $to;
     $hash = md5($toAsString . ':' . $now . ':' . $subj);
     if ($intercept != '') {
         //$to = explode(',', $intercept);
         $to = $intercept;
     }
     // set the transport option
     if (!isset($options['transport'])) {
         $options['transport'] = $config->getEnvSetting('mailTransport');
     }
     $result = false;
     $intro = Util::lavnn('intro', $options, '');
     if ($to != '') {
         $result = MailUtil::transportMail($to, $subj, $body, $hash, $options);
         // if first sending attempt to provided transport failed, try alternatives (if any)
         if (!$result && isset($options['alternativeTransport'])) {
             foreach ((array) $options['alternativeTransport'] as $alternativeTransport) {
                 $options['transport'] = $alternativeTransport;
                 if ($to != '') {
                     $result = MailUtil::transportMail($to, $subj, $body, $hash, $options);
                     if ($result) {
                         break;
                     }
                 }
             }
         }
     } else {
         $messageParams = array('email' => $to, 'subject' => $subj, 'message' => $body, 'intro' => $intro);
         $adminAlert = new AdminAlert();
         $adminAlert->insert(array('title' => 'Message sent with empty email address', 'description' => print_r($messageParams, 1), 'context' => print_r(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS), 1)));
     }
     if ($result) {
         // save sent email to the database
         $mailModel = new MailMessageSent();
         $mailParams = array('hash' => $hash, 'email' => is_array($to) ? join(', ', $to) : $to, 'subject' => $subj, 'message' => $body, 'intro' => $intro, 'sent' => $now);
         $manual = Util::lavnn('manual', $options, 0);
         if ($manual > 0) {
             $mailParams['manual'] = 1;
         }
         $sender = Util::lavnn('sender', $options, 0);
         if ($sender > 0) {
             $mailParams['sender'] = $sender;
         }
         $mailParams['signature'] = Util::lavnn('signature', $options, 'team');
         $mailParams['language'] = Util::lavnn('language', $options, 'en');
         // @TODO use from $config
         $result = $mailModel->insert($mailParams) > 0;
     }
     return $result;
 }