diff --git a/src/Illuminate/Contracts/Translation/HasLocalePreference.php b/src/Illuminate/Contracts/Translation/HasLocalePreference.php new file mode 100644 index 000000000000..28030a9132de --- /dev/null +++ b/src/Illuminate/Contracts/Translation/HasLocalePreference.php @@ -0,0 +1,11 @@ +to = $users; + if (! $this->locale && $users instanceof HasLocalePreference) { + $this->locale($users->preferredLocale()); + } + return $this; } diff --git a/tests/Integration/Mail/SendingMailWithLocaleTest.php b/tests/Integration/Mail/SendingMailWithLocaleTest.php index b1692be8ae67..7fd3417e0998 100644 --- a/tests/Integration/Mail/SendingMailWithLocaleTest.php +++ b/tests/Integration/Mail/SendingMailWithLocaleTest.php @@ -7,6 +7,9 @@ use Orchestra\Testbench\TestCase; use Illuminate\Support\Facades\Mail; use Illuminate\Support\Facades\View; +use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Collection; +use Illuminate\Contracts\Translation\HasLocalePreference; /** * @group integration @@ -63,6 +66,98 @@ public function test_mail_is_sent_with_selected_locale() ); } + public function test_locale_is_sent_with_model_locale() + { + $recipient = new TestEmailLocaleUser([ + 'email' => 'test@mail.com', + 'email_locale' => 'ar', + ]); + + Mail::to($recipient)->send(new TestMail()); + + $this->assertContains('esm', + app('swift.transport')->messages()[0]->getBody() + ); + } + + public function test_locale_is_sent_with_selected_locale_overriding_model_locale() + { + $recipient = new TestEmailLocaleUser([ + 'email' => 'test@mail.com', + 'email_locale' => 'en', + ]); + + Mail::to($recipient)->locale('ar')->send(new TestMail()); + + $this->assertContains('esm', + app('swift.transport')->messages()[0]->getBody() + ); + } + + public function test_locale_is_sent_with_model_locale_will_ignore_preferred_locale_of_the_cc_recipient() + { + $toRecipient = new TestEmailLocaleUser([ + 'email' => 'test@mail.com', + 'email_locale' => 'ar', + ]); + + $ccRecipient = new TestEmailLocaleUser([ + 'email' => 'test.cc@mail.com', + 'email_locale' => 'en', + ]); + + Mail::to($toRecipient)->cc($ccRecipient)->send(new TestMail()); + + $this->assertContains('esm', + app('swift.transport')->messages()[0]->getBody() + ); + } + + public function test_locale_is_not_sent_with_model_locale_when_there_are_multiple_recipients() + { + $recipients = [ + new TestEmailLocaleUser([ + 'email' => 'test@mail.com', + 'email_locale' => 'ar', + ]), + new TestEmailLocaleUser([ + 'email' => 'test.2@mail.com', + 'email_locale' => 'ar', + ]), + ]; + + Mail::to($recipients)->send(new TestMail()); + + $this->assertContains('name', + app('swift.transport')->messages()[0]->getBody() + ); + } + + public function test_locale_is_sent_for_multiple_recipients_when_has_emaile_locale_contract_is_given() + { + $recipients = (new class extends Collection implements HasLocalePreference { + public function preferredLocale() + { + return 'ar'; + } + })::make([ + new TestEmailLocaleUser([ + 'email' => 'test@mail.com', + 'email_locale' => 'es', + ]), + new TestEmailLocaleUser([ + 'email' => 'test.2@mail.com', + 'email_locale' => 'en', + ]), + ]); + + Mail::to($recipients)->send(new TestMail()); + + $this->assertContains('esm', + app('swift.transport')->messages()[0]->getBody() + ); + } + public function test_locale_is_set_back_to_default_after_mail_sent() { Mail::to('test@mail.com')->locale('ar')->send(new TestMail()); @@ -92,3 +187,16 @@ public function build() return $this->view('view'); } } + +class TestEmailLocaleUser extends Model implements HasLocalePreference +{ + protected $fillable = [ + 'email', + 'email_locale', + ]; + + public function preferredLocale() + { + return $this->email_locale; + } +}