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();
Comments
Comment #3
adamps commentedI 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:
Please create a separate issue.
Comment #4
lamp5It is not good approach to set current user based "TO", why:
there is a lot of scenarios where:
next, in this approach, you can not use account switcher to send email using correct context ...
Comment #5
adamps commentedPlease 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.
Which is normally exactly correct??? Otherwise it is possible accidentally to reveal protected information available from the current user which may be an admin.
This module makes it very easy to use the account switcher correctly.
Comment #6
adamps commentedIf 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.
Comment #7
adamps commented