Send daily email notifications
authorTim Düsterhus <duesterhus@woltlab.com>
Thu, 16 Jun 2016 19:34:41 +0000 (21:34 +0200)
committerTim Düsterhus <duesterhus@woltlab.com>
Fri, 29 Jul 2016 14:25:53 +0000 (16:25 +0200)
com.woltlab.wcf/templates/email_dailyNotification.tpl [new file with mode: 0644]
wcfsetup/install/files/lib/system/cronjob/DailyMailNotificationCronjob.class.php

diff --git a/com.woltlab.wcf/templates/email_dailyNotification.tpl b/com.woltlab.wcf/templates/email_dailyNotification.tpl
new file mode 100644 (file)
index 0000000..5609b93
--- /dev/null
@@ -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]}
+       <div>
+               {if $notificationContent|is_array}
+                       {include file=$notificationContent[template] application=$notificationContent[application] variables=$notificationContent[variables]}
+               {else}
+                       {@$notificationContent}
+               {/if}
+       </div>
+       {/foreach}
+
+       {*lang}TODO: wcf.user.notification.mail.html.outro{/lang*}
+       {/capture}
+       {include file='email_html'}
+{/if}
index 2d279c0872491027ddbd3ce1e9e88852abecab9f..771d5912c46077c79061b1c7340d31895bac6486 100644 (file)
@@ -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();
                }
        }
 }