Allow overriding the envelope sender in Email::getJobs()
authorTim Düsterhus <duesterhus@woltlab.com>
Tue, 14 Jun 2016 21:36:06 +0000 (23:36 +0200)
committerTim Düsterhus <duesterhus@woltlab.com>
Tue, 14 Jun 2016 21:44:07 +0000 (23:44 +0200)
wcfsetup/install/files/lib/system/background/job/EmailDeliveryBackgroundJob.class.php
wcfsetup/install/files/lib/system/email/Email.class.php
wcfsetup/install/files/lib/system/email/transport/DebugEmailTransport.class.php
wcfsetup/install/files/lib/system/email/transport/EmailTransport.class.php
wcfsetup/install/files/lib/system/email/transport/PhpEmailTransport.class.php
wcfsetup/install/files/lib/system/email/transport/SmtpEmailTransport.class.php

index f5ed2cf89d0bd642f86da80b64ab6f0d97f91e0e..77cdc4edc9bc1d9f7a389bb89dea2f8061fc922a 100644 (file)
@@ -22,11 +22,17 @@ class EmailDeliveryBackgroundJob extends AbstractBackgroundJob {
         */
        protected $email;
        
+       /**
+        * sender mailbox
+        * @var \wcf\system\email\Mailbox
+        */
+       protected $envelopeFrom;
+       
        /**
         * recipient mailbox
         * @var \wcf\system\email\Mailbox
         */
-       protected $mailbox;
+       protected $envelopeTo;
        
        /**
         * instance of the default transport
@@ -38,12 +44,14 @@ class EmailDeliveryBackgroundJob extends AbstractBackgroundJob {
         * Creates the job using the given the email and the destination mailbox.
         * 
         * @param       \wcf\system\email\Email         $email
-        * @param       \wcf\system\email\Mailbox       $mailbox
+        * @param       \wcf\system\email\Mailbox       $envelopeFrom
+        * @param       \wcf\system\email\Mailbox       $envelopeTo
         * @see         \wcf\system\email\transport\EmailTransport
         */
-       public function __construct(Email $email, Mailbox $mailbox) {
+       public function __construct(Email $email, Mailbox $envelopeFrom, Mailbox $envelopeTo) {
                $this->email = $email;
-               $this->mailbox = $mailbox;
+               $this->envelopeFrom = $envelopeFrom;
+               $this->envelopeTo = $envelopeTo;
        }
        
        /**
@@ -72,7 +80,7 @@ class EmailDeliveryBackgroundJob extends AbstractBackgroundJob {
                }
                
                try {
-                       self::$transport->deliver($this->email, $this->mailbox);
+                       self::$transport->deliver($this->email, $this->envelopeFrom, $this->envelopeTo);
                }
                catch (PermanentFailure $e) {
                        // no need for retrying. Eat Exception and log the error.
index 0dc8c46538ecbaa4a7e4e831c30e6067b8981569..5f2dbc9d7fcec69c64e46b947ebebab2b979cd45 100644 (file)
@@ -492,6 +492,7 @@ class Email {
                        $data = [
                                'mail' => $mail,
                                'recipient' => $recipient,
+                               'sender' => $mail->getSender(),
                                'skip' => false
                        ];
                        EventHandler::getInstance()->fireAction($this, 'getJobs', $data);
@@ -499,7 +500,7 @@ class Email {
                        // an event decided that this email should be skipped
                        if ($data['skip']) continue;
                        
-                       $jobs[] = new EmailDeliveryBackgroundJob($mail, $recipient['mailbox']);
+                       $jobs[] = new EmailDeliveryBackgroundJob($mail, $data['sender'], $data['recipient']['mailbox']);
                }
                
                return $jobs;
index fd19a4b980667cac101e50d74e540590cf35a3ac..bf0d94d889ec8c63e6dce07dd0031083a3c1dd99 100644 (file)
@@ -39,10 +39,11 @@ class DebugEmailTransport implements EmailTransport {
         * Writes the given $email into the mbox.
         * 
         * @param       \wcf\system\email\Email         $email
+        * @param       \wcf\system\email\Mailbox       $envelopeFrom
         * @param       \wcf\system\email\Mailbox       $envelopeTo
         */
-       public function deliver(Email $email, Mailbox $envelopeTo) {
-               $this->mbox->write("From ".$email->getSender()->getAddress()." ".DateUtil::getDateTimeByTimestamp(TIME_NOW)->format('D M d H:i:s Y')."\r\n");
+       public function deliver(Email $email, Mailbox $envelopeFrom, Mailbox $envelopeTo) {
+               $this->mbox->write("From ".$envelopeFrom->getAddress()." ".DateUtil::getDateTimeByTimestamp(TIME_NOW)->format('D M d H:i:s Y')."\r\n");
                $this->mbox->write("Delivered-To: ".$envelopeTo->getAddress()."\r\n");
                $this->mbox->write($email->getEmail());
                $this->mbox->write("\r\n");
index 1c193b4efb21470e0a6d5cc8a46bafbec53368e3..1a3b4c82c631b79d7f5e587c7996afa8c917680d 100644 (file)
@@ -19,7 +19,8 @@ interface EmailTransport {
         * Delivers the given $email to the given Mailbox as the recipient.
         * 
         * @param       \wcf\system\email\Email         $email
+        * @param       \wcf\system\email\Mailbox       $envelopeFrom
         * @param       \wcf\system\email\Mailbox       $envelopeTo
         */
-       public function deliver(Email $email, Mailbox $envelopeTo);
+       public function deliver(Email $email, Mailbox $envelopeFrom, Mailbox $envelopeTo);
 }
index 3f97c14313d46d162056ddd96d9ae97ba83fb1ff..3d706a2605a7fbbd8059f3305f73f70eaf59712b 100644 (file)
@@ -21,9 +21,10 @@ class PhpEmailTransport implements EmailTransport {
         * Delivers the given email via mail().
         * 
         * @param       \wcf\system\email\Email         $email
+        * @param       \wcf\system\email\Mailbox       $envelopeFrom
         * @param       \wcf\system\email\Mailbox       $envelopeTo
         */
-       public function deliver(Email $email, Mailbox $envelopeTo) {
+       public function deliver(Email $email, Mailbox $envelopeFrom, Mailbox $envelopeTo) {
                $headers = array_filter($email->getHeaders(), function ($item) {
                        // filter out headers that are either
                        //   a) automatically added by PHP
@@ -42,7 +43,7 @@ class PhpEmailTransport implements EmailTransport {
                }, $headers));
                
                if (MAIL_USE_F_PARAM) {
-                       $return = mail($envelopeTo->getAddress(), $email->getSubject(), StringUtil::unifyNewlines($email->getBodyString()), $headers, '-f'.$email->getSender()->getAddress());
+                       $return = mail($envelopeTo->getAddress(), $email->getSubject(), StringUtil::unifyNewlines($email->getBodyString()), $headers, '-f'.$envelopeFrom->getAddress());
                }
                else {
                        $return = mail($envelopeTo->getAddress(), $email->getSubject(), StringUtil::unifyNewlines($email->getBodyString()), $headers);
index 87dff5a03579b10d8c6b20c2f87a65d96c192712..6a6c6c84ac4f53618ae0e8e8a67a038769139694 100644 (file)
@@ -314,13 +314,14 @@ class SmtpEmailTransport implements EmailTransport {
         * Delivers the given email using SMTP.
         * 
         * @param       Email           $email
+        * @param       Mailbox         $envelopeFrom
         * @param       Mailbox         $envelopeTo
         * @throws      \Exception
         * @throws      PermanentFailure
         * @throws      TransientFailure
         * @throws      SystemException
         */
-       public function deliver(Email $email, Mailbox $envelopeTo) {
+       public function deliver(Email $email, Mailbox $envelopeFrom, Mailbox $envelopeTo) {
                // delivery is locked
                if ($this->locked instanceof \Exception) {
                        throw $this->locked;