Convert MailWorker to new email API
authorTim Düsterhus <duesterhus@woltlab.com>
Sun, 31 Jul 2016 17:54:15 +0000 (19:54 +0200)
committerTim Düsterhus <duesterhus@woltlab.com>
Sun, 31 Jul 2016 17:58:23 +0000 (19:58 +0200)
com.woltlab.wcf/templates/email_mailWorker.tpl [new file with mode: 0644]
wcfsetup/install/files/lib/system/worker/MailWorker.class.php
wcfsetup/install/files/lib/system/worker/SendNewPasswordWorker.class.php

diff --git a/com.woltlab.wcf/templates/email_mailWorker.tpl b/com.woltlab.wcf/templates/email_mailWorker.tpl
new file mode 100644 (file)
index 0000000..96ebd95
--- /dev/null
@@ -0,0 +1,9 @@
+{assign var='text' value="\x7Busername\x7D"|str_replace:$mailbox->getUser()->username:$text}{if $mimeType === 'text/plain'}
+{include file='email_plaintext' content=$text}
+{else}
+       {if $enableHTML}
+               {include file='email_html' content=$text}
+       {else}
+               {include file='email_html' content=$text|newlineToBreak}
+       {/if}
+{/if}
index 7a7286a1607494cab30df73bd5baa2dc287e683e..a3125eee3881245d09ff48d3c09279beb2037eea 100644 (file)
@@ -1,10 +1,15 @@
 <?php
 namespace wcf\system\worker;
 use wcf\data\user\User;
+use wcf\system\background\BackgroundQueueHandler;
 use wcf\system\clipboard\ClipboardHandler;
 use wcf\system\database\util\PreparedStatementConditionBuilder;
+use wcf\system\email\mime\MimePartFacade;
+use wcf\system\email\mime\RecipientAwareTextMimePart;
+use wcf\system\email\Email;
+use wcf\system\email\Mailbox;
+use wcf\system\email\UserMailbox;
 use wcf\system\exception\SystemException;
-use wcf\system\mail\Mail;
 use wcf\system\request\LinkHandler;
 use wcf\system\WCF;
 
@@ -102,6 +107,25 @@ class MailWorker extends AbstractWorker {
         * @inheritDoc
         */
        public function execute() {
+               $email = new Email();
+               $email->setSubject($this->mailData['subject']);
+               $from = new Mailbox($this->mailData['from']);
+               $email->setSender($from);
+               $email->setReplyTo($from);
+               $variables = [
+                       'text' => $this->mailData['text'],
+                       'enableHTML' => $this->mailData['enableHTML'] ? true : false
+               ];
+               if ($this->mailData['enableHTML']) {
+                       $email->setBody(new RecipientAwareTextMimePart('text/html', 'email_mailWorker', 'wcf', $variables));
+               }
+               else {
+                       $email->setBody(new MimePartFacade([
+                               new RecipientAwareTextMimePart('text/html', 'email_mailWorker', 'wcf', $variables),
+                               new RecipientAwareTextMimePart('text/plain', 'email_mailWorker', 'wcf', $variables)
+                       ]));
+               }
+               
                // get users
                $sql = "SELECT          user_option.*, user.*
                        FROM            wcf".WCF_N."_user user
@@ -115,25 +139,23 @@ class MailWorker extends AbstractWorker {
                        $user = new User(null, $row);
                        $adminCanMail = $user->adminCanMail;
                        if ($adminCanMail === null || $adminCanMail) {
-                               $this->sendMail($user);
+                               $this->sendMail($email, $user);
                        }
                }
        }
        
        /**
-        * Sends the mail to given user.
+        * Sends the given blueprint (Email without recipients) to the given user.
         * 
-        * @param       \wcf\data\user\User     $user
+        * @param       Email   $blueprint
+        * @param       User    $user
         */
-       protected function sendMail(User $user) {
-               try {
-                       $mail = new Mail([$user->username => $user->email], $this->mailData['subject'], str_replace('{$username}', $user->username, $this->mailData['text']), $this->mailData['from']);
-                       if ($this->mailData['enableHTML']) $mail->setContentType('text/html');
-                       $mail->setLanguage($user->getLanguage());
-                       $mail->send();
-               }
-               catch (SystemException $e) {
-                       // ignore errors
+       protected function sendMail(Email $blueprint, User $user) {
+               $email = clone $blueprint;
+               $email->addRecipient(new UserMailbox($user));
+               $jobs = $email->getJobs();
+               foreach ($jobs as $job) {
+                       BackgroundQueueHandler::getInstance()->performJob($job);
                }
        }
        
index 46c8dff86231353b61d0e607be527258e3a91091..81ce47de48b9ab1b6e209d76ca739c96d96ad5df 100644 (file)
@@ -4,6 +4,7 @@ use wcf\data\user\User;
 use wcf\data\user\UserAction;
 use wcf\data\user\UserEditor;
 use wcf\data\user\UserList;
+use wcf\system\background\BackgroundQueueHandler;
 use wcf\system\clipboard\ClipboardHandler;
 use wcf\system\email\mime\MimePartFacade;
 use wcf\system\email\mime\RecipientAwareTextMimePart;
@@ -125,7 +126,10 @@ class SendNewPasswordWorker extends AbstractWorker {
                        new RecipientAwareTextMimePart('text/html', 'email_sendNewPassword'),
                        new RecipientAwareTextMimePart('text/plain', 'email_sendNewPassword')
                ]));
-               $email->send();
+               $jobs = $email->getJobs();
+               foreach ($jobs as $job) {
+                       BackgroundQueueHandler::getInstance()->performJob($job);
+               }
        }
        
        /**