From: Tim Düsterhus Date: Wed, 15 Jun 2016 18:14:31 +0000 (+0200) Subject: Make use of new email API in UserNotificationHandler::sendInstantMailNotification() X-Git-Tag: 3.0.0_Beta_1~916^2~1^2~14 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=69fa057bb4041d600489146e054cc0be953a1f11;p=GitHub%2FWoltLab%2FWCF.git Make use of new email API in UserNotificationHandler::sendInstantMailNotification() --- diff --git a/com.woltlab.wcf/templates/email_html.tpl b/com.woltlab.wcf/templates/email_html.tpl index 7d3ee1d099..bcfdf3da55 100644 --- a/com.woltlab.wcf/templates/email_html.tpl +++ b/com.woltlab.wcf/templates/email_html.tpl @@ -51,9 +51,11 @@ + {if $beforeContent|isset}{@$beforeContent}{/if}
{@$content}
+ {if $afterContent|isset}{@$afterContent}{/if} {capture assign='footer'} {hascontent} --
diff --git a/com.woltlab.wcf/templates/email_notification.tpl b/com.woltlab.wcf/templates/email_notification.tpl new file mode 100644 index 0000000000..0595d9ab17 --- /dev/null +++ b/com.woltlab.wcf/templates/email_notification.tpl @@ -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'} +

{lang}wcf.user.notification.mail.html.headline{/lang}

+ {lang}wcf.user.notification.mail.html.intro{/lang} + + {@$notificationContent} + + {lang}wcf.user.notification.mail.html.outro{/lang} + {/capture} + {include file='email_html'} +{/if} diff --git a/com.woltlab.wcf/templates/email_plaintext.tpl b/com.woltlab.wcf/templates/email_plaintext.tpl index f428196ae9..50bdae538a 100644 --- a/com.woltlab.wcf/templates/email_plaintext.tpl +++ b/com.woltlab.wcf/templates/email_plaintext.tpl @@ -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 *} diff --git a/wcfsetup/install/files/lib/system/email/mime/RecipientAwareTextMimePart.class.php b/wcfsetup/install/files/lib/system/email/mime/RecipientAwareTextMimePart.class.php index 122025b641..bddec4650a 100644 --- a/wcfsetup/install/files/lib/system/email/mime/RecipientAwareTextMimePart.class.php +++ b/wcfsetup/install/files/lib/system/email/mime/RecipientAwareTextMimePart.class.php @@ -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; } } diff --git a/wcfsetup/install/files/lib/system/user/notification/UserNotificationHandler.class.php b/wcfsetup/install/files/lib/system/user/notification/UserNotificationHandler.class.php index 03d58838d0..cc8e876d3b 100644 --- a/wcfsetup/install/files/lib/system/user/notification/UserNotificationHandler.class.php +++ b/wcfsetup/install/files/lib/system/user/notification/UserNotificationHandler.class.php @@ -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(); } /** diff --git a/wcfsetup/install/lang/de.xml b/wcfsetup/install/lang/de.xml index 25b292328b..2d10d43571 100644 --- a/wcfsetup/install/lang/de.xml +++ b/wcfsetup/install/lang/de.xml @@ -3241,15 +3241,17 @@ Die E-Mail-Adresse des neuen Benutzers lautet: {@$user->email} username} folgt {if LANGUAGE_USE_INFORMAL_VARIANT}dir{else}Ihnen{/if}.]]> 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}.]]> - + + getUser()->username},]]> + eventID}&userID={@$user->userID}&token={@$token}{/link}]]> - username},]]> - +{link controller='NotificationDisable' isEmail=true}eventID={@$event->eventID}&userID={@$mailbox->getUser()->userID}&token={@$mailbox->getUser()->notificationMailToken}{/link}]]> 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}]]> +