Ejemplo n.º 1
0
 /**
  * Send an email notification to all super users.
  *
  * @param $subject
  * @param $message
  */
 protected function sendEmailNotification($subject, $message)
 {
     $superUsers = UsersManagerApi::getInstance()->getUsersHavingSuperUserAccess();
     foreach ($superUsers as $superUser) {
         $mail = new Mail();
         $mail->setDefaultFromPiwik();
         $mail->addTo($superUser['email']);
         $mail->setSubject($subject);
         $mail->setBodyText($message);
         $mail->send();
     }
 }
Ejemplo n.º 2
0
 public function sendReport($reportType, $report, $contents, $filename, $prettyDate, $reportSubject, $reportTitle, $additionalFiles, Period $period = null, $force)
 {
     if (!self::manageEvent($reportType)) {
         return;
     }
     // Safeguard against sending the same report twice to the same email (unless $force is true)
     if (!$force && $this->reportAlreadySent($report, $period)) {
         Log::warning('Preventing the same scheduled report from being sent again (report #%s for period "%s")', $report['idreport'], $prettyDate);
         return;
     }
     $periods = self::getPeriodToFrequencyAsAdjective();
     $message = Piwik::translate('ScheduledReports_EmailHello');
     $subject = Piwik::translate('General_Report') . ' ' . $reportTitle . " - " . $prettyDate;
     $mail = new Mail();
     $mail->setDefaultFromPiwik();
     $mail->setSubject($subject);
     $attachmentName = $subject;
     $this->setReplyToAsSender($mail, $report);
     $displaySegmentInfo = false;
     $segmentInfo = null;
     $segment = API::getSegment($report['idsegment']);
     if ($segment != null) {
         $displaySegmentInfo = true;
         $segmentInfo = Piwik::translate('ScheduledReports_SegmentAppliedToReports', $segment['name']);
     }
     $messageFindAttached = Piwik::translate('ScheduledReports_PleaseFindAttachedFile', array($periods[$report['period']], $reportTitle));
     $messageFindBelow = Piwik::translate('ScheduledReports_PleaseFindBelow', array($periods[$report['period']], $reportTitle));
     $messageSentFrom = '';
     $piwikUrl = SettingsPiwik::getPiwikUrl();
     if (!empty($piwikUrl)) {
         $messageSentFrom = Piwik::translate('ScheduledReports_SentFromX', $piwikUrl);
     }
     switch ($report['format']) {
         case 'html':
             // Needed when using images as attachment with cid
             $mail->setType(Zend_Mime::MULTIPART_RELATED);
             $message .= "<br/>{$messageFindBelow}<br/>{$messageSentFrom}";
             if ($displaySegmentInfo) {
                 $message .= " " . $segmentInfo;
             }
             $mail->setBodyHtml($message . "<br/><br/>" . $contents);
             break;
         case 'csv':
             $message .= "\n{$messageFindAttached}\n{$messageSentFrom}";
             if ($displaySegmentInfo) {
                 $message .= " " . $segmentInfo;
             }
             $mail->setBodyText($message);
             $mail->createAttachment($contents, 'application/csv', Zend_Mime::DISPOSITION_INLINE, Zend_Mime::ENCODING_BASE64, $attachmentName . '.csv');
             break;
         default:
         case 'pdf':
             $message .= "\n{$messageFindAttached}\n{$messageSentFrom}";
             if ($displaySegmentInfo) {
                 $message .= " " . $segmentInfo;
             }
             $mail->setBodyText($message);
             $mail->createAttachment($contents, 'application/pdf', Zend_Mime::DISPOSITION_INLINE, Zend_Mime::ENCODING_BASE64, $attachmentName . '.pdf');
             break;
     }
     foreach ($additionalFiles as $additionalFile) {
         $fileContent = $additionalFile['content'];
         $at = $mail->createAttachment($fileContent, $additionalFile['mimeType'], Zend_Mime::DISPOSITION_INLINE, $additionalFile['encoding'], $additionalFile['filename']);
         $at->id = $additionalFile['cid'];
         unset($fileContent);
     }
     // Get user emails and languages
     $reportParameters = $report['parameters'];
     $emails = array();
     if (isset($reportParameters[self::ADDITIONAL_EMAILS_PARAMETER])) {
         $emails = $reportParameters[self::ADDITIONAL_EMAILS_PARAMETER];
     }
     if ($reportParameters[self::EMAIL_ME_PARAMETER] == 1) {
         if (Piwik::getCurrentUserLogin() == $report['login']) {
             $emails[] = Piwik::getCurrentUserEmail();
         } else {
             try {
                 $user = APIUsersManager::getInstance()->getUser($report['login']);
             } catch (Exception $e) {
                 return;
             }
             $emails[] = $user['email'];
         }
     }
     if (!$force) {
         $this->markReportAsSent($report, $period);
     }
     foreach ($emails as $email) {
         if (empty($email)) {
             continue;
         }
         $mail->addTo($email);
         try {
             $mail->send();
         } catch (Exception $e) {
             // If running from piwik.php with debug, we ignore the 'email not sent' error
             $tracker = new Tracker();
             if (!$tracker->isDebugModeEnabled()) {
                 throw new Exception("An error occured while sending '{$filename}' " . " to " . implode(', ', $mail->getRecipients()) . ". Error was '" . $e->getMessage() . "'");
             }
         }
         $mail->clearRecipients();
     }
 }
