Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mail::to($email)->send((new Mail())->local('en')) won't override the localization (As documented) #27023

Closed
Abu3safeer opened this issue Dec 31, 2018 · 4 comments

Comments

@Abu3safeer
Copy link

  • Laravel Version: 5.7.19
  • PHP Version: 7.2.12
  • Database Driver & Version: MariaDB 10.1.37

Description:

When trying to send an email with specific local using ->local() method on new Mailable class, example:

\Mail::to('[email protected]')->send((new NewOrder($order)->locale('en')));

while if you use ->local() on the Mail directly before ->send() method, it will override the local

\Mail::to('[email protected]')->locale('en')->send((new NewOrder($order)));

Steps To Reproduce:

tr;dr:

simply use ->local('en') on any Mailable instance, it won't change the localization of that email.

Detailed:

  1. Make several language files, (you can use files from this repository (https://github.com/caouecs/Laravel-lang ).
  2. Create an email (Mailable instance), and fill it with texts from localization files inside "resources\lang".
  3. Attach ->local() method on new Mailable instance like this code:
\Mail::to('[email protected]')->send((new NewOrder($order)->locale('en')));
  1. Try different localizations, it won't work.

Additional details:

This is the code I use exactly:

\Mail::to([
                env('ORDER_MAIL_TO_ADDRESS_1'),
                env('ORDER_MAIL_TO_ADDRESS_2')
                ])->send((new NewOrder($order))->locale('ar'));

The default local is English, I am trying to send this email in Arabic, it never arrived in Arabic except with this code:

\Mail::to([
                env('ORDER_MAIL_TO_ADDRESS_1'),
                env('ORDER_MAIL_TO_ADDRESS_2')
                ])->locale('ar')->send((new NewOrder($order)));

Not overriding method

I discovered this issue from Laravel documentations here:
https://laravel.com/docs/5.7/mail#localizing-mailables
image

Link for this method on the repository:

/**
* Set the locale of the message.
*
* @param string $locale
* @return $this
*/
public function locale($locale)
{
$this->locale = $locale;
return $this;
}

Overriding method

and found the working method here:
#25752
image

Link for this method on the repository:

/**
* Set the locale of the message.
*
* @param string $locale
* @return $this
*/
public function locale($locale)
{
$this->locale = $locale;
return $this;
}

@Abu3safeer Abu3safeer changed the title Mail::to($email)->send((new Mail)->local('en')) won't override the localization (As documented) Mail::to($email)->send((new Mail())->local('en')) won't override the localization (As documented) Dec 31, 2018
@koenhoeijmakers
Copy link
Contributor

You should call the locale method before sending, not after.
You can't change the locale for a mail that has already been sent

@Abu3safeer
Copy link
Author

This is totally makes sense, but what about this?

\Mail::to('[email protected]')->send((new NewOrder($order)->locale('en')));

as you can see, the local is set before the mail was sent, but still didn't work.
I don't have any issues anymore, but the above code is the documented one, beside it does send the email, so if anyone tried the code above would have the same issue, and won't figure out the reason because everything will seem right, code written as documented, Maillble has ->local() method, so they will be lost unless they follow read this PR #25752

to be honest I was about to use App::setLocal() to work around this issue, that PR saved me.
so I think this should be fixed since it is not difficult issue.

@derekmd
Copy link
Contributor

derekmd commented Jan 1, 2019

Yeah, I never touched Mohamed's original Mail service implementation. The PendingMail locale always overwrites the Mailable locale before the mail body is built. Someone else misunderstood the implementation here: #25691

The Mail service is meant to select the locale, not the mailable itself. It's possible to allow both but the framework code gets messier and requires more tests.

I didn't realize the docs were written like that. I'll submit a pull request later to correct it, calling Mail::to()->locale() instead.

@Abu3safeer
Copy link
Author

Thank you very much, since it was just misunderstanding, I will close this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants