Make use of new email API in UserNotificationHandler::sendInstantMailNotification()
authorTim Düsterhus <duesterhus@woltlab.com>
Wed, 15 Jun 2016 18:14:31 +0000 (20:14 +0200)
committerTim Düsterhus <duesterhus@woltlab.com>
Fri, 29 Jul 2016 14:25:53 +0000 (16:25 +0200)
com.woltlab.wcf/templates/email_html.tpl
com.woltlab.wcf/templates/email_notification.tpl [new file with mode: 0644]
com.woltlab.wcf/templates/email_plaintext.tpl
wcfsetup/install/files/lib/system/email/mime/RecipientAwareTextMimePart.class.php
wcfsetup/install/files/lib/system/user/notification/UserNotificationHandler.class.php
wcfsetup/install/lang/de.xml

index 7d3ee1d0994751181374ae8c07526b86004ee310..bcfdf3da55676621946c8847cca77641642c4ed4 100644 (file)
                </style>
        </head>
        <body>
+       {if $beforeContent|isset}{@$beforeContent}{/if}
        <div class="content">
                {@$content}
        </div>
+       {if $afterContent|isset}{@$afterContent}{/if}
        {capture assign='footer'}
        {hascontent}
        <span style="font-size: 0;">-- <br></span>
diff --git a/com.woltlab.wcf/templates/email_notification.tpl b/com.woltlab.wcf/templates/email_notification.tpl
new file mode 100644 (file)
index 0000000..0595d9a
--- /dev/null
@@ -0,0 +1,20 @@
+{if $mimeType === 'text/plain'}
+{capture assign='content'}
+{lang}wcf.user.notification.mail.plaintext.intro{/lang}
+
+{@$notificationContent}
+
+{lang}wcf.user.notification.mail.plaintext.outro{/lang}
+{/capture}
+{include file='email_plaintext'}
+{else}
+       {capture assign='content'}
+       <h1>{lang}wcf.user.notification.mail.html.headline{/lang}</h1>
+       {lang}wcf.user.notification.mail.html.intro{/lang}
+
+       {@$notificationContent}
+
+       {lang}wcf.user.notification.mail.html.outro{/lang}
+       {/capture}
+       {include file='email_html'}
+{/if}
index f428196ae9f330203beec3e111354f81acd7b9d1..50bdae538a0bf0330a9c4a2b9457e86985f36d11 100644 (file)
@@ -1,4 +1,8 @@
+{if $beforeContent|isset}{@$beforeContent}{/if}
+
 {@$content}
+
+{if $afterContent|isset}{@$afterContent}{/if}
 {hascontent} {* this line ends with a space *}
 
 -- {* this line ends with a space *}
index 122025b641e44307f920bc7e89eb2644bd4e12e7..bddec4650aeb08077ae997090a93ca245f377fcd 100644 (file)
@@ -44,9 +44,9 @@ class RecipientAwareTextMimePart extends TextMimePart implements IRecipientAware
         * @param       string  $mimeType       Mime type to provide in the email. You *must* not provide a charset. UTF-8 will be used automatically.
         * @param       string  $template       Template to evaluate
         * @param       string  $application    Application of the template to evaluate (default: wcf)
-        * @param       string  $content        Content of this text part (this is passed to the template).
+        * @param       mixed   $content        Content of this text part. Passend as 'content' to the template. If it is an array it will additionally be merged with the template variables.
         */
-       public function __construct($mimeType, $template, $application = 'wcf', $content = '') {
+       public function __construct($mimeType, $template, $application = 'wcf', $content = null) {
                parent::__construct($content, $mimeType);
                
                $this->template = $template;
@@ -113,11 +113,17 @@ class RecipientAwareTextMimePart extends TextMimePart implements IRecipientAware
        protected function getTemplateVariables() {
                $styleCache = StyleCacheBuilder::getInstance()->getData();
                
-               return [
+               $result = [
                        'content' => $this->content,
                        'mimeType' => $this->mimeType,
                        'mailbox' => $this->mailbox,
                        'style' => new ActiveStyle($styleCache['styles'][$styleCache['default']])
                ];
+               
+               if (is_array($this->content)) {
+                       return array_merge($result, $this->content);
+               }
+               
+               return $result;
        }
 }
index 03d58838d068affce058095018b83545c9507a58..cc8e876d3baeb92b9bd4ff838248fbc40fd29b88 100644 (file)
@@ -14,14 +14,17 @@ use wcf\system\cache\runtime\UserProfileRuntimeCache;
 use wcf\system\database\util\PreparedStatementConditionBuilder;
 use wcf\system\event\EventHandler;
 use wcf\system\exception\SystemException;
-use wcf\system\mail\Mail;
+use wcf\system\email\mime\AbstractMimePart;
+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\object\type\IUserNotificationObjectType;
 use wcf\system\user\notification\object\IUserNotificationObject;
 use wcf\system\user\storage\UserStorageHandler;
 use wcf\system\SingletonFactory;
 use wcf\system\WCF;
-use wcf\util\StringUtil;
+use wcf\util\CryptoUtil;
 
 /**
  * Handles user notifications.
@@ -646,32 +649,34 @@ class UserNotificationHandler extends SingletonFactory {
                $event->setLanguage($user->getLanguage());
                WCF::setLanguage($user->getLanguage()->languageID);
                
-               // add mail header
-               $message = $user->getLanguage()->getDynamicVariable('wcf.user.notification.mail.header', [
-                       'user' => $user
-                       ])."\n\n";
-               
-               // get message
-               $message .= $event->getEmailMessage();
-               
-               // 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".$user->getLanguage()->getDynamicVariable('wcf.user.notification.mail.footer', [
-                       'user' => $user,
-                       'token' => $token,
-                       'notification' => $notification
-                       ]);
-               
-               // build mail
-               $mail = new Mail([$user->username => $user->email], $user->getLanguage()->getDynamicVariable('wcf.user.notification.mail.subject', ['title' => $event->getEmailTitle()]), $message);
-               $mail->setLanguage($user->getLanguage());
-               $mail->send();
+               
+               $email = new Email();
+               $email->setSubject($user->getLanguage()->getDynamicVariable('wcf.user.notification.mail.subject', [
+                       'title' => $event->getEmailTitle()
+               ]));
+               $email->addRecipient(new UserMailbox($user));
+               
+               $message = $event->getEmailMessage('instant');
+               if ($message instanceof AbstractMimePart) {
+                       $email->setBody($message);
+               }
+               else {
+                       $email->setBody(new RecipientAwareTextMimePart('text/plain', 'email_notification', 'wcf', [
+                               'notificationContent' => $message,
+                               'event' => $event
+                       ]));
+               }
+               
+               $email->send();
        }
        
        /**
index 25b292328b3a9e148560316be971cd5b3dc8673b..2d10d4357121858ecf6d5446774f41040162b443 100644 (file)
@@ -3241,15 +3241,17 @@ Die E-Mail-Adresse des neuen Benutzers lautet: {@$user->email}
                <item name="wcf.user.notification.follow.mail"><![CDATA[{@$author->username} folgt {if LANGUAGE_USE_INFORMAL_VARIANT}dir{else}Ihnen{/if}.]]></item>
                <item name="wcf.user.notification.follow.mail.stacked"><![CDATA[{if $count < 4}{@$authors[0]->username}{if $count == 2} und {else}, {/if}{@$authors[1]->username}{if $count == 3} und {@$authors[2]->username}{/if}{else}{@$authors[0]->username} und {#$others} weiteren{/if} folgen {if LANGUAGE_USE_INFORMAL_VARIANT}dir{else}Ihnen{/if}.]]></item>
                <item name="wcf.user.notification.mail.disabled"><![CDATA[Die E-Mail-Benachrichtigung wurde erfolgreich abgeschaltet.]]></item>
-               <item name="wcf.user.notification.mail.footer"><![CDATA[Diese E-Mail ist eine automatische Benachrichtigung. BITTE {if LANGUAGE_USE_INFORMAL_VARIANT}ANTWORTE{else}ANTWORTEN SIE{/if} NICHT AUF DIESE E-MAIL.
+               
+               <!-- Emails -->
+               <item name="wcf.user.notification.mail.subject"><![CDATA[Neue Benachrichtigung: {@$title}]]></item>
+               <item name="wcf.user.notification.mail.plaintext.intro"><![CDATA[Hallo {@$mailbox->getUser()->username},]]></item>
+               <item name="wcf.user.notification.mail.plaintext.outro"><![CDATA[Diese E-Mail ist eine automatische Benachrichtigung. BITTE {if LANGUAGE_USE_INFORMAL_VARIANT}ANTWORTE{else}ANTWORTEN SIE{/if} NICHT AUF DIESE E-MAIL.
                
 {if LANGUAGE_USE_INFORMAL_VARIANT}Du kannst{else}Sie können{/if} die Einstellungen für {if LANGUAGE_USE_INFORMAL_VARIANT}deine{else}Ihre{/if} Benachrichtigungen auf {@PAGE_TITLE|language} unter folgender URL detailliert konfigurieren:
 {link controller='NotificationSettings' isEmail=true}{/link} 
 
 {if LANGUAGE_USE_INFORMAL_VARIANT}Möchtest du{else}Möchten Sie{/if} diese E-Mail-Benachrichtigung in Zukunft nicht mehr erhalten, {if LANGUAGE_USE_INFORMAL_VARIANT}kannst du{else}können Sie{/if} folgenden Link aufrufen, um die Benachrichtigung abzuschalten:
-{link controller='NotificationDisable' isEmail=true}eventID={@$notification->eventID}&userID={@$user->userID}&token={@$token}{/link}]]></item>
-               <item name="wcf.user.notification.mail.header"><![CDATA[Hallo {@$user->username},]]></item>
-               <item name="wcf.user.notification.mail.subject"><![CDATA[Neue Benachrichtigung: {@$title}]]></item>
+{link controller='NotificationDisable' isEmail=true}eventID={@$event->eventID}&userID={@$mailbox->getUser()->userID}&token={@$mailbox->getUser()->notificationMailToken}{/link}]]></item>
                <item name="wcf.user.notification.mail.daily.subject"><![CDATA[{if $count == 1}Neue Benachrichtigung{else}{#$count} neue Benachrichtigungen{/if}]]></item>
                <item name="wcf.user.notification.mail.daily.footer"><![CDATA[Diese E-Mail ist eine automatische Benachrichtigung. BITTE {if LANGUAGE_USE_INFORMAL_VARIANT}ANTWORTE{else}ANTWORTEN SIE{/if} NICHT AUF DIESE E-MAIL.
                
@@ -3258,6 +3260,7 @@ Die E-Mail-Adresse des neuen Benutzers lautet: {@$user->email}
 
 {if LANGUAGE_USE_INFORMAL_VARIANT}Möchtest du{else}Möchten Sie{/if} diese E-Mail-Benachrichtigung in Zukunft nicht mehr erhalten, {if LANGUAGE_USE_INFORMAL_VARIANT}kannst du{else}können Sie{/if} folgenden Link aufrufen, um die Benachrichtigung abzuschalten:
 {link controller='NotificationDisable' isEmail=true}userID={@$user->userID}&token={@$token}{/link}]]></item>
+       
                <item name="wcf.user.notification.mailNotificationType.none"><![CDATA[Keine E-Mail-Benachrichtigung]]></item>
                <item name="wcf.user.notification.mailNotificationType.instant"><![CDATA[Sofortige E-Mail-Benachrichtigung]]></item>
                <item name="wcf.user.notification.mailNotificationType.daily"><![CDATA[Tägliche E-Mail-Benachrichtigung]]></item>