Cakephp send email in different user selected language -
i want sent email on specific language regarding of web language.
for example when user register has possibility select language example english - en, italian - it, german - de , french - fr.
the website multilingual, wanna when user fills form example contact form, , after submits form e-mail sent him.
so lets assume has selected italian language of website, when registered had selected english. e-mail should sent on english though site in italian.
emails translated through __() function of cakephp using .pot files.
email template 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 contacting us!'); echo "<br />"; echo __('we take <strong>reservation enquiry</strong> , booking quote'); echo "<br />"; echo "<hr />"; echo __('<p>thanks , regards.</p>'); ?>
and function send e-mail this:
/* send message 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 method 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('info@sitename.com'=>'sitename.com')) ->to($emailto) ->subject($subject); return $email->send(); }
so asking is there way or parameter pass email indicate in language want email sent. like: $language = 'en';
my cakephp version is: 2.5.6
thanks in advance.
i modified bit of @drmonkeyninja code. way im telling site store value on session access 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 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('info@biriola.com'=>'biriola.com')) ->to($emailto) ->subject($subject); $result = $email->send(); // restore site language $this->session->write('config.language', $sitelanguage); configure::write('config.language', $sitelanguage); return $result; }
so way change session value of language , change them again previows value.
hope helps!.
Comments
Post a Comment