From 8109b11b70dae2d3ee773065a95213bb0bee4081 Mon Sep 17 00:00:00 2001 From: Olivier PEREZ Date: Tue, 23 Dec 2014 00:58:00 +0100 Subject: [PATCH] Move mail sending to class \Framadate\Service\MailService --- .../Framadate/Services/MailService.php | 49 +++++++++++++++++++ app/classes/Framadate/Utils.php | 15 +++--- studs.php | 31 +++++++++++- 3 files changed, 87 insertions(+), 8 deletions(-) create mode 100644 app/classes/Framadate/Services/MailService.php diff --git a/app/classes/Framadate/Services/MailService.php b/app/classes/Framadate/Services/MailService.php new file mode 100644 index 0000000..233d767 --- /dev/null +++ b/app/classes/Framadate/Services/MailService.php @@ -0,0 +1,49 @@ +smtp_allowed = $smtp_allowed; + } + + public function isValidEmail($email) { + return filter_var($email, FILTER_VALIDATE_EMAIL); + } + + function send($to, $subject, $body, $param = '') { + if($this->smtp_allowed == true) { + mb_internal_encoding('UTF-8'); + + $subject = mb_encode_mimeheader(html_entity_decode($subject, ENT_QUOTES, 'UTF-8'), 'UTF-8', 'B', "\n", 9); + + $encoded_app = mb_encode_mimeheader(NOMAPPLICATION, 'UTF-8', 'B', "\n", 6); + $size_encoded_app = (6 + strlen($encoded_app)) % 75; + $size_admin_email = strlen(ADRESSEMAILADMIN); + + if (($size_encoded_app + $size_admin_email + 9) > 74) { + $folding = "\n"; + } else { + $folding = ''; + }; + + $from = sprintf("From: %s%s <%s>\n", $encoded_app, $folding, ADRESSEMAILADMIN); + + $headers = $from; + $headers .= 'Reply-To: ' . ADRESSEMAILREPONSEAUTO . "\n"; + $headers .= "MIME-Version: 1.0\n"; + $headers .= "Content-Type: text/plain; charset=UTF-8\n"; + $headers .= "Content-Transfer-Encoding: 8bit\n"; + $headers .= "Auto-Submitted:auto-generated\n"; + $headers .= 'Return-Path: <>'; + + $body = html_entity_decode($body, ENT_QUOTES, 'UTF-8') . _('\n--\n\n« La route est longue, mais la voie est libre… »\nFramasoft ne vit que par vos dons (déductibles des impôts).\nMerci d\'avance pour votre soutien http://soutenir.framasoft.org.'); + + mail($to, $subject, $body, $headers, $param); + } + } + +} + \ No newline at end of file diff --git a/app/classes/Framadate/Utils.php b/app/classes/Framadate/Utils.php index 0226eb7..a07719d 100644 --- a/app/classes/Framadate/Utils.php +++ b/app/classes/Framadate/Utils.php @@ -45,6 +45,10 @@ class Utils return (USE_REMOTE_USER && isset($_SERVER['REMOTE_USER'])) || isset($_SESSION['nom']); } + /** + * @param string $title + * @deprecated + */ public static function print_header($title = '') { global $lang; @@ -87,6 +91,7 @@ class Utils * * @param string $email Email address to check * @return bool True if valid. False if not valid. + * @deprecated */ public static function isValidEmail($email) { @@ -96,7 +101,7 @@ class Utils /** * Envoi un courrier avec un codage correct de To et Subject * Les en-têtes complémentaires ne sont pas gérés - * + * @deprecated */ public static function sendEmail( $to, $subject, $body, $headers='', $param='') { @@ -175,8 +180,7 @@ class Utils * Completly delete data about the given poll * TODO Move this function to FramaDB */ - public static function removeSondage($poll_id) - { + public static function removeSondage($poll_id) { global $connect; $prepared = $connect->prepare('DELETE FROM sujet_studs WHERE id_sondage = ?'); @@ -195,7 +199,7 @@ class Utils /** * Clean old poll (end_date < now). - * TODO Move this function to FramaDB + * TODO Move this function to PurgePollService */ public static function cleaningOldPolls($log_txt) { global $connect; @@ -216,8 +220,7 @@ class Utils * This method pretty prints an object to the page framed by pre tags. * @param mixed $object The object to print. */ - public static function debug($object) - { + public static function debug($object) { echo '
';
         print_r($object);
         echo '
'; diff --git a/studs.php b/studs.php index 08a0206..437e462 100644 --- a/studs.php +++ b/studs.php @@ -18,6 +18,7 @@ */ use Framadate\Services\PollService; use Framadate\Services\InputService; +use Framadate\Services\MailService; use Framadate\Message; use Framadate\Utils; @@ -25,6 +26,7 @@ include_once __DIR__ . '/app/inc/init.php'; /* Variables */ /* --------- */ + $poll_id = null; $poll = null; $message = null; @@ -35,6 +37,31 @@ $editingVoteId = 0; $pollService = new PollService($connect); $inputService = new InputService(); +$mailService = new MailService($config['use_smtp']); + +/* Functions */ +/*-----------*/ + +/** + * Send a notification to the poll admin to notify him about an update. + * + * @param $poll Object The poll + * @param $mailService MailService The mail service + */ +function sendUpdateNotification($poll, $mailService) { + if ($poll->receiveNewVotes && !isset($_SESSION['mail_sent'][$poll->poll_id])) { + + $subject = '[' . NOMAPPLICATION . '] ' . _('Poll\'s participation') . ' : ' . html_entity_decode($poll->title, ENT_QUOTES, 'UTF-8'); + $message = html_entity_decode('"$nom" ', ENT_QUOTES, 'UTF-8') . + _('has filled a line.\nYou can find your poll at the link') . " :\n\n" . + Utils::getUrlSondage($poll->admin_poll_id, true) . " \n\n" . + _('Thanks for your confidence.') . "\n" . NOMAPPLICATION; + + $mailService->send($poll->admin_mail, $subject, $message); + + $_SESSION["mail_sent"][$poll->poll_id] = true; + } +} /* PAGE */ /* ---- */ @@ -79,7 +106,7 @@ if (!empty($_POST['save'])) { // Save edition of an old vote $result = $pollService->updateVote($poll_id, $editedVote, $choices); if ($result) { $message = new Message('success', _('Update vote successfully.')); - // TODO Send mail to notify the poll admin + sendUpdateNotification($poll, $mailService); } else { $message = new Message('danger', _('Update vote failed.')); } @@ -100,7 +127,7 @@ if (!empty($_POST['save'])) { // Save edition of an old vote $result = $pollService->addVote($poll_id, $name, $choices); if ($result) { $message = new Message('success', _('Update vote successfully.')); - // TODO Send mail to notify the poll admin + sendUpdateNotification($poll, $mailService); } else { $message = new Message('danger', _('Update vote failed.')); }