Jump to content

Problem changing language for emails containing translateable strings


Juergen
 Share

Recommended Posts

Hello,

I have created a module which sends out emails to users triggered by LazyCron - works well.

The emails contain translateable strings and the problem is that the emails will be always sent in the default language and not in the stored user language.

Here is my method that sends out the emails:

/**
  * Create WireMail object and send the email with the activation link
  * @param User $user - the user object
  * @return bool - returns true on success and false on failure
  */
  protected function sendActivationLinkEmail(User $user): bool
  {

    // send an email with the activation link to the user
    // but check first if activation code (fl_activation) exists inside the database
    if($user->fl_activation){
      $m = wireMail();
      $m->to($user->email);
      $m->from($this->input_email);
      $m->subject($this->_('Action required to activate your account'));
      $m->body($this->createActivationLinkEmail($user));
      $m->mailTemplate($this->input_emailTemplate);
      return $m->send();
    }
    return false;

  }

The method is part of a trait that I use.

As you can see the subject contains a translateable string. The body text will be created by another method (createActivationLinkEmail) and contains also translateable strings, but you cannot see it here.

The emails will be sent, but the text is always in English (default), independant if the user language is English, German or another language.

I have tried to add

$this->wire('languages')->set($user->language->id);

but it does not work.

Can someone help me out how to set the language inside this method to send the emails in the user language?

Thanks in advance

Link to comment
Share on other sites

Hi @Juergen

Try to set current user language to the langauge that are set for user to which you are sending mail like 

 protected function sendActivationLinkEmail(User $user): bool
  {
	$savedLanguage = $this->wire('user')->language;
    $this->wire('user')->language = $user->language;

    // send an email with the activation link to the user
    // but check first if activation code (fl_activation) exists inside the database
    if($user->fl_activation){
      $m = wireMail();
      $m->to($user->email);
      $m->from($this->input_email);
      $m->subject($this->_('Action required to activate your account'));
      $m->body($this->createActivationLinkEmail($user));
      $m->mailTemplate($this->input_emailTemplate);
      return $m->send();
    }

	$this->wire('user')->language = $savedLanguage;

    return false;

  }

 

  • Thanks 1
Link to comment
Share on other sites

Thanks @Zeka

I have tried this, but it does not work. It seems the problem is that this method is inside a trait without any reference to a page language.

The trait (called activation in my case) will be included inside a class via

 use activation;

and it works - without the exception of the translations.

I have some other classes for rendering frontend forms and texts and in this case the translationable strings work.

But thank you for your answere.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...