My Email template looks like this:
#component('mail::message')
# {{ $helloUser }}
#lang('welcome.message')
This
\App::setLocale('de);
$activeMail = new \App\Mail\Register\Activate($user);
\Mail::to($user)->send($activeMail);
will send an mail with German text.
However, when I use a queue
\App::setLocale('de);
$activeMail = new \App\Mail\Register\Activate($user);
\Mail::to($user)->queue($activeMail);
The mail is send in English, which is the default language of my app.
How can I send a message in German with the queue without changing the default language?
In Laravel 5.6. the Mailable class has gotten a locale method to care for this:
$activeMail = new \App\Mail\Register\Activate($user);
$locale = $user->lang; // de
\Mail::to($user)->locale($locale)->queue($activeMail);
For Laravel < 5.6 one could save the text in the mail object
class Activate extends Mailable
{
public $mainText
public function __construct()
{
$this->mainText = __('welcome.message');
}
}
and change the template to
#component('mail::message')
# {{ $helloUser }}
{{$mainText}}
The difference is that $mainText is the string from the language when the mail object was created, while #lang('welcome.message') would be the string of the default language from your app.
Since Laravel 5.7, there's something that can help you with that. Take a look at Localizing Mailables in the documentation.
use Illuminate\Contracts\Translation\HasLocalePreference;
class User extends Model implements HasLocalePreference
{
/**
* Get the user's preferred locale.
*
* #return string
*/
public function preferredLocale()
{
return $this->locale;
}
}
Related
What is the most straightforward way to get the current category in the view? I notice that there is a getTerm method in the Term class:
public function getEntity()
{
return $this->getTerm();
}
/**
* Returns the current Wordpress category
* This is just a wrapper for getCurrentCategory()
*
* #return \FishPig\WordPress\Model\Term
*/
public function getTerm()
{
if (!$this->hasTerm()) {
$this->setTerm($this->_registry->registry(Term::ENTITY));
}
return $this->_getData('term');
}
However if I try to utilize the method within a template (for example, the default post list wrapper.phtml template which utilizes the Term block in the layout) it throws an error:
<?php echo $this->getTerm() ?>
Recoverable Error: Object of class FishPig\WordPress\Model\Term could
not be converted to string in
I'm probably just missing something simple, any help would be greatly appreciated. Thanks!
$term = \Magento\Framework\App\ObjectManager::getInstance()
->get('Magento\Framework\Registry')
->registry('wordpress_term');
I'd like my application to use Mailables for all of the emails it sends, so I created my own ResetPasswordEmail class that extends Mailable. Then I created my own ResetPassword notification class that extends the vendor class of the same name and overrode the toMail method as follows:
public function toMail($notifiable)
{
return (new ResetPasswordEmail())->with('token', $this->token);
}
Then I overrode the sendPasswordResetNotification from the CanResetPassword trait in my User model like this:
public function sendPasswordResetNotification($token)
{
$this->notify(new ResetPassword($token));
}
Calling my custom ResetPassword notification class.
The problem is that if I use the default method of creating a MailMessage and sending it, it automatically populates the 'to' field with the user's email. But when I use my ResetPasswordEmail mailable class, it doesn't.
Is there a good way to get it to work like that with my custom mailable?
Well in the end I just set the "to" field for my Mailable instance like this:
public function toMail($notifiable)
{
return (new ResetPasswordEmail())->with('token', $this->token)->to($notifiable->email);
}
Since $notifiable is an instance of the User model in this case, I can get the email like that. I don't know if this is the best way to do it, but it works.
With Symfony 2.7, you could customize a form's name in your EntityType class with the method getName()
This is now deprecated. Is there another way to do that with Symfony 3.0 ?
I have custom prototype entry_rows for collections that I would need to use in different forms.
Since the name of the rows is based on the form's name, I would need to change the later in order to use them with a different form.
You should implements the getBlockPrefix method instead of getName as described in the migration guide here.
As example:
/**
* Returns the prefix of the template block name for this type.
*
* The block prefix defaults to the underscored short class name with
* the "Type" suffix removed (e.g. "UserProfileType" => "user_profile").
*
* #return string The prefix of the template block name
*/
public function getBlockPrefix()
{
return "form_name";
}
Hope this help
Depending on how your form is built, there is different ways to set the name of your form.
If you are creating the form through $this->createForm(CustomType::class):
$formFactory = $this->get('form.factory');
$form = $formFactory->createNamed('custom_form_name', CustomType::class);
If you are building the form from the controller directly through $this->createFormBuilder():
$formFactory = $this->get('form.factory');
$form = $formFactory->createNamedBuilder('custom_form_name', CustomType::class);
Look at the FormFactory and FormBuilder APIs for more information.
You can try it, remove prefix on field name
public function getBlockPrefix()
{
return null;
}
When I do a password reset in Laravel 5.1, I get the email, but it says "No Sender" in the title.
Is there a way to specify the sender from somewhere for password reset emails? Apart from the sender, I assume Laravel should automatically uses the email settings as specified in the config files? It's strange because when I set the Laravel mail config to use the 'mail' driver, I get bounced emails saying I can't send from a dynamic address (which is to be expected on dev), but still the password reset emails go through. Shouldn't email reset use the same config settings?
Jack, you can set in the From attributes for email id and name in config/mail.php. I too had the same problem and just got it sorted, as i mentioned above.
#SeriousJelly Answer Update for Laravel 5.2
in your Auth PasswordController Override resetEmailBuilder Method
class PasswordController extends Controller
{
protected function resetEmailBuilder()
{
return function (Message $message) {
$message->subject($this->getEmailSubject());
$message->from('you#email.com', 'you');
};
}
}
This might help someone
So, Alexey Mezenin's answer is almost there, however, one big no no is overwriting the core files as any future updates can break functionality.
As your PasswordController should be using the ResetsPassword trait you should be able to just overwrite any methods from ResetsPassword trait in your PasswordController.
For example, adding your own from and subject line to emails is a simple case of finding the relevant function in your trait, copy and pasting into your PasswordController and amending it.
Here is an example PasswordController with a function that over writes the sendResetLinkEmail() function.
<?php
namespace App\Http\Controllers\Auth;
use Illuminate\Http\Request;
use Illuminate\Mail\Message;
use Illuminate\Support\Facades\Password;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ResetsPasswords;
class PasswordController extends Controller
{
/*
|--------------------------------------------------------------------------
| Password Reset Controller
|--------------------------------------------------------------------------
|
| This controller is responsible for handling password reset requests
| and uses a simple trait to include this behavior. You're free to
| explore this trait and override any methods you wish to tweak.
|
*/
use ResetsPasswords;
/**
* Create a new password controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('guest');
}
/**
* Send a reset link to the given user.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function sendResetLinkEmail(Request $request)
{
$this->validate($request, ['email' => 'required|email']);
$broker = $this->getBroker();
$response = Password::broker($broker)->sendResetLink($request->only('email'), function (Message $message) {
$message->subject($this->getEmailSubject());
$message->from(env('MAIL_FROM'), env('APP_NAME'));
});
switch ($response) {
case Password::RESET_LINK_SENT:
return $this->getSendResetLinkEmailSuccessResponse($response);
case Password::INVALID_USER:
default:
return $this->getSendResetLinkEmailFailureResponse($response);
}
}
Maybe there is a better solution, but you could manually add code into \vendor\laravel\framework\src\Illuminate\Foundation\Auth\ResetPasswords.php, after this line:
$message->subject($this->getEmailSubject()); // this is line 66
Add something like this:
$message->from('my#email.com', 'My Site');
https://laravel.com/docs/5.1/mail#sending-mail
I've sanitized my form's input (textarea field) and when I display it on my view it comes out like this:
<p>I\'m in it to win it!! I\'m looking forward to playing the contest in <br />Contest Central. He aims to cross-pollinate the stage, screen and stereo <br />with work that speaks to both the humor and frustrations of modern life.</p>
In my controller I have this:
public function init(){
$this->view->setEscape('html_entity_decode');
$this->view->setEscape('stripslashes');
}
But only one works, if I erase one the setEscape then the other works and vice versa. So I can get stripslashes to work if I put it first but html_entity_decode wont work and vice versa
You need to define your own function that should be used for escaping. For example, you can defined a class My_Tools in library/My/Tools.php as follows:
<?php
#Tools.php
class My_Tools {
/**
* My custom escape function
*
* #param string $str String to be escaped
* #return string Escaped string
*/
static function myEscape($str) {
$str = html_entity_decode($str);
return stripslashes($str);
}
}
?>
Then, your init() could have the following form:
public function init() {
require_once(APPLICATION_PATH . '/../library/My/Tools.php');
$this->view->setEscape(array('My_Tools', 'myEscape'));
}
Off course it would be better to add Tools to Autoloader, but for this is just an example.