Problem/Motivation

An email should be customised for the recipient: langcode and address (affects permissions to see fields/files). We can benefit from extending the address to include this extra information then langcode and address can be calculated automatically.

Before

$email->setTo($email->getParam('user'))
  ->setLangcode(...)
  ->setAddress(...)

After:

$email->setTo($email->getParam('user'));

Proposed resolution

Unfortunately Symfony address is a final class with no interface

  • Create a new class Address and interface AddressInterface.
  • Use AddressInterface on EmailInterface address get/set functions
  • AddressInterface has extra fields langcode, account compared with Symfony Address
  • Address::create automatically converts from a variety of data types
  • Calculate email langcode and account from To address

Remaining tasks

User interface changes

API changes

Data model changes

Comments

AdamPS created an issue. See original summary.

adamps’s picture

Status: Active » Needs review
StatusFileSize
new44.9 KB

  • AdamPS committed 4ad8b85 on 1.x
    Issue #3276863 by AdamPS: Calculate langcode and account from to address
    
adamps’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

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

lamp5’s picture

Ok that is clear for a new implementations, but how to apply language when mails are sending using default drupal service?
$mailManager->mail($module, $key, $to, $langcode, $params, $reply, $send);
It does not work right now.

adamps’s picture

@lamp5 Your question should be a separate issue please.

The way it should work is that LegacyEmailBuilder calls $this->mailManager->emailFromArray which calls $email->setAddress with a langcode.

However it has probably just been broken by #3280322: LegacyEmailBuilder calls hook_mail() too early because setTo() is only valid in the build phase. I reopened that issue. This commit is more recent than your comment so it's probably not the cause of your problem.

adamps’s picture

@lamp5 I guess that the correct fix for #3280322: LegacyEmailBuilder calls hook_mail() too early will solve your problem. hook_mail() is called to early, so before the correct language has been set.

seanb’s picture

How is this supposed to work if you want to use the language of a node when sending an email? I could understand using the account language as a default, but I guess you should be able to override that right? Is it possible to add setLangcode() back?

      $email = $this->emailFactory->newModuleEmail('antonius_mail', 'node_mail');
      $email
        ->setTo($group_mail)
        ->setSubject($node->label())
        ->appendBodyEntity($node, 'mail')
        ->setLangcode($node->language()->getId())
        ->send();