Merge pull request #12641 from mfb/reply-forwarding
authorMonish Deb <monish.deb@jmaconsulting.biz>
Sun, 24 Feb 2019 17:01:00 +0000 (22:31 +0530)
committerGitHub <noreply@github.com>
Sun, 24 Feb 2019 17:01:00 +0000 (22:31 +0530)
CiviMail: Fix reply forwarding for mailers with From: and Return-path: limitations

1  2 
CRM/Mailing/Event/BAO/Reply.php

index 31f2bae2c39f3d112768146eaccfa5aadea73bb2,d7851563b8a9f653ad42b221c59fa33a58267d09..3bfc007104065f9a40d99ddc0094280e64bd0701
@@@ -3,7 -3,7 +3,7 @@@
   +--------------------------------------------------------------------+
   | CiviCRM version 5                                                  |
   +--------------------------------------------------------------------+
 - | Copyright CiviCRM LLC (c) 2004-2018                                |
 + | Copyright CiviCRM LLC (c) 2004-2019                                |
   +--------------------------------------------------------------------+
   | This file is a part of CiviCRM.                                    |
   |                                                                    |
@@@ -28,7 -28,7 +28,7 @@@
  /**
   *
   * @package CRM
 - * @copyright CiviCRM LLC (c) 2004-2018
 + * @copyright CiviCRM LLC (c) 2004-2019
   */
  
  require_once 'Mail/mime.php';
@@@ -119,6 -119,7 +119,7 @@@ class CRM_Mailing_Event_BAO_Reply exten
      $contacts = CRM_Contact_BAO_Contact::getTableName();
      $domain_id = CRM_Core_Config::domainID();
      $domainValues = civicrm_api3('Domain', 'get', array('sequential' => 1, 'id' => $domain_id));
+     $fromEmail = CRM_Core_BAO_Domain::getNoReplyEmailAddress();
  
      $eq = new CRM_Core_DAO();
      $eq->query("SELECT     $contacts.display_name as display_name,
        // to the forward goes to the sender of the reply
        $parsed->setHeader('Reply-To', $replyto instanceof ezcMailAddress ? $replyto : $parsed->from->__toString());
  
+       // Using the original from address may not be permitted by the mailer.
+       $fromName = empty($parsed->from->name) ? $parsed->from->email : "{$parsed->from->name} ({$parsed->from->email})";
+       $parsed->from = new ezcMailAddress($fromEmail, $fromName);
        // CRM-17754 Include re-sent headers to indicate that we have forwarded on the email
        $domainEmail = $domainValues['values'][0]['from_email'];
        $parsed->setHeader('Resent-From', $domainEmail);
        $parsed->setHeader('Resent-Date', date('r'));
+       // Rewrite any invalid Return-Path headers.
+       $parsed->setHeader('Return-Path', $fromEmail);
  
        // $h must be an array, so we can't use generateHeaders()'s result,
        // but we have to regenerate the headers because we changed To
        $h = $parsed->headers->getCaseSensitiveArray();
        $b = $parsed->generateBody();
  
-       // strip Return-Path of possible bounding brackets, CRM-4502
-       if (!empty($h['Return-Path'])) {
-         $h['Return-Path'] = trim($h['Return-Path'], '<>');
-       }
        // FIXME: ugly hack - find the first MIME boundary in
        // the body and make the boundary in the header match it
        $ct = $h['Content-Type'];
        }
      }
      else {
-       if (empty($eq->display_name)) {
-         $from = $eq->email;
-       }
-       else {
-         $from = "\"{$eq->display_name}\" <{$eq->email}>";
-       }
+       $fromName = empty($eq->display_name) ? $eq->email : "{$eq->display_name} ({$eq->email})";
  
        $message = new Mail_mime("\n");
  
        $headers = array(
          'Subject' => "Re: {$mailing->subject}",
          'To' => $mailing->replyto_email,
-         'From' => $from,
+         'From' => "\"$fromName\" <$fromEmail>",
          'Reply-To' => empty($replyto) ? $eq->email : $replyto,
          'Return-Path' => CRM_Core_BAO_Domain::getNoReplyEmailAddress(),
          // CRM-17754 Include re-sent headers to indicate that we have forwarded on the email
  
      $to = empty($replyto) ? $eq->email : $replyto;
  
 -    $component = new CRM_Mailing_BAO_Component();
 +    $component = new CRM_Mailing_BAO_MailingComponent();
      $component->id = $mailing->reply_id;
      $component->find(TRUE);