EmailSender::replaceTokens fails with null value from $email->getReplyTo()

Created on 8 February 2024, 9 months ago
Updated 22 February 2024, 9 months ago

TypeError: Drupal\commerce_email\EmailSender::replaceTokens(): Argument #1 ($value) must be of type string, null given, called in /var/www/drupal10/web/modules/contrib/commerce_email/src/EmailSender.php on line 111 in Drupal\commerce_email\EmailSender->replaceTokens() (line 158 of /var/www/drupal10/web/modules/contrib/commerce_email/src/EmailSender.php).

Steps to reproduce

Not sure, but presumably send an email with a null value returned from $email->getReplyTo().

Proposed resolution

Allow null|string argument, or cast the null to an empty string before calling replaceTokens()?

๐Ÿ› Bug report
Status

Fixed

Version

1.0

Component

Code

Created by

๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom fonant

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @fonant
  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom fonant

    I've hacked a workaround for line 111 of commerce_email/src/EmailSender.php, which I think will fix the problem:

    'reply-to' => $this->replaceTokens(($email->getReplyTo() ?? ''), $replacements),
    

    Maybe we need an update function to add empty ReplyTo strings for older orders?

  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom fonant

    Doh! Of course the other way to fix this is to edit each email definition at /admin/commerce/config/emails and just re-save each one.

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia chetan 11

    chetan 11 โ†’ made their first commit to this issueโ€™s fork.

  • Merge request !18fixed โ†’ (Merged) created by chetan 11
  • Status changed to Needs review 9 months ago
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia chetan 11

    Please check the above MR.

  • Pipeline finished with Success
    9 months ago
    Total: 137s
    #90493
  • ๐Ÿ‡บ๐Ÿ‡ฆUkraine marchuk.vitaliy Rivne, UA

    vmarchuk โ†’ made their first commit to this issueโ€™s fork.

  • ๐Ÿ‡บ๐Ÿ‡ฆUkraine marchuk.vitaliy Rivne, UA

    Yeah, I just reproduced it too. In the getReplyTo() method we need to use the default value and that's it.
    MR updated with fixes.

  • ๐Ÿ‡ฎ๐Ÿ‡ฑIsrael jsacksick

    I think the right fix would be to change the code from:

    $params = [
          'id' => 'commerce_email_' . $email->id(),
          'from' => $this->replaceTokens($email->getFrom(), $replacements),
          'cc' => $this->replaceTokens($email->getCc(), $replacements),
          'bcc' => $this->replaceTokens($email->getBcc(), $replacements),
          'reply-to' => $this->replaceTokens($email->getReplyTo(), $replacements),
        ];

    to:

        $params = [
          'id' => 'commerce_email_' . $email->id(),
          'from' => $this->replaceTokens($email->getFrom(), $replacements),
          'cc' => $this->replaceTokens($email->getCc(), $replacements),
          'bcc' => $this->replaceTokens($email->getBcc(), $replacements),
        ];
        $reply_to = $email->getReplyTo();
        if (!empty($reply_to) {
          $params['reply-to'] = $reply_to;
        }
    
  • Status changed to Fixed 9 months ago
  • ๐Ÿ‡บ๐Ÿ‡ฆUkraine marchuk.vitaliy Rivne, UA

    Committed!

  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024