I want to sent email on a specific language regarding of the web language.
For example when the user register he has the possibility to select a language for example English - en, Italian - it, German - de and french - fr.
The website is multilingual, so what i wanna do is that when a user fills a form for example the contact form, and after he submits the form an e-mail is sent to him.
So lets assume that he has selected Italian as language of the website, but when he was registered he had selected English. So the E-mail should be sent on English even though the site is in Italian.
Emails are translated through __() function of cakephp using .pot files.
Email Template is this:
contact_us_user
<h2 style="color: #ee2424;">
<?php
echo __('SITENAME');
?>
</h2>
<?php
echo "<h2 style='text-align: left;'>";
if (isset($firstname) && isset($lastname) && isset($title)) {
echo __('Hello <span style="color: #ee2424;"> %s %s</span>.', $firstname, $lastname);
} else {
echo __('Hello');
}
echo "</h2>";
echo __('Thank you for contacting us!');
echo "<br />";
echo __('We will take a look to your <strong>reservation enquiry</strong> and we will get back to you with a booking quote');
echo "<br />";
echo "<hr />";
echo __('<p>Thanks and Regards.</p>');
?>
And the function that send the e-mail is this:
/* SEND MESSAGE TO THE USER */
$layout = 'default';
$template = 'contact_us_user';
$subject = __('TEST');
$title_for_layout = __('Contact US');
$viewVars = array(
"firstname" => $this->request->data['Contact']['name'],
"lastname" => $this->request->data['Contact']['surname'],
);
if(isset($this->request->data['Contact']['email']) && !empty($this->request->data['Contact']['email']) && trim($this->request->data['Contact']['email'])!='') {
$this->__sendEmail($this->request->data['Contact']['email'], $subject, $template, $viewVars, $layout, $title_for_layout);
}
And the method is as bellow:
/**
* send E-mail method
*
* @return boolean
*/
public function __sendEmail($emailTo, $subject = 'Email', $template = 'default', $viewVars, $layout = 'default', $title_for_layout = 'test') {
$this->set('title_for_layout', $title_for_layout);
App::uses('CakeEmail', 'Network/Email');
$Email = new CakeEmail();
$Email->template($template, $layout)
->emailFormat('html')
->viewVars($viewVars)
->from(array('[email protected]'=>'sitename.com'))
->to($emailTo)
->subject($subject);
return $Email->send();
}
So what I am asking is that is there a way or parameter to pass to email to indicate in which language I want email to be sent.
Something like: $language = 'en';
My Cakephp version is: 2.5.6
Thanks in advance.
I modified a bit of @drmonkeyninja code. This way im also telling the site to store the value on session to access the correct locale:
public function __sendEmailWithLanguage($emailTo, $viewVars, $lang, $subject = 'Email', $template = 'default', $layout = 'default') {
// Store site language
$siteLanguage = Configure::read('Config.language');
if (isset($lang) && !empty($lang) && trim($lang)!='') {
// Switch to preferred email language
$this->Session->write('Config.language', $lang);
Configure::write('Config.language', $lang);
}
// Send email
App::uses('CakeEmail', 'Network/Email');
$Email = new CakeEmail();
$Email->template($template, $layout)
->emailFormat('html')
->viewVars($viewVars)
->from(array('[email protected]'=>'Biriola.com'))
->to($emailTo)
->subject($subject);
$result = $Email->send();
// Restore to site language
$this->Session->write('Config.language', $siteLanguage);
Configure::write('Config.language', $siteLanguage);
return $result;
}
So this way you change even the session value of language and the you change them back again to previows value.
Hope this helps!.
You could perhaps try switching the configured language at the time of sending the email and then restore the site language after sending the email:-
public function __sendEmail($emailTo, $subject = 'Email', $template = 'default', $viewVars, $layout = 'default', $title_for_layout = 'test', $lang = 'eng') {
$this->set('title_for_layout', $title_for_layout);
App::uses('CakeEmail', 'Network/Email');
$Email = new CakeEmail();
// Store site language
$siteLanguage = Configure::read('Config.language');
// Switch to preferred email language
Configure::write('Config.language', $lang);
// Send email
$Email->template($template, $layout)
->emailFormat('html')
->viewVars($viewVars)
->from(array('[email protected]'=>'sitename.com'))
->to($emailTo)
->subject($subject);
$result = $Email->send();
// Restore to site language
Configure::write('Config.language', $siteLanguage);
return $result;
}
Not sure why you're setting the title_for_layout here. Apart from the fact that title_for_layout was deprecated in CakePHP 2.5 it would be better if this method was defined in a model; even better would be to have the email called as an Event.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With