/** * Notifies the given user of a report problem * * @param User $recipient Message recipient * @param string $subject Message subject * @param string $body Message body */ protected function sendNotificationOfReport(User $recipient, $subject, $body) { $mailer = MailerFactory::getSystemDefaultMailer(); // set the subject of the email $mailer->setSubject($subject); // set the body of the email... $textOnly = EmailFormatter::isTextOnly($body); if ($textOnly) { $mailer->setTextBody($body); } else { $textBody = strip_tags(br2nl($body)); // need to create the plain-text part $mailer->setTextBody($textBody); $mailer->setHtmlBody($body); } // add the recipient... // first get all email addresses known for this recipient $recipientEmailAddresses = array($recipient->email1, $recipient->email2); $recipientEmailAddresses = array_filter($recipientEmailAddresses); // then retrieve first non-empty email address $recipientEmailAddress = array_shift($recipientEmailAddresses); // a MailerException is raised if $email is invalid, which prevents the call to send below $mailer->addRecipientsTo(new EmailIdentity($recipientEmailAddress)); // send the email $mailer->send(); }
/** * @param $data * @return bool */ public function run($data) { global $current_user; global $current_language; global $locale; $this->job->runnable_ran = true; $this->job->runnable_data = $data; $report_schedule_id = $data; require_once 'modules/Reports/schedule/ReportSchedule.php'; $reportSchedule = new ReportSchedule(); $scheduleInfo = $reportSchedule->getInfo($report_schedule_id); $GLOBALS["log"]->debug("-----> in Reports foreach() loop"); $savedReport = BeanFactory::getBean('Reports', $scheduleInfo['report_id']); $GLOBALS["log"]->debug("-----> Generating Reporter"); require_once 'modules/Reports/Report.php'; $reporter = new Report(from_html($savedReport->content)); $reporter->is_saved_report = true; $reporter->saved_report = $savedReport; $reporter->saved_report_id = $savedReport->id; $mod_strings = return_module_language($current_language, 'Reports'); // prevent invalid report from being processed if (!$reporter->is_definition_valid()) { $invalidFields = $reporter->get_invalid_fields(); $args = array($scheduleInfo['report_id'], implode(', ', $invalidFields)); $message = string_format($mod_strings['ERR_REPORT_INVALID'], $args); $GLOBALS["log"]->fatal("-----> {$message}"); $reportOwner = BeanFactory::retrieveBean('Users', $savedReport->assigned_user_id); if ($reportOwner) { require_once 'modules/Reports/utils.php'; $reportsUtils = new ReportsUtilities(); try { $reportsUtils->sendNotificationOfInvalidReport($reportOwner, $message); } catch (MailerException $me) { //@todo consider logging the error at the very least } } $this->job->failJob('Report field definition is invalid'); return false; } else { $GLOBALS["log"]->debug("-----> Reporter settings attributes"); $reporter->layout_manager->setAttribute("no_sort", 1); $GLOBALS["log"]->debug("-----> Reporter Handling PDF output"); require_once 'modules/Reports/templates/templates_tcpdf.php'; $reportFilename = template_handle_pdf($reporter, false); // get the recipient's data... // first get all email addresses known for this recipient $recipientEmailAddresses = array($current_user->email1, $current_user->email2); $recipientEmailAddresses = array_filter($recipientEmailAddresses); // then retrieve first non-empty email address $recipientEmailAddress = array_shift($recipientEmailAddresses); // get the recipient name that accompanies the email address $recipientName = $locale->formatName($current_user); $result = false; try { $GLOBALS["log"]->debug("-----> Generating Mailer"); $mailer = MailerFactory::getSystemDefaultMailer(); // set the subject of the email $subject = empty($savedReport->name) ? "Report" : $savedReport->name; $mailer->setSubject($subject); // add the recipient $mailer->addRecipientsTo(new EmailIdentity($recipientEmailAddress, $recipientName)); // attach the report, using the subject as the name of the attachment $charsToRemove = array("\r", "\n"); // remove these characters from the attachment name $attachmentName = str_replace($charsToRemove, "", $subject); // replace spaces with the underscores $attachmentName = str_replace(" ", "_", "{$attachmentName}.pdf"); $attachment = new Attachment($reportFilename, $attachmentName, Encoding::Base64, "application/pdf"); $mailer->addAttachment($attachment); // set the body of the email $body = $mod_strings["LBL_HELLO"]; if ($recipientName != "") { $body .= " {$recipientName}"; } $body .= ",\n\n" . $mod_strings["LBL_SCHEDULED_REPORT_MSG_INTRO"] . $savedReport->date_entered . $mod_strings["LBL_SCHEDULED_REPORT_MSG_BODY1"] . $savedReport->name . $mod_strings["LBL_SCHEDULED_REPORT_MSG_BODY2"]; $textOnly = EmailFormatter::isTextOnly($body); if ($textOnly) { $mailer->setTextBody($body); } else { $textBody = strip_tags(br2nl($body)); // need to create the plain-text part $mailer->setTextBody($textBody); $mailer->setHtmlBody($body); } $GLOBALS["log"]->debug("-----> Sending PDF via Email to [ {$recipientEmailAddress} ]"); $mailer->send(); $result = true; $GLOBALS["log"]->debug("-----> Send successful"); $reportSchedule->update_next_run_time($report_schedule_id, $scheduleInfo["next_run"], $scheduleInfo["time_interval"]); } catch (MailerException $me) { switch ($me->getCode()) { case MailerException::InvalidEmailAddress: $GLOBALS["log"]->info("No email address for {$recipientName}"); break; default: $GLOBALS["log"]->fatal("Mail error: " . $me->getMessage()); break; } } $GLOBALS["log"]->debug("-----> Removing temporary PDF file"); unlink($reportFilename); if ($result) { $this->job->succeedJob(); } return $result; } }
function get_system_default_body(&$mail_object, $focus, &$notify_user) { global $sugar_version, $sugar_config, $current_user; if (!isset($_SESSION['authenticated_user_language']) || empty($_SESSION['authenticated_user_language'])) { $currentLanguage = $sugar_config['default_language']; } else { $currentLanguage = $_SESSION['authenticated_user_language']; } $xtpl = new XTemplate("include/language/{$currentLanguage}.notify_template.html"); $templateName = $focus->object_name; $focus->current_notify_user = $notify_user; if (in_array('set_notification_body', get_class_methods($focus))) { $xtpl = $focus->set_notification_body($xtpl, $focus); } else { $xtpl->assign("OBJECT", $focus->object_name); $templateName = "Default"; } $xtpl->assign("ASSIGNED_USER", $focus->new_assigned_user_name); $xtpl->assign("ASSIGNER", $current_user->user_name); $xtpl->assign("URL", "{$sugar_config['site_url']}/index.php?module={$focus->module_dir}&action=DetailView&record={$focus->id}"); $xtpl->assign("SUGAR", "Sugar v{$sugar_version}"); $xtpl->parse($templateName); $xtpl->parse("{$templateName}_Subject"); $subject = $xtpl->text("{$templateName}_Subject"); $mail_object->setSubject($subject); $body = trim($xtpl->text($templateName)); $textOnly = EmailFormatter::isTextOnly($body); if ($textOnly) { $mail_object->setTextBody($body); } else { $textBody = strip_tags(br2nl($body)); // need to create the plain-text part $mail_object->setTextBody($textBody); $mail_object->setHtmlBody($body); } return false; // false=no errors }
/** * send reminders * @param SugarBean $bean * @param Administration $admin *use is deprecated* * @param array $recipients * @return boolean */ protected function sendReminders(SugarBean $bean, Administration $admin, $recipients) { if (!empty($_SESSION["authenticated_user_language"])) { $currentLanguage = $_SESSION["authenticated_user_language"]; } else { $currentLanguage = $GLOBALS["sugar_config"]["default_language"]; } $user = BeanFactory::getBean('Users', $bean->created_by); $xtpl = new XTemplate(get_notify_template_file($currentLanguage)); $xtpl = $this->setReminderBody($xtpl, $bean, $user); $templateName = "{$GLOBALS["beanList"][$bean->module_dir]}Reminder"; $xtpl->parse($templateName); $xtpl->parse("{$templateName}_Subject"); $mailTransmissionProtocol = "unknown"; try { $mailer = MailerFactory::getSystemDefaultMailer(); $mailTransmissionProtocol = $mailer->getMailTransmissionProtocol(); // set the subject of the email $subject = $xtpl->text("{$templateName}_Subject"); $mailer->setSubject($subject); // set the body of the email $body = trim($xtpl->text($templateName)); $textOnly = EmailFormatter::isTextOnly($body); if ($textOnly) { $mailer->setTextBody($body); } else { $textBody = strip_tags(br2nl($body)); // need to create the plain-text part $mailer->setTextBody($textBody); $mailer->setHtmlBody($body); } foreach ($recipients as $recipient) { // reuse the mailer, but process one send per recipient $mailer->clearRecipients(); $mailer->addRecipientsTo(new EmailIdentity($recipient["email"], $recipient["name"])); $mailer->send(); } } catch (MailerException $me) { $message = $me->getMessage(); switch ($me->getCode()) { case MailerException::FailedToConnectToRemoteServer: $GLOBALS["log"]->fatal("Email Reminder: error sending email, system smtp server is not set"); break; default: $GLOBALS["log"]->fatal("Email Reminder: error sending e-mail (method: {$mailTransmissionProtocol}), (error: {$message})"); break; } return false; } return true; }