Problem/Motivation

When calling $email->setTo() with a string email, the user is always switched to anonymous.

Proposed resolution

If the email belongs to a user, then we should switch to that user instead.

Remaining tasks

User interface changes

API changes

Data model changes

Original summary

With the latest version of module after committing #3280322: LegacyEmailBuilder calls hook_mail() too early current user in hook_mail is anonymous so tokens does not work.
Extra, we can see that construction email object using module name, key and params return undefined index error so emails body is always empty.

$email = $this->emailFactory->newModuleEmail('app', $key, $params);

We have to re set everything to $params:

$params['params'] = $params;
    $params['module'] = 'app';
    $params['key'] = $key;

    $email = $this->emailFactory->newModuleEmail('abv_app', $key, $params);

Steps to reproduce

// if you call here hook app_mail, the current user is correct
$params['params'] = $params;
    $params['module'] = 'app';
    $params['key'] = $key;

    $email = $this->emailFactory->newModuleEmail(app', $key, $params);

    /** @var \Drupal\symfony_mailer\MailerHelper $mailerHelper */
    $mailerHelper = \Drupal::service('symfony_mailer.helper');

    if (isset($params['from'])) {
      $email->setFrom($mailerHelper->parseAddress($params['from'], $params['langcode']));
      $email->setReplyTo($params['replyTo']);
    }

    if (isset($params['cc'])) {
      $email->setCc($params['cc']);
    }

    $email->setTo($mailerHelper->parseAddress($params['to'], $params['langcode']));
    if (!empty($message)) {
      $email->setBody(check_markup($message, 'full_html'));
    }

    if (isset($params['subject'])) {
      $email->setSubject($params['subject']);
    }

// but when send() calls hook abv_mail, the current user is anonymous.
    return $email->send();
Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

lamp5 created an issue. See original summary.

immaculatexavier made their first commit to this issue’s fork.

adamps’s picture

Status: Active » Needs review
StatusFileSize
new1.12 KB

I created a patch (untested) that I think would solve this.

This module sets the langcode and current user automatically from the 'To' address. If you want to send an email to a user account then this code is the best way:

$email->setTo($account);
Extra, we can see that construction email object using module name, key and params return undefined index error so emails body is always empty.

Please create a separate issue.

lamp5’s picture

It is not good approach to set current user based "TO", why:
there is a lot of scenarios where:

  • you send emails to only email addresses (not users) so your current user will be anonymous
  • user want to invite someone using emails so you want to use current_user tokens
  • etc

next, in this approach, you can not use account switcher to send email using correct context ...

adamps’s picture

It is not good approach to set current user based "TO", why:

Please create a new issue if you want to discuss that further. I think mostly your concerns are not true, the system is actually pretty flexible. By default it does what you want 99% of the time, and if not you can override it.

setTo($account) // Send to an account
setTo($email) // Send to an email address, switch to associated user if there is one
setTo(new Address($email, NULL, NULL, $account)) // Send to an email, using a specific account
you send emails to only email addresses (not users) so your current user will be anonymous

Which is normally exactly correct??? Otherwise it is possible accidentally to reveal protected information available from the current user which may be an admin.

you can not use account switcher to send email using correct context ...

This module makes it very easy to use the account switcher correctly.

adamps’s picture

Issue summary: View changes
StatusFileSize
new941 bytes

user want to invite someone using emails so you want to use current_user tokens

If you call $email->setParam('user', $current_user) then you can use user tokens. I feel this is better than relying on the current user.

Here is a slightly simpler patch. Before the patch, the user switch is always anonymous when calling $email->setTo() on a string. After, the user switch will be correct when sending to a user. Hopefully this is clearly better.

In a separate issue we could create a way to disable user switching, easier than in #5.

adamps’s picture

Status: Needs review » Fixed

  • AdamPS committed 6edad2c on 1.x
    Issue #3294459 by AdamPS: Current user is incorrect during sending...

Status: Fixed » Closed (fixed)

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