protected function sendReportEmail($emails, $outputFilename, $prettyDate, $websiteName, $report, $reportFormat, $additionalFiles) { $periods = self::getPeriodToFrequency(); $message = Piwik_Translate('PDFReports_EmailHello'); $subject = Piwik_Translate('General_Report') . ' ' . $websiteName . " - " . $prettyDate; if (!file_exists($outputFilename)) { throw new Exception("The report file wasn't found in {$outputFilename}"); } $filename = basename($outputFilename); $handle = fopen($outputFilename, "r"); $contents = fread($handle, filesize($outputFilename)); fclose($handle); $mail = new Piwik_Mail(); $mail->setSubject($subject); $fromEmailName = Piwik_Config::getInstance()->branding['use_custom_logo'] ? Piwik_Translate('CoreHome_WebAnalyticsReports') : Piwik_Translate('PDFReports_PiwikReports'); $fromEmailAddress = Piwik_Config::getInstance()->General['noreply_email_address']; $attachmentName = $subject; $mail->setFrom($fromEmailAddress, $fromEmailName); switch ($reportFormat) { case 'html': // Needed when using images as attachment with cid $mail->setType(Zend_Mime::MULTIPART_RELATED); $message .= "<br/>" . Piwik_Translate('PDFReports_PleaseFindBelow', array($periods[$report['period']], $websiteName)); $mail->setBodyHtml($message . "<br/><br/>" . $contents); break; default: case 'pdf': $message .= "\n" . Piwik_Translate('PDFReports_PleaseFindAttachedFile', array($periods[$report['period']], $websiteName)); $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); } foreach ($emails as $email) { $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(); } // Update flag in DB Zend_Registry::get('db')->update(Piwik_Common::prefixTable('pdf'), array('ts_last_sent' => Piwik_Date::now()->getDatetime()), "idreport = " . $report['idreport']); // If running from piwik.php with debug, do not delete the PDF after sending the email if (!isset($GLOBALS['PIWIK_TRACKER_DEBUG']) || !$GLOBALS['PIWIK_TRACKER_DEBUG']) { @chmod($outputFilename, 0600); @unlink($outputFilename); } }
/** * @param Piwik_Event_Notification $notification notification object */ function sendReport($notification) { if (self::manageEvent($notification)) { $notificationInfo = $notification->getNotificationInfo(); $report = $notificationInfo[Piwik_PDFReports_API::REPORT_KEY]; $websiteName = $notificationInfo[Piwik_PDFReports_API::WEBSITE_NAME_KEY]; $prettyDate = $notificationInfo[Piwik_PDFReports_API::PRETTY_DATE_KEY]; $contents = $notificationInfo[Piwik_PDFReports_API::REPORT_CONTENT_KEY]; $filename = $notificationInfo[Piwik_PDFReports_API::FILENAME_KEY]; $additionalFiles = $notificationInfo[Piwik_PDFReports_API::ADDITIONAL_FILES_KEY]; $periods = self::getPeriodToFrequency(); $message = Piwik_Translate('PDFReports_EmailHello'); $subject = Piwik_Translate('General_Report') . ' ' . $websiteName . " - " . $prettyDate; $mail = new Piwik_Mail(); $mail->setSubject($subject); $fromEmailName = Piwik_Config::getInstance()->branding['use_custom_logo'] ? Piwik_Translate('CoreHome_WebAnalyticsReports') : Piwik_Translate('PDFReports_PiwikReports'); $fromEmailAddress = Piwik_Config::getInstance()->General['noreply_email_address']; $attachmentName = $subject; $mail->setFrom($fromEmailAddress, $fromEmailName); switch ($report['format']) { case 'html': // Needed when using images as attachment with cid $mail->setType(Zend_Mime::MULTIPART_RELATED); $message .= "<br/>" . Piwik_Translate('PDFReports_PleaseFindBelow', array($periods[$report['period']], $websiteName)); $mail->setBodyHtml($message . "<br/><br/>" . $contents); break; default: case 'pdf': $message .= "\n" . Piwik_Translate('PDFReports_PleaseFindAttachedFile', array($periods[$report['period']], $websiteName)); $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(); } elseif ($report['login'] == Piwik_Config::getInstance()->superuser['login']) { $emails[] = Piwik::getSuperUserEmail(); } else { try { $user = Piwik_UsersManager_API::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(); } } }