From b5bbbcac128793327549fb90ba2e98ccc05f650f Mon Sep 17 00:00:00 2001 From: Julio Montoya Date: Thu, 20 Dec 2012 13:22:29 +0100 Subject: [PATCH] Adding swiftmailer and monolog for debug --- main/inc/global.inc.php | 8 +- main/inc/lib/events_email.class.php | 159 ++++++++++++---------------- main/inc/lib/main_api.lib.php | 38 +++---- 3 files changed, 94 insertions(+), 111 deletions(-) diff --git a/main/inc/global.inc.php b/main/inc/global.inc.php index 15ee063cfc..bf1b14ba61 100644 --- a/main/inc/global.inc.php +++ b/main/inc/global.inc.php @@ -289,6 +289,11 @@ $app->error(function (\Exception $e, $code) use ($app) { return new Response($response); });*/ +/* +use Symfony\Component\HttpKernel\Debug\ErrorHandler; +ErrorHandler::register(); + */ + //Filters $app->before(function() use ($app) { //Check the PHP version @@ -332,9 +337,6 @@ $app['default_layout'] = 'layout_1_col.tpl'; //Database constants require_once $lib_path.'database.constants.inc.php'; -//@todo use swift mail -//require_once $lib_path.'mail.lib.inc.php'; - require_once $lib_path.'fileManage.lib.php'; require_once $lib_path.'text.lib.php'; require_once $lib_path.'array.lib.php'; diff --git a/main/inc/lib/events_email.class.php b/main/inc/lib/events_email.class.php index 9cb24455d5..293230bc6d 100644 --- a/main/inc/lib/events_email.class.php +++ b/main/inc/lib/events_email.class.php @@ -1,13 +1,15 @@ ", $message); + if (isset($values["certificate_pdf_file"])) { + $message = str_replace("\n", "
", $message); @api_mail_html($recipient_name, $user_info["mail"], $subject, $message, $sender_name, $email_admin, null, array($values['certificate_pdf_file'])); + } else { + @api_mail_html($recipient_name, $user_info["mail"], $subject, $message, $sender_name, $email_admin); } - else - { - @api_mail($recipient_name, $user_info["mail"], $subject, $message, $sender_name, $email_admin); - } - + // If the mail only need to be send once (we know that thanks to the events.conf), we log it in the table - if ($event_config[$event_name]["sending_mail_once"]) - { - $sql = 'INSERT INTO ' . Database::get_main_table(TABLE_EVENT_SENT) . ' + if ($event_config[$event_name]["sending_mail_once"]) { + $sql = 'INSERT INTO '.Database::get_main_table(TABLE_EVENT_SENT).' (user_from, user_to, event_type_name) VALUES ('.$event_data["user_id"].', '.$id.' ,"'.Database::escape_string($event_name).'"); '; @@ -114,48 +105,43 @@ class EventsMail } } } - + // Second, we send to people linked to the event // So, we get everyone - $sql = 'SELECT u.user_id, u.language, u.email, u.firstname, u.lastname FROM ' . Database::get_main_table(TABLE_EVENT_TYPE_REL_USER) . ' ue + $sql = 'SELECT u.user_id, u.language, u.email, u.firstname, u.lastname FROM '.Database::get_main_table(TABLE_EVENT_TYPE_REL_USER).' ue INNER JOIN '.Database::get_main_table(TABLE_MAIN_USER).' u ON u.user_id = ue.user_id - WHERE event_type_name = "' . $event_name . '"'; + WHERE event_type_name = "'.$event_name.'"'; $result = Database::store_result(Database::query($sql), 'ASSOC'); - - foreach ($result as $key => $value) // for each of the linked users - { + + foreach ($result as $key => $value) { // for each of the linked users // we get the language - if ($event_data["prior_lang"] != null) // if $lang is not null, we use that lang - { + if ($event_data["prior_lang"] != null) { // if $lang is not null, we use that lang $language = $event_data["prior_lang"]; - } - else // else we get the user's lang - { - $sql = 'SELECT language FROM '.Database::get_main_table(TABLE_MAIN_USER).' + } else { // else we get the user's lang + $sql = 'SELECT language FROM '.Database::get_main_table(TABLE_MAIN_USER).' where user_id = '.$value["user_id"].' '; $result = Database::store_result(Database::query($sql), 'ASSOC'); $language = $result[0]["language"]; } - + // we get the message in the correct language (or in english if doesn't exist) $result = self::getMessage($event_name, $language); $message = ""; $subject = ""; self::getCorrectMessage($message, $subject, $language, $result); - + // replace the keycodes used in the message self::formatMessage($message, $subject, $event_config, $event_name, $event_data); - + // we send the mail $recipient_name = api_get_person_name($value['firstname'], $value['lastname']); @api_mail($recipient_name, $value["email"], $subject, $message, $sender_name, $email_admin); - + // If the mail only need to be send once (we know that thanks to the events.conf, we log it in the table - if ($event_config[$event_name]["sending_mail_once"]) - { - $sql = 'INSERT INTO ' . Database::get_main_table(TABLE_EVENT_SENT) . ' + if ($event_config[$event_name]["sending_mail_once"]) { + $sql = 'INSERT INTO '.Database::get_main_table(TABLE_EVENT_SENT).' (user_from, user_to, event_type_name) VALUES ('.$event_data["user_id"].', '.$value["user_id"].' , "'.Database::escape_string($event_name).'"); '; @@ -165,12 +151,12 @@ class EventsMail } /** - * Checks if a message in a language exists, if the event is activated + * Checks if a message in a language exists, if the event is activated * and if "manage event" is checked in admin panel. - * If yes to three, we can use this class, else we still use api_mail. + * If yes to three, we can use this class, else we still use api_mail. * * @param string $event_name - * @return boolean + * @return boolean */ public static function check_if_using_class($event_name) { if (api_get_setting('activate_email_template') === 'false') { @@ -178,74 +164,67 @@ class EventsMail } $current_language = api_get_interface_language(); - $sql = 'SELECT COUNT(*) as total FROM ' . Database::get_main_table(TABLE_EVENT_EMAIL_TEMPLATE) . ' em - INNER JOIN ' . Database::get_main_table(TABLE_MAIN_LANGUAGE) . ' l on em.language_id = l.id - WHERE em.event_type_name = "' . $event_name . '" and l.dokeos_folder = "'.$current_language.'" and em.activated = 1'; + $sql = 'SELECT COUNT(*) as total FROM '.Database::get_main_table(TABLE_EVENT_EMAIL_TEMPLATE).' em + INNER JOIN '.Database::get_main_table(TABLE_MAIN_LANGUAGE).' l on em.language_id = l.id + WHERE em.event_type_name = "'.$event_name.'" and l.dokeos_folder = "'.$current_language.'" and em.activated = 1'; - $exists = Database::store_result(Database::query($sql), 'ASSOC'); - if ($exists[0]["total"]) { - return true; + $exists = Database::store_result(Database::query($sql), 'ASSOC'); + if ($exists[0]["total"]) { + return true; } else { return false; } } - + /** * Get the record containing the good message and subject * * @param string $event_name * @param string $language - * @return array + * @return array */ - private static function getMessage($event_name, $language) - { - $sql = 'SELECT message, subject, l.dokeos_folder FROM ' . Database::get_main_table(TABLE_EVENT_EMAIL_TEMPLATE) . ' em - INNER JOIN ' . Database::get_main_table(TABLE_MAIN_LANGUAGE) . ' l on em.language_id = l.id - WHERE em.event_type_name = "' . $event_name . '" and (l.dokeos_folder = "' . $language . '" OR l.dokeos_folder = "english") and em.message <> "" + private static function getMessage($event_name, $language) { + $sql = 'SELECT message, subject, l.dokeos_folder FROM '.Database::get_main_table(TABLE_EVENT_EMAIL_TEMPLATE).' em + INNER JOIN '.Database::get_main_table(TABLE_MAIN_LANGUAGE).' l on em.language_id = l.id + WHERE em.event_type_name = "'.$event_name.'" and (l.dokeos_folder = "'.$language.'" OR l.dokeos_folder = "english") and em.message <> "" '; return Database::store_result(Database::query($sql), 'ASSOC'); } - + /** * Get the correct message, meaning in the specified language or in english if previous one doesn't exist * * @param string $message * @param string $subject * @param string $language - * @param array $result + * @param array $result */ - private static function getCorrectMessage(&$message, &$subject, $language, $result) - { - foreach ($result as $msg) - { - if ($msg["dokeos_folder"] == $language) - { + private static function getCorrectMessage(&$message, &$subject, $language, $result) { + foreach ($result as $msg) { + if ($msg["dokeos_folder"] == $language) { $message = $msg["message"]; $subject = $msg["subject"]; break; - } - else if ($msg["dokeos_folder"] == "english") - { + } else if ($msg["dokeos_folder"] == "english") { $message = $msg["message"]; $subject = $msg["subject"]; } } } - + /** * Replaces the ((key)) by the good values * * @param string $message * @param string $subject * @param array $event_config - * @param string $event_name + * @param string $event_name */ - private static function formatMessage(&$message, &$subject, $event_config, $event_name, &$event_data) - { - foreach ($event_config[$event_name]["available_keyvars"] as $key => $word) - { - $message = str_replace('((' . $key . '))', $event_data[$word], $message); - $subject = str_replace('((' . $key . '))', $event_data[$word], $subject); + private static function formatMessage(&$message, &$subject, $event_config, $event_name, &$event_data) { + foreach ($event_config[$event_name]["available_keyvars"] as $key => $word) { + $message = str_replace('(('.$key.'))', $event_data[$word], $message); + $subject = str_replace('(('.$key.'))', $event_data[$word], $subject); } } + } diff --git a/main/inc/lib/main_api.lib.php b/main/inc/lib/main_api.lib.php index 21dc3b0034..4057895c2d 100644 --- a/main/inc/lib/main_api.lib.php +++ b/main/inc/lib/main_api.lib.php @@ -6569,8 +6569,6 @@ function api_set_setting_last_update() { api_set_setting('settings_latest_update', api_get_utc_datetime()); } - - /** * Sends email using the phpmailer class * Sender name and email can be specified, if not specified @@ -6614,27 +6612,31 @@ function api_mail($recipient_name, $recipient_email, $subject, $message, $sender function api_mail_html($recipient_name, $recipient_email, $subject, $body, $sender_name = '', $sender_email = '', $extra_headers = null, $data_file = array(), $embedded_image = false) { global $app; + $reply_to_mail = $sender_email; + $reply_to_name = $sender_name; - if (($sender_email != '') && ($sender_name != '')) { - //$mail->AddReplyTo($sender_email, $sender_name); - } - $reply_to_mail = null; - $reply_to_name = null; if (isset($extra_headers['reply_to'])) { $reply_to_mail = $extra_headers['reply_to']['mail']; $reply_to_name = $extra_headers['reply_to']['name']; } - - $message = \Swift_Message::newInstance() - ->setSubject($subject) - ->setFrom(array($sender_email => $sender_name)) - ->setTo(array($recipient_email => $recipient_name)) - ->setReplyTo(array($reply_to_mail => $reply_to_name)) - ->setBody($body); - $status = $app['mailer']->send($message); - return $status; - - + try { + $message = \Swift_Message::newInstance() + ->setSubject($subject) + ->setFrom(array($sender_email => $sender_name)) + ->setTo(array($recipient_email => $recipient_name)) + ->setReplyTo(array($reply_to_mail => $reply_to_name)) + ->setBody($body); + + if (!empty($data_file)) { + // Attach it to the message + $message->attach(Swift_Attachment::fromPath($data_file['path']))->setFilename($data_file['filename']); + } + return $app['mailer']->send($message); + // Your code to send the email + } catch (Swift_RfcComplianceException $e) { + $app['monolog']->addError('Email address not valid:' . $e->getMessage()); + } + return false; $mail = new PHPMailer(); $mail->Mailer = $platform_email['SMTP_MAILER'];