Ejemplo n.º 3
0
 public function sendReport($reportType, $report, $contents, $filename, $prettyDate, $reportSubject, $reportTitle, $additionalFiles)
 {
     if (self::manageEvent($reportType)) {
         $periods = self::getPeriodToFrequencyAsAdjective();
         $message = Piwik::translate('ScheduledReports_EmailHello');
         $subject = Piwik::translate('General_Report') . ' ' . $reportTitle . " - " . $prettyDate;
         $mail = new Mail();
         $mail->setDefaultFromPiwik();
         $mail->setSubject($subject);
         $attachmentName = $subject;
         $displaySegmentInfo = false;
         $segmentInfo = null;
         $segment = API::getSegment($report['idsegment']);
         if ($segment != null) {
             $displaySegmentInfo = true;
             $segmentInfo = Piwik::translate('ScheduledReports_SegmentAppliedToReports', $segment['name']);
         }
         switch ($report['format']) {
             case 'html':
                 // Needed when using images as attachment with cid
                 $mail->setType(Zend_Mime::MULTIPART_RELATED);
                 $message .= "<br/>" . Piwik::translate('ScheduledReports_PleaseFindBelow', array($periods[$report['period']], $reportTitle));
                 if ($displaySegmentInfo) {
                     $message .= " " . $segmentInfo;
                 }
                 $mail->setBodyHtml($message . "<br/><br/>" . $contents);
                 break;
             case 'csv':
                 $message .= "\n" . Piwik::translate('ScheduledReports_PleaseFindAttachedFile', array($periods[$report['period']], $reportTitle));
                 if ($displaySegmentInfo) {
                     $message .= " " . $segmentInfo;
                 }
                 $mail->setBodyText($message);
                 $mail->createAttachment($contents, 'application/csv', Zend_Mime::DISPOSITION_INLINE, Zend_Mime::ENCODING_BASE64, $attachmentName . '.csv');
                 break;
             default:
             case 'pdf':
                 $message .= "\n" . Piwik::translate('ScheduledReports_PleaseFindAttachedFile', array($periods[$report['period']], $reportTitle));
                 if ($displaySegmentInfo) {
                     $message .= " " . $segmentInfo;
                 }
                 $mail->setBodyText($message);
                 $mail->createAttachment($contents, 'application/pdf', Zend_Mime::DISPOSITION_INLINE, Zend_Mime::ENCODING_BASE64, $attachmentName . '.pdf');
                 break;
         }
         foreach ($additionalFiles as $additionalFile) {
             $fileContent = $additionalFile['content'];
             $at = $mail->createAttachment($fileContent, $additionalFile['mimeType'], Zend_Mime::DISPOSITION_INLINE, $additionalFile['encoding'], $additionalFile['filename']);
             $at->id = $additionalFile['cid'];
             unset($fileContent);
         }
         // Get user emails and languages
         $reportParameters = $report['parameters'];
         $emails = array();
         if (isset($reportParameters[self::ADDITIONAL_EMAILS_PARAMETER])) {
             $emails = $reportParameters[self::ADDITIONAL_EMAILS_PARAMETER];
         }
         if ($reportParameters[self::EMAIL_ME_PARAMETER] == 1) {
             if (Piwik::getCurrentUserLogin() == $report['login']) {
                 $emails[] = Piwik::getCurrentUserEmail();
             } else {
                 try {
                     $user = APIUsersManager::getInstance()->getUser($report['login']);
                 } catch (Exception $e) {
                     return;
                 }
                 $emails[] = $user['email'];
             }
         }
         foreach ($emails as $email) {
             if (empty($email)) {
                 continue;
             }
             $mail->addTo($email);
             try {
                 $mail->send();
             } catch (Exception $e) {
                 // If running from piwik.php with debug, we ignore the 'email not sent' error
                 if (!isset($GLOBALS['PIWIK_TRACKER_DEBUG']) || !$GLOBALS['PIWIK_TRACKER_DEBUG']) {
                     throw new Exception("An error occured while sending '{$filename}' " . " to " . implode(', ', $mail->getRecipients()) . ". Error was '" . $e->getMessage() . "'");
                 }
             }
             $mail->clearRecipients();
         }
     }
 }
Ejemplo n.º 4
0
 /**
  * @param array $alerts
  * @param Mail $mail
  * @param string[] $recipient Email addresses
  * @param $period
  * @param $idSite
  */
 protected function sendAlertsPerEmailToRecipient($alerts, Mail $mail, $recipient, $period, $idSite)
 {
     if (empty($recipient) || empty($alerts)) {
         return;
     }
     $prettyDate = $this->getPrettyDateForSite($period, $idSite);
     $websiteName = Site::getNameFor($idSite);
     $mail->setDefaultFromPiwik();
     $mail->addTo($recipient);
     $mail->setSubject(Piwik::translate('CustomAlerts_MailAlertSubject', array($websiteName, $prettyDate)));
     $controller = new Controller();
     $viewHtml = new View('@CustomAlerts/alertHtmlMail');
     $viewHtml->assign('triggeredAlerts', $controller->formatAlerts($alerts, 'html'));
     $mail->setBodyHtml($viewHtml->render());
     $viewText = new View('@CustomAlerts/alertTextMail');
     $viewText->assign('triggeredAlerts', $controller->formatAlerts($alerts, 'text'));
     $viewText->setContentType('text/plain');
     $mail->setBodyText($viewText->render());
     $mail->send();
 }