Commit 8d8e11db authored by Jakob P's avatar Jakob P
Browse files

Issue #3308653 by japerry, rivimey: SMTP From address meta issue

parent ec5e4bc0
Loading
Loading
Loading
Loading
+23 −34
Original line number Diff line number Diff line
@@ -87,7 +87,7 @@ class SMTPMailSystem implements MailInterface, ContainerFactoryPluginInterface {
  /**
   * The SMTP object, stored between calls when keep alive is enabled.
   *
   * @var PHPMailer\PHPMailer\SMTP
   * @var \PHPMailer\PHPMailer\SMTP
   */
  protected $persistentSmtp;

@@ -255,9 +255,14 @@ class SMTPMailSystem implements MailInterface, ContainerFactoryPluginInterface {
      $from_name = $this->configFactory->get('system.site')->get('name');
    }

    $smtpFrom = $this->smtpConfig->get('smtp_from');
    // Set from email.
    if (!empty($message['params']['from_mail'])) {
    if (!empty($this->smtpConfig->get('smtp_from')) && $this->emailValidator->isValid($this->smtpConfig->get('smtp_from'))) {
      $from = $this->smtpConfig->get('smtp_from');
    }

    // If the SMTP module from email has not been provided, use the provided
    // from email
    elseif (!empty($message['params']['from_mail'])) {
      $from = $message['params']['from_mail'];
    }

@@ -266,11 +271,6 @@ class SMTPMailSystem implements MailInterface, ContainerFactoryPluginInterface {
      $from = $message['from'];
    }

    // Set SMTP module email from if its not overridden.
    elseif (!empty($this->smtpConfig->get('smtp_from')) && $this->emailValidator->isValid($this->smtpConfig->get('smtp_from'))) {
      $from = $this->smtpConfig->get('smtp_from');
    }

    // Set SMTP from the default site mail as a fallback.
    else {
      $from = $this->configFactory->get('system.site')->get('mail');
@@ -283,14 +283,11 @@ class SMTPMailSystem implements MailInterface, ContainerFactoryPluginInterface {
      $from = $matches[2];
    }

    // Updates $headers fields.
    $headers['sender'] = $from;
    $headers['return-path'] = $from;
    $headers['reply-to'] = $from;

    // Defines the From value to what we expect.
    $mailer->From = $from;
    $mailer->FromName = (new UnstructuredHeader('From', $from_name))->getBodyAsString();
    // Sender needs to be set to an email address only to prevent trying to
    // send an email with an invalid sender (where the name is present).
    $mailer->Sender = $from;

    $hostname = $this->smtpConfig->get('smtp_client_hostname');
@@ -323,19 +320,6 @@ class SMTPMailSystem implements MailInterface, ContainerFactoryPluginInterface {
        continue;
      }
      switch ($key) {
        case 'from':
          if ($from == NULL or $from == '') {
            // If a from value was already given, then set based on header.
            // Should be the most common situation since drupal_mail moves the.
            // from to headers.
            $from = $value;
            $mailer->From = $value;

            $mailer->FromName = '';
            $mailer->Sender = $value;
          }
          break;

        case 'content-type':
          // Parse several values on the Content-type header,
          // storing them in an array like.
@@ -411,18 +395,23 @@ class SMTPMailSystem implements MailInterface, ContainerFactoryPluginInterface {
          break;

        case 'reply-to':
          // Only add a "reply-to" if it's not the same as "return-path".
          if ($value != $headers['return-path']) {
          // Only add a "reply-to" if it's not the same as "from".
          $reply_to_comp = $this->getComponents($value);
            $mailer->AddReplyTo($reply_to_comp['email'], $reply_to_comp['name']);
          $reply_to_email = $reply_to_comp['email'];
          if ($reply_to_email !== $from) {
            $mailer->AddReplyTo($reply_to_email, $reply_to_comp['name']);
          }
          break;

        case 'sender':
          // Only add a "reply-to" if it's not the same as "return-path".
          if ($value != $headers['return-path']) {
            $reply_to_comp = $this->getComponents($value);
            $mailer->Sender = $value;
          // Header always needs to be set so the mail won't fail to be sent.
          $headers['sender'] = $from;
          // Change the "sender" if it's not the same as "from".
          $sender_comp = $this->getComponents($value);
          $sender_email = $sender_comp['email'];
          if ($sender_email !== $from) {
            $mailer->Sender = $sender_email;
            $headers['sender'] = $sender_email;
          }
          break;