我想发送有关网络语言的特定语言的电子邮件。
例如,当用户注册时,他可以选择一种语言,例如英语 - en ,意大利语 - 它,德语 - de 和法语 - fr 。
该网站是多语言的,所以我想做的是当用户填写表格例如联系表格时,在他提交表格后,会向他发送电子邮件。
所以我们假设他选择意大利语作为该网站的语言,但是当他注册时,他选择了英语。所以即使网站是意大利语,也应该用英语发送电子邮件。
使用.pot文件通过cakephp的__()函数翻译电子邮件。
电子邮件模板是这样的:
contact_us_user
nx.{Graph|DiGraph|MultiGraph|MultiDiGraph}
发送电子邮件的功能是:
<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>');
?>
方法如下:
/* 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);
}
所以我要问的是,有没有一种方法或参数可以传递给电子邮件,以表明我希望以哪种语言发送电子邮件。
类似于:/**
* 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();
}
我的Cakephp版本是:2.5.6
提前致谢。
答案 0 :(得分:4)
我修改了一些@drmonkeyninja代码。 这样我也告诉网站将值存储在会话上以访问正确的语言环境:
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('info@biriola.com'=>'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;
}
因此,这样您甚至可以更改语言的会话值,然后再将其更改为prevows value。
希望这有帮助!
答案 1 :(得分:1)
您可以尝试在发送电子邮件时切换配置的语言,然后在发送电子邮件后恢复网站语言: -
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('info@sitename.com'=>'sitename.com'))
->to($emailTo)
->subject($subject);
$result = $Email->send();
// Restore to site language
Configure::write('Config.language', $siteLanguage);
return $result;
}
不确定为什么要在此处设置title_for_layout
。除了title_for_layout
为deprecated in CakePHP 2.5之外,如果在模型中定义此方法会更好;更好的方法是将电子邮件称为Event。