From bc1a4c3a71bd6d0bf4b4a6eded4b759f1f5e2760 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tim=20D=C3=BCsterhus?= Date: Thu, 16 Jun 2016 21:34:41 +0200 Subject: [PATCH] Send daily email notifications --- .../templates/email_dailyNotification.tpl | 36 ++++++++++ .../DailyMailNotificationCronjob.class.php | 68 ++++++++++++------- 2 files changed, 78 insertions(+), 26 deletions(-) create mode 100644 com.woltlab.wcf/templates/email_dailyNotification.tpl diff --git a/com.woltlab.wcf/templates/email_dailyNotification.tpl b/com.woltlab.wcf/templates/email_dailyNotification.tpl new file mode 100644 index 0000000000..5609b93252 --- /dev/null +++ b/com.woltlab.wcf/templates/email_dailyNotification.tpl @@ -0,0 +1,36 @@ +{if $mimeType === 'text/plain'} +{capture assign='content'} +{lang}wcf.user.notification.mail.plaintext.intro{/lang} + +{foreach from=$notifications item='notification'} +{assign var='event' value=$notification[event]} +{assign var='notificationContent' value=$notification[notificationContent]} +{assign var='notificationType' value=$notification[notificationType]} +{if $notificationContent|is_array}{include file=$notificationContent[template] application=$notificationContent[application] variables=$notificationContent[variables]}{* +*}{else}{@$notificationContent}{/if} +{/foreach} + +{*lang}TODO: wcf.user.notification.mail.plaintext.outro{/lang*} +{/capture} +{include file='email_plaintext'} +{else} + {capture assign='content'} + {lang}wcf.user.notification.mail.html.intro{/lang} + + {foreach from=$notifications item='notification'} + {assign var='event' value=$notification[event]} + {assign var='notificationContent' value=$notification[notificationContent]} + {assign var='notificationType' value=$notification[notificationType]} +
+ {if $notificationContent|is_array} + {include file=$notificationContent[template] application=$notificationContent[application] variables=$notificationContent[variables]} + {else} + {@$notificationContent} + {/if} +
+ {/foreach} + + {*lang}TODO: wcf.user.notification.mail.html.outro{/lang*} + {/capture} + {include file='email_html'} +{/if} diff --git a/wcfsetup/install/files/lib/system/cronjob/DailyMailNotificationCronjob.class.php b/wcfsetup/install/files/lib/system/cronjob/DailyMailNotificationCronjob.class.php index 2d279c0872..771d5912c4 100644 --- a/wcfsetup/install/files/lib/system/cronjob/DailyMailNotificationCronjob.class.php +++ b/wcfsetup/install/files/lib/system/cronjob/DailyMailNotificationCronjob.class.php @@ -9,7 +9,10 @@ use wcf\data\user\UserList; use wcf\data\user\UserProfile; use wcf\system\cache\runtime\UserProfileRuntimeCache; use wcf\system\database\util\PreparedStatementConditionBuilder; -use wcf\system\mail\Mail; +use wcf\system\email\mime\MimePartFacade; +use wcf\system\email\mime\RecipientAwareTextMimePart; +use wcf\system\email\Email; +use wcf\system\email\UserMailbox; use wcf\system\user\notification\event\IUserNotificationEvent; use wcf\system\user\notification\UserNotificationHandler; use wcf\system\WCF; @@ -39,7 +42,7 @@ class DailyMailNotificationCronjob extends AbstractCronjob { $statement = WCF::getDB()->prepareStatement($sql); $statement->execute([ 0, - TIME_NOW - 3600 * 23, + TIME_NOW/*TODO - 3600 * 23*/, 0 ]); $userIDs = $statement->fetchAll(\PDO::FETCH_COLUMN); @@ -138,7 +141,7 @@ class DailyMailNotificationCronjob extends AbstractCronjob { $eventList->readObjects(); $eventObjects = $eventList->getObjects(); - foreach ($eventsToUser as $userID => $events) { + foreach ($eventsToUser as $userID => $notificationIDs) { if (!isset($users[$userID])) continue; $user = $users[$userID]; @@ -146,12 +149,7 @@ class DailyMailNotificationCronjob extends AbstractCronjob { if ($user->activationCode) continue; if ($user->banned) continue; - // add mail header - $message = $user->getLanguage()->getDynamicVariable('wcf.user.notification.mail.header', [ - 'user' => $user - ]); - - foreach ($events as $notificationID) { + $notifications = array_map(function ($notificationID) use ($notificationObjects, $eventObjects, $user, $objectTypes, $authors, $authorToNotification) { $notification = $notificationObjects[$notificationID]; $className = $eventObjects[$notification->eventID]->className; @@ -182,29 +180,47 @@ class DailyMailNotificationCronjob extends AbstractCronjob { } } - $message .= "\n\n"; - $message .= $class->getEmailMessage('daily'); - } + $message = $class->getEmailMessage('daily'); + if (is_array($message)) { + $variables = [ + 'notificationContent' => $message, + 'event' => $class, + 'notificationType' => 'daily' + ]; + if (isset($message['variables'])) { + $variables['variables'] = $message['variables']; + } + + return $variables; + } + else { + return [ + 'notificationContent' => $message, + 'event' => $class, + 'notificationType' => 'daily' + ]; + } + }, $notificationIDs); - // append notification mail footer - $token = $user->notificationMailToken; - if (!$token) { - // generate token if not present - $token = mb_substr(StringUtil::getHash(serialize([$user->userID, StringUtil::getRandomID()])), 0, 20); + // generate token if not present + if (!$user->notificationMailToken) { + $token = bin2hex(CryptoUtil::randomBytes(10)); $editor = new UserEditor($user); $editor->update(['notificationMailToken' => $token]); + + // reload user + $user = new User($user->userID); } - $message .= "\n\n"; - $message .= $user->getLanguage()->getDynamicVariable('wcf.user.notification.mail.daily.footer', [ - 'user' => $user, - 'token' => $token - ]); + $email = new Email(); + $email->setSubject($user->getLanguage()->getDynamicVariable('wcf.user.notification.mail.daily.subject', ['count' => count($notifications)])); + $email->addRecipient(new UserMailbox($user)); + + $html = new RecipientAwareTextMimePart('text/html', 'email_dailyNotification', 'wcf', ['notifications' => $notifications]); + $plainText = new RecipientAwareTextMimePart('text/plain', 'email_dailyNotification', 'wcf', ['notifications' => $notifications]); + $email->setBody(new MimePartFacade([$html, $plainText])); - // build mail - $mail = new Mail([$user->username => $user->email], $user->getLanguage()->getDynamicVariable('wcf.user.notification.mail.daily.subject', ['count' => count($events)]), $message); - $mail->setLanguage($user->getLanguage()); - $mail->send(); + $email->send(); } } } -- 2.20.